| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use autodie qw(open close); |
| 7 |
use English qw( -no_match_vars ); |
| 8 |
use Getopt::Long; |
| 9 |
use Pod::Usage; |
| 10 |
use Carp; |
| 11 |
|
| 12 |
use Text::CSV; |
| 13 |
use File::Basename; |
| 14 |
use Cwd; |
| 15 |
|
| 16 |
use Template; |
| 17 |
|
| 18 |
use LaTeX::Table; |
| 19 |
use LaTeX::Encode; |
| 20 |
use LaTeX::Driver; |
| 21 |
|
| 22 |
use version; our $VERSION = qv('1.0.6'); |
| 23 |
|
| 24 |
my ( $infile, $outfile, $outfiletex, $help, $man, $version ); |
| 25 |
|
| 26 |
my $sep_char = q{,}; |
| 27 |
my $latex_encode = 0; |
| 28 |
my $landscape = 0; |
| 29 |
my $outputlatex = 0; |
| 30 |
my $theme = 0; |
| 31 |
my $title = 0; |
| 32 |
my $coldef = 0; |
| 33 |
my $header = 0; |
| 34 |
|
| 35 |
my $options_ok = GetOptions( |
| 36 |
'in=s' => \$infile, |
| 37 |
'out=s' => \$outfile, |
| 38 |
'sep_char=s' => \$sep_char, |
| 39 |
'latex_encode' => \$latex_encode, |
| 40 |
'landscape' => \$landscape, |
| 41 |
'outputlatex' => \$outputlatex, |
| 42 |
'theme=s' => \$theme, |
| 43 |
'title=s' => \$title, |
| 44 |
'coldef=s' => \$coldef, |
| 45 |
'header=s' => \$header, |
| 46 |
'help|?' => \$help, |
| 47 |
'version|v' => \$version, |
| 48 |
'man' => \$man, |
| 49 |
) or pod2usage(2); |
| 50 |
|
| 51 |
if ($version) { |
| 52 |
print "$PROGRAM_NAME $VERSION\n" or croak q{Can't print to stdout.}; |
| 53 |
exit; |
| 54 |
} |
| 55 |
|
| 56 |
if ($man) { |
| 57 |
pod2usage( -exitstatus => 0, -verbose => 2 ); |
| 58 |
} |
| 59 |
if ( $help || !defined $infile ) { |
| 60 |
pod2usage(1); |
| 61 |
} |
| 62 |
if ( !defined $outfile ) { |
| 63 |
$outfile = q{./} . fileparse( $infile, qw(csv txt dat) ) . 'pdf'; |
| 64 |
$outfiletex = fileparse( $infile, qw(csv txt dat) ) . 'tex'; |
| 65 |
} |
| 66 |
else { |
| 67 |
$outfiletex = fileparse( $outfile, qw(pdf) ) . 'tex'; |
| 68 |
} |
| 69 |
|
| 70 |
my $csv = Text::CSV->new( |
| 71 |
{ binary => 1, |
| 72 |
sep_char => $sep_char, |
| 73 |
allow_whitespace => 1 |
| 74 |
} |
| 75 |
); |
| 76 |
|
| 77 |
my @header; |
| 78 |
my @data; |
| 79 |
|
| 80 |
if ($header) { |
| 81 |
my $status = $csv->parse($header); |
| 82 |
@header = [ $csv->fields() ]; |
| 83 |
} |
| 84 |
|
| 85 |
my $line_number = 0; |
| 86 |
|
| 87 |
open my $IN, '<', $infile; |
| 88 |
while ( my $line = <$IN> ) { |
| 89 |
chomp $line; |
| 90 |
my $status = $csv->parse($line); |
| 91 |
if ( !$header && $line_number == 0 ) { |
| 92 |
@header = [ $csv->fields() ]; |
| 93 |
} |
| 94 |
else { |
| 95 |
push @data, [ $csv->fields() ]; |
| 96 |
} |
| 97 |
$line_number++; |
| 98 |
} |
| 99 |
close $IN; |
| 100 |
|
| 101 |
my $table = LaTeX::Table->new( |
| 102 |
{ header => \@header, |
| 103 |
data => \@data, |
| 104 |
type => 'longtable', |
| 105 |
tabletail => q{ }, |
| 106 |
( $theme ? ( theme => $theme ) : () ), |
| 107 |
filename => $outfiletex, |
| 108 |
center => 0, |
| 109 |
width_environment => 'tabularx', |
| 110 |
coldef => $coldef, |
| 111 |
callback => sub { |
| 112 |
my ( $row, $col, $value, $is_header ) = @_; |
| 113 |
if ($latex_encode) { |
| 114 |
return latex_encode($value); |
| 115 |
} |
| 116 |
return $value; |
| 117 |
}, |
| 118 |
} |
| 119 |
); |
| 120 |
|
| 121 |
$table->generate; |
| 122 |
|
| 123 |
my $latex_code = create_latex_code(); |
| 124 |
|
| 125 |
if ($outputlatex) { |
| 126 |
print $latex_code or croak q{Can't print to stdout.}; |
| 127 |
} |
| 128 |
|
| 129 |
my $drv = LaTeX::Driver->new( |
| 130 |
source => \$latex_code, |
| 131 |
output => $outfile, |
| 132 |
format => 'pdf', |
| 133 |
); |
| 134 |
|
| 135 |
my $ok = $drv->run; |
| 136 |
|
| 137 |
sub create_latex_code { |
| 138 |
|
| 139 |
my $template_obj = Template->new(); |
| 140 |
|
| 141 |
my %template_vars = ( |
| 142 |
LANDSCAPE => $landscape ? '[landscape]' : 0, |
| 143 |
TITLE => $title ? '\title{' . $title . '}' : 0, |
| 144 |
TABLECODE => '\LTXtable{\textwidth}{' |
| 145 |
. getcwd() . q{/} |
| 146 |
. $outfiletex . '}', |
| 147 |
); |
| 148 |
|
| 149 |
my $template = << 'EOT' |
| 150 |
\documentclass[%IF LANDSCAPE %][% LANDSCAPE %][% END %]{article} |
| 151 |
[% IF LANDSCAPE %]\usepackage[landscape]{geometry} |
| 152 |
[% END %]\usepackage{booktabs} |
| 153 |
\usepackage{xcolor} |
| 154 |
\usepackage{ltxtable} |
| 155 |
\usepackage{longtable} |
| 156 |
\usepackage{colortbl} |
| 157 |
\usepackage{array} |
| 158 |
\usepackage{helvet} |
| 159 |
[% IF TITLE %][% TITLE %][% END %] |
| 160 |
\begin{document} |
| 161 |
[% IF TITLE %]\maketitle |
| 162 |
[% END %][% TABLECODE %] |
| 163 |
\end{document}; |
| 164 |
EOT |
| 165 |
; |
| 166 |
my $code; |
| 167 |
|
| 168 |
$template_obj->process( \$template, \%template_vars, \$code ) |
| 169 |
or croak $template_obj->error; |
| 170 |
|
| 171 |
return $code; |
| 172 |
} |
| 173 |
|
| 174 |
__END__ |
| 175 |
|
| 176 |
=head1 NAME |
| 177 |
|
| 178 |
csv2pdf - A simple but yet powerful LaTeX::Table example application. |
| 179 |
|
| 180 |
=head1 SYNOPSIS |
| 181 |
|
| 182 |
csv2pdf [OPTIONS] --in in.csv [--out out.pdf] |
| 183 |
|
| 184 |
=head1 OPTIONS |
| 185 |
|
| 186 |
=over |
| 187 |
|
| 188 |
=item C<--sep_char> |
| 189 |
|
| 190 |
The separator character. Default comma ','. |
| 191 |
|
| 192 |
=item C<--latex_encode> |
| 193 |
|
| 194 |
Use L<LaTeX::Encode>. |
| 195 |
|
| 196 |
=item C<--landscape> |
| 197 |
|
| 198 |
Output the PDF in landscape orientation. |
| 199 |
|
| 200 |
=item C<--theme> |
| 201 |
|
| 202 |
The table theme. See L<LaTeX::Table>. Default I<Meyrin>. |
| 203 |
|
| 204 |
=item C<--coldef> |
| 205 |
|
| 206 |
The column definition, e.g. 'llp{5cm}'. If unset, guessed by L<LaTeX::Table>. |
| 207 |
|
| 208 |
=item C<--title> |
| 209 |
|
| 210 |
If set, then uses the specified string as title. |
| 211 |
|
| 212 |
=item C<--header> |
| 213 |
|
| 214 |
Instead of the first line, use the specified string as header. |
| 215 |
|
| 216 |
--header "Header A, Header B, Header C" |
| 217 |
|
| 218 |
The separator character must be the same as in the file. If unset, then the |
| 219 |
first line is used as header. |
| 220 |
|
| 221 |
=item C<--outputlatex> |
| 222 |
|
| 223 |
Prints the LaTeX code to STDOUT. |
| 224 |
|
| 225 |
=item C<--man> |
| 226 |
|
| 227 |
Display manpage. |
| 228 |
|
| 229 |
=item C<--version> |
| 230 |
|
| 231 |
Print version number of this software. |
| 232 |
|
| 233 |
=back |
| 234 |
|
| 235 |
=head1 DESCRIPTION |
| 236 |
|
| 237 |
Converts a CSV file to PDF. Requires LaTeX. |
| 238 |
|
| 239 |
=head1 EXAMPLE |
| 240 |
|
| 241 |
csv2pdf --landscape --theme Redmond --in examples/imdbtop40.dat |
| 242 |
|
| 243 |
=head1 CONFIGURATION AND ENVIRONMENT |
| 244 |
|
| 245 |
C<csv2pdf> does not support configuration files or environment variables. |
| 246 |
|
| 247 |
=head1 DEPENDENCIES |
| 248 |
|
| 249 |
LaTeX. |
| 250 |
|
| 251 |
L<autodie>, L<Carp>, L<Cwd>, L<File::Basename>, L<Getopt::Long>, L<LaTeX::Driver>, L<LaTeX::Encode>, L<LaTeX::Table>, L<Pod::Usage>, L<Template>, L<Text::CSV> |
| 252 |
|
| 253 |
=head1 BUGS AND LIMITATIONS |
| 254 |
|
| 255 |
No bugs have been reported. |
| 256 |
|
| 257 |
Please report any bugs or feature requests to |
| 258 |
C<[email protected]>, or through the web interface at |
| 259 |
L<http://rt.cpan.org>. |
| 260 |
|
| 261 |
=head1 LICENSE AND COPYRIGHT |
| 262 |
|
| 263 |
Copyright (c) 2006-2010, C<< <[email protected]> >>. |
| 264 |
|
| 265 |
This program is free software; you can redistribute it and/or |
| 266 |
modify it under the same terms as Perl itself. See L<perlartistic>. |
| 267 |
|
| 268 |
=head1 DISCLAIMER OF WARRANTY |
| 269 |
|
| 270 |
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY |
| 271 |
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN |
| 272 |
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES |
| 273 |
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER |
| 274 |
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 275 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE |
| 276 |
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH |
| 277 |
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL |
| 278 |
NECESSARY SERVICING, REPAIR, OR CORRECTION. |
| 279 |
|
| 280 |
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING |
| 281 |
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR |
| 282 |
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE |
| 283 |
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, |
| 284 |
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE |
| 285 |
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING |
| 286 |
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A |
| 287 |
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF |
| 288 |
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF |
| 289 |
SUCH DAMAGES. |
| 290 |
|
| 291 |
=cut |
| 292 |
|
| 293 |
# vim: ft=perl sw=4 ts=4 expandtab |