Turning HTML Results Page into PDF and Downloading

2019-09-07 01:11发布

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条回答
该账号已被封号
2楼-- · 2019-09-07 01:23

If you are open to an external program/dependency for the conversion I highly recommend http://wkhtmltopdf.org/

Flow:

  1. Button Click
  2. wkhtmltopdf is provided target URL
  3. 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

查看更多
登录 后发表回答