Creating PDF with perl

What is it about

This site is about a perl-module i created to help me with some problems creating PDF documents from perl. A long time i was looking for a possibility to simply create PDF’s out of perl for the web environment. In most cases there was the problem to create a pdf document from a present HTML-Page. One day i found htmldoc, a shell(and x)-programm that is able exactly to do this. Because i was to lazy to deal with all the command-line-switches over and over again, i wrote the perl-module HTML::HTMLDoc as an adaptor.

Prerequirements

The Perl-Module requires a command-line tool called htmldoc. It can be downloaded in open-source or commercial license:
http://www.htmldoc.org/

Where can it be downloaded

You can find the perl-module using the CPAN-Network

or using the cpan shell command:

> cpan
> install HTML::HTMLDoc

Features

Because the perl module is only an adaptor for the command-line tool, it only supports the features supported by it. So the actual version 1.8.24 unfortunatly does not support CSS-stylesheets, the Perl module can not support it. The implementation of css is accounced for the next mature release.

Examples

Here you get some example of how to use the module.



simply create a pdf from html-code that is in a scalar and print out the pdf:


use HTML::HTMLDoc;

# create new instance
my $htmldoc = new HTML::HTMLDoc();

# feed it with some html-code
$htmldoc->set_html_content(qq~A PDF file~);
# produce a pdf from the settings and print it out
my $pdf = $htmldoc->generate_pdf();
print $pdf->to_string();



You can also store the pdf to a file rather than printing it directy:

print $pdf->to_file("/tmp/example.pdf");



use a present html-file from your filesystem for feeding:
$htmldoc->set_input_file($filename)



If you want to send the pdf-file directly to a client of a web-server, you need to send a correct header first, to tell the clients browser what it will get here:

print "Content-type:application/pdf\n\n";
print $pdf->to_string();