Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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

In reply to Re^2: How to create beautiful PDF from Pod by Tux
in thread How to create beautiful PDF from Pod by Tux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (14)
    As of 2014-11-26 08:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (164 votes), past polls