How can I convert HTML to PDF using Perl?

后端 未结 6 948
生来不讨喜
生来不讨喜 2020-12-10 05:43

I need to convert some HTML reports into PDF using Perl. What are the best CPAN modules for the job?

相关标签:
6条回答
  • 2020-12-10 06:11

    It depends on exactly what you need to do, but I'd probably look at Template::Extract and PDF::Template.

    0 讨论(0)
  • 2020-12-10 06:21

    I hope PDF::FromHTML may be of help.

    0 讨论(0)
  • 2020-12-10 06:21

    PDF::WebKit

    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>
    
    0 讨论(0)
  • 2020-12-10 06:22

    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.

    0 讨论(0)
  • 2020-12-10 06:27

    HTML::HTMLDoc uses the underlying htmldoc C library which is built to do just this. And it's pretty fast too.

    0 讨论(0)
  • 2020-12-10 06:32

    I've used PDF::API2 to create PDF reports with great success.

    0 讨论(0)
提交回复
热议问题