Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: How to create beautiful PDF from Pod

by Tux (Monsignor)
on Oct 28, 2012 at 14:50 UTC ( #1001271=note: print w/ replies, xml ) Need Help??


in reply to Re: How to create beautiful PDF from Pod
in thread How to create beautiful PDF from Pod

The pyhton stuff installed but failed due to missing dependency. I didn't want to pursue that.

The wkhtmltopdf solution works quite well. The perl module around it sucks, as it does not offer any features beyond the program itself. I would have hoped for it to support streaming (piped input, piped output), but all it does is pass the arguments to the system command. I can do that myself.

Pod::Html shows its age. Not well thought through, sadly. Even if one uses local *STDOUT;, there is no way to get its output caught. This module also doe not support piping/streaming.

I ended up with the code below (feel free to use, alter or comment on), where I still note a few drawbacks

  • Page wraps are unclean. On many page breaks the top half of a line is on the end of one page, the bottom half is on the next
  • CSS like text-align: justify; doesn't work very well.
#!/pro/bin/perl use 5.14.1; use warnings; sub usage { my $err = shift and select STDERR; say "usage: pod2pdf [--output=file.pdf] [input]"; exit $err; } # usage use Getopt::Long qw(:config bundling); my $opt_o; my $opt_t; my $opt_c; GetOptions ( "help|?" => sub { usage (0); }, "o|output=s" => \$opt_o, "t|title=s" => \$opt_t, "c|css=s" => \$opt_c, ) or usage (1); use Pod::Html; use File::Temp qw( tempdir ); # Pod::Html doesn't support piped output + :( :( use HTML::TreeBuilder; my $td = tempdir (CLEANUP => 1); my %tf = map { $_ => "$td/pod2pdf.$_" } "html", "pdf"; my $html; { pod2html "--outfile=$tf{html}", @ARGV; # Cleanup leftover junk from Pod::Html :( -f $_ && unlink $_ for map { "$_/pod2htmd.tmp" } ".", $ENV{TMPDIR} + // "/tmp"; open my $fh, "<", $tf{html}; $html = join "" => <$fh>; close $fh; } my $css = join "" => <DATA>; if ($opt_c && open my $fh, "<", $opt_c) { $css = join "" => <$fh>; close $fh; } my $tree = HTML::TreeBuilder->new; $tree->parse_content ($html); foreach my $head ($tree->look_down (_tag => "head")) { my $style = HTML::Element->new ("style"); $style->attr (type => "text/css"); $style->push_content ($css); $head->push_content ($style); } my $pdf = do { open my $fh, ">", $tf{html} or die "$tf{html}: $!\n"; print $fh $tree->as_HTML (undef, " ", {}); close $fh; my @cmd = qw( wkhtmltopdf --quiet --load-error-handling ignore ); $opt_t and push @cmd, "--title", $opt_t; push @cmd, $tf{html}, $tf{pdf}; system @cmd; open $fh, "<", $tf{pdf} or die "$tf{pdf}: $!\n"; join "" => <$fh>; }; if ($opt_o) { open my $fh, ">", $opt_o or die "$opt_o: $!\n"; print $fh $pdf; close $fh; } else { print $pdf; } __END__ body, p, ul, ol, h1, h2, h3 { font-family: "DejaVu Sans", "Nimbus Sans L", Helvetica, sans-serif; } body { color: Black; background: White; } a:link { color: Purple; } a:visited { color: Maroon; } pre, tt, code { font-family: "DejaVu Sans Mono", "Liberation Mono", LettrGothic12BT, "Lucida Console", mono, monospace; } p, ul, ol { text-align: left; } h1 { color: Maroon; } h2 { color: Green; } h3 { color: Navy; }

Enjoy, Have FUN! H.Merijn


Comment on Re^2: How to create beautiful PDF from Pod
Select or Download Code
Re^3: How to create beautiful PDF from Pod
by daxim (Chaplain) on Oct 29, 2012 at 08:09 UTC
    potyl's work with HTML → Webkit → Cairo → PDF is super-sweet: CPAN, Github. There's lots of example code strewn around, check every distro/repository that looks related/has the keywords in the name.

    As of 2012, this is the most modern and formidable solution.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1001271]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-07-29 03:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (211 votes), past polls