<?xml version="1.0" encoding="windows-1252"?>
<node id="1001271" title="Re^2: How to create beautiful PDF from Pod" created="2012-10-28 10:50:43" updated="2012-10-28 10:50:43">
<type id="11">
note</type>
<author id="200365">
Tux</author>
<data>
<field name="doctext">
&lt;p&gt;The pyhton stuff installed but failed due to missing dependency. I didn't want to pursue that.&lt;/p&gt;
&lt;p&gt;The wkhtmltopdf solution works quite well. The perl module around it sucks, as it does not offer &lt;i&gt;any&lt;/i&gt; 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.&lt;/p&gt;
&lt;p&gt;[mod://Pod::Html] shows its age. Not well thought through, sadly. Even if one uses &lt;c&gt;local *STDOUT;&lt;/c&gt;, there is no way to get its output caught. This module also doe not support piping/streaming.&lt;/p&gt;
&lt;p&gt;I ended up with the code below (feel free to use, alter or comment on), where I still note a few drawbacks …&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;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&lt;/li&gt;
  &lt;li&gt;CSS like &lt;c&gt;text-align: justify;&lt;/c&gt; doesn't work very well.&lt;/li&gt;
  &lt;/ul&gt;
&lt;readmore&gt;&lt;code&gt;
#!/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|?"     =&gt; sub { usage (0); },
    "o|output=s" =&gt; \$opt_o,
    "t|title=s"  =&gt; \$opt_t,
    "c|css=s"    =&gt; \$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 =&gt; 1);
my %tf = map { $_ =&gt; "$td/pod2pdf.$_" } "html", "pdf";

my $html;
{   pod2html "--outfile=$tf{html}", @ARGV;
    # Cleanup leftover junk from Pod::Html :(
    -f $_ &amp;&amp; unlink $_ for map { "$_/pod2htmd.tmp" } ".", $ENV{TMPDIR} // "/tmp";
    open my $fh, "&lt;", $tf{html};
    $html = join "" =&gt; &lt;$fh&gt;;
    close $fh;
    }

my $css  = join "" =&gt; &lt;DATA&gt;;
if ($opt_c &amp;&amp; open my $fh, "&lt;", $opt_c) {
    $css = join "" =&gt; &lt;$fh&gt;;
    close $fh;
    }

my $tree = HTML::TreeBuilder-&gt;new;
$tree-&gt;parse_content ($html);
foreach my $head ($tree-&gt;look_down (_tag =&gt; "head")) {
    my $style = HTML::Element-&gt;new ("style");
    $style-&gt;attr (type =&gt; "text/css");
    $style-&gt;push_content ($css);
    $head-&gt;push_content ($style);
    }

my $pdf = do {
    open my $fh, "&gt;", $tf{html} or die "$tf{html}: $!\n";
    print $fh $tree-&gt;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, "&lt;", $tf{pdf} or die "$tf{pdf}: $!\n";
    join "" =&gt; &lt;$fh&gt;;
    };

if ($opt_o) {
    open my $fh, "&gt;", $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;                  }
&lt;/code&gt;&lt;/readmore&gt;
&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-200365"&gt;
&lt;br /&gt;
Enjoy, Have FUN! H.Merijn
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1001077</field>
<field name="parent_node">
1001084</field>
</data>
</node>
