I need to convert some HTML reports into PDF using Perl. What are the best CPAN modules for the job?
It depends on exactly what you need to do, but I'd probably look at Template::Extract and PDF::Template.
I hope PDF::FromHTML may be of help.
I could convert from HTML to PDF with Perl with PDF::WebKit, which in turn uses wkhtmltopdf. From apt show wkhtmltopdf
:
Command line utilities to convert html to pdf or image using WebKit wkhtmltopdf is a command line program which permits one to create a pdf or an image from an url, a local html file or stdin. It produces a pdf or an image like rendered with the WebKit engine.
This program requires an X11 server to run.
Therefore, this solution seems unacceptable on a server. Maybe WeasyPrint (built with Python) or athenapdf? Or Pandoc?
The latest version is headless (does not require X server).
Installation:
sudo cpanm install PDF::WebKit
sudo apt install xfonts-75dpi
sudo apt install wkhtmltopdf
Use .deb from official site to get the latest version.
html2pdf.pl
#!/usr/bin/perl
use PDF::WebKit;
my $kit = PDF::WebKit->new('/tmp/index.html');
my $file = $kit->to_file('/tmp/my.pdf');
Sample, index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Title</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
</head>
<body>
<p class="text-primary">.text-primary</p>
<p class="text-secondary">.text-secondary</p>
<p class="text-success">.text-success</p>
<p class="text-danger">.text-danger</p>
<p class="text-warning">.text-warning</p>
</body>
</html>
PinceXML
That doesn't answer you question in the sense of using Perl, but as far as I know that is the best HTML to PDF converter available.
HTML::HTMLDoc uses the underlying htmldoc C library which is built to do just this. And it's pretty fast too.
I've used PDF::API2 to create PDF reports with great success.