| 1 | #!/usr/local/bin/perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Getopt::Long; | ||
| 5 | use PDF::FromHTML; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | html2pdf.pl - Convert HTML to PDF | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | B<html2pdf.pl> S<[ B<-l> ]> S<[ B<-e> I<encoding> ] [ B<-f> I<font> ] [ B<-s> I<size> ]> | ||
| 14 | S<I<input.html> [ I<output.pdf> ]> | ||
| 15 | |||
| 16 | If I<input.html> is not given, reads HTML from standard input. | ||
| 17 | |||
| 18 | If I<output.pdf> is not given, writes PDF to the same name as the input file | ||
| 19 | with an additional F<.pdf> suffix, or to standard output if it's being | ||
| 20 | redirected to somewhere other than a terminal. | ||
| 21 | |||
| 22 | If I<encoding> is not given, the input encoding defaults to C<utf-8>. | ||
| 23 | |||
| 24 | If I<font> is not given, the base font family defaults to C<Helvetica>. | ||
| 25 | The value of I<font> can be a truetype font file, one of the PDF core fonts, | ||
| 26 | or one of: C<traditional>/C<simplified>/C<korean>/C<japanese>. | ||
| 27 | |||
| 28 | If I<size> is not given, the base font size defaults to C<12> points. | ||
| 29 | |||
| 30 | If B<-l> is specified, the output uses landscape layout. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | my $font = 'Helvetica'; | ||
| 35 | my $encoding = 'utf-8'; | ||
| 36 | my $size = 12; | ||
| 37 | my $landscape = 0; | ||
| 38 | GetOptions( | ||
| 39 | "e|encoding=s" => \$encoding, | ||
| 40 | "f|font=s" => \$font, | ||
| 41 | "s|size=s" => \$size, | ||
| 42 | "l|landscape" => \$landscape, | ||
| 43 | ); | ||
| 44 | |||
| 45 | my $pdf = PDF::FromHTML->new( | ||
| 46 | encoding => $encoding, | ||
| 47 | ); | ||
| 48 | |||
| 49 | local $SIG{__DIE__} = sub { require Carp; Carp::confess(@_) }; | ||
| 50 | |||
| 51 | my $input_file = @ARGV ? shift : '-'; | ||
| 52 | my $output_file = @ARGV ? shift : (-t STDOUT and $input_file ne '-') ? "$input_file.pdf" : '-'; | ||
| 53 | |||
| 54 | $pdf->load_file($input_file); | ||
| 55 | $pdf->convert( | ||
| 56 | Font => $font, | ||
| 57 | LineHeight => $size, | ||
| 58 | Landscape => $landscape, | ||
| 59 | ); | ||
| 60 | #warn $pdf->twig->sprint; | ||
| 61 | $pdf->write_file($output_file); | ||
| 62 | |||
| 63 | 1; | ||
| 64 | |||
| 65 | __END__ |