#!/usr/bin/perl ############################################################################# # $Author: markus $ # $Date: 2009-09-07 17:06:22 +0200 (Mon, 07 Sep 2009) $ # $Revision: 1836 $ ############################################################################# use strict; use warnings; use utf8; use autodie qw(open close); use English qw( -no_match_vars ); use Getopt::Long; use Pod::Usage; use Carp; use Text::CSV; use File::Basename; use Cwd; use Template; use LaTeX::Table; use LaTeX::Encode; use LaTeX::Driver; use version; our $VERSION = qv('1.0.0'); my ( $infile, $outfile, $outfiletex, $help, $man, $version ); my $sep_char = q{,}; my $latex_encode = 0; my $landscape = 0; my $outputlatex = 0; my $theme = 0; my $title = 0; my $coldef = 0; my $header = 0; my $options_ok = GetOptions( 'in=s' => \$infile, 'out=s' => \$outfile, 'sep_char=s' => \$sep_char, 'latex_encode' => \$latex_encode, 'landscape' => \$landscape, 'outputlatex' => \$outputlatex, 'theme=s' => \$theme, 'title=s' => \$title, 'coldef=s' => \$coldef, 'header=s' => \$header, 'help|?' => \$help, 'version|v' => \$version, 'man' => \$man, ) or pod2usage(2); if ($version) { print "$PROGRAM_NAME $VERSION\n" or croak q{Can't print to stdout.}; exit; } if ($man) { pod2usage( -exitstatus => 0, -verbose => 2 ); } if ( $help || !defined $infile ) { pod2usage(1); } if ( !defined $outfile ) { $outfile = q{./} . fileparse( $infile, qw(csv txt dat) ) . 'pdf'; $outfiletex = fileparse( $infile, qw(csv txt dat) ) . 'tex'; } else { $outfiletex = fileparse( $outfile, qw(pdf) ) . 'tex'; } my $csv = Text::CSV->new( { binary => 1, sep_char => $sep_char, allow_whitespace => 1 } ); my @header; my @data; if ($header) { my $status = $csv->parse($header); @header = [ $csv->fields() ]; } my $line_number = 0; open my $IN, '<', $infile; while ( my $line = <$IN> ) { chomp $line; my $status = $csv->parse($line); if ( !$header && $line_number == 0 ) { @header = [ $csv->fields() ]; } else { push @data, [ $csv->fields() ]; } $line_number++; } close $IN; my $table = LaTeX::Table->new( { header => \@header, data => \@data, type => 'longtable', tabletail => q{ }, ( $theme ? ( theme => $theme ) : () ), filename => $outfiletex, center => 0, width_environment => 'tabularx', coldef => $coldef, callback => sub { my ( $row, $col, $value, $is_header ) = @_; if ($latex_encode) { return latex_encode($value); } return $value; }, } ); $table->generate; my $latex_code = create_latex_code(); if ($outputlatex) { print $latex_code or croak q{Can't print to stdout.}; } my $drv = LaTeX::Driver->new( source => \$latex_code, output => $outfile, format => 'pdf', ); my $ok = $drv->run; sub create_latex_code { my $template_obj = Template->new(); my %template_vars = ( LANDSCAPE => $landscape ? '[landscape]' : 0, TITLE => $title ? '\title{' . $title . '}' : 0, TABLECODE => '\LTXtable{\textwidth}{' . getcwd() . q{/} . $outfiletex . '}', ); my $template = << 'EOT' \documentclass[%IF LANDSCAPE %][% LANDSCAPE %][% END %]{article} [% IF LANDSCAPE %]\usepackage[landscape]{geometry} [% END %]\usepackage{booktabs} \usepackage{xcolor} \usepackage{ltxtable} \usepackage{longtable} \usepackage{colortbl} \usepackage{array} \usepackage{helvet} [% IF TITLE %][% TITLE %][% END %] \begin{document} [% IF TITLE %]\maketitle [% END %][% TABLECODE %] \end{document}; EOT ; my $code; $template_obj->process( \$template, \%template_vars, \$code ) or croak $template_obj->error; return $code; } __END__ =head1 NAME csv2pdf - A simple but yet powerful LaTeX::Table example application. =head1 SYNOPSIS csv2pdf [OPTIONS] --in in.csv [--out out.pdf] =head1 OPTIONS =over =item C<--sep_char> The separator character. Default comma ','. =item C<--latex_encode> Use L. =item C<--landscape> Output the PDF in landscape orientation. =item C<--theme> The table theme. See L. Default I. =item C<--coldef> The column definition, e.g. 'llp{5cm}'. If unset, guessed by L. =item C<--title> If set, then uses the specified string as title. =item C<--header> Instead of the first line, use the specified string as header. --header "Header A, Header B, Header C" The separator character must be the same as in the file. If unset, then the first line is used as header. =item C<--outputlatex> Prints the LaTeX code to STDOUT. =item C<--man> Display manpage. =item C<--version> Print version number of this software. =back =head1 DESCRIPTION Converts a CSV file to PDF. Requires LaTeX. =head1 EXAMPLE csv2pdf --landscape --theme Redmond --in examples/imdbtop40.dat =head1 CONFIGURATION AND ENVIRONMENT C does not support configuration files or environment variables. =head1 DEPENDENCIES LaTeX. L, L, L, L, L, L, L, L, L, L