问题
I am working in PERL and I have an html results page that displays perfectly. I want to add a button that makes this html page a pdf that downloads onto your machine. Below is the code that generates the html page. fileFH is generated in the CGI code.
sub searchResults {
my $data = shift;
my $file = "$OUTFILES/$data.html";
open(my $fileFH, '<', $file) or return "Can not find file\n";
print $cgi->header();
while (<$fileFH>) {
print qq($_);
}
close($fileFH);
}
回答1:
If you are open to an external program/dependency for the conversion I highly recommend http://wkhtmltopdf.org/
Flow:
- Button Click
- wkhtmltopdf is provided target URL
- Output sent to user
You may want to cache the output as well to reduce the number of conversions.
EDIT Perl already has a wrapper too.
https://metacpan.org/release/PDF-WebKit
来源:https://stackoverflow.com/questions/39500817/turning-html-results-page-into-pdf-and-downloading-in-cgi-file