How can I automate HTML-to-PDF conversions?

后端 未结 14 790
攒了一身酷
攒了一身酷 2020-12-23 19:29

I\'ve been using htmldoc for a while, but I\'ve run into some fairly serious limitations. I need the end solution to work on a Linux box. I\'ll be calling this library/utili

相关标签:
14条回答
  • 2020-12-23 20:19

    WeasyPrint produces nice PDFs with selectable text and hyperlinks.

    weasyprint input.html output.pdf
    

    If you use wkhtmltopdf instead, try the following options:

    wkhtmltopdf --margin-bottom 20mm --margin-top 20mm --minimum-font-size 16 ...
    
    0 讨论(0)
  • 2020-12-23 20:19

    I have found Electroshot to be supportive of modern CSS features, particularly layout. This was after struggling with wkhtmltopdf showing its age in not supporting things like CSS3.

    From Electroshot's features description:

    Electroshot uses Electron, which offers the most recent stable version of Chrome (rather than one from years ago); this means that pages render as they would in a browser...

    I've been able to use Bootstrap 4 to design a page, and then use Electroshot to render a PDF very closely resembling the HTML/CSS.

    0 讨论(0)
  • 2020-12-23 20:25

    Sorry to unearth this old post, but it came out first in my search for the best HTML/PDF conversion tool. On Linux wkhtmltopdf is very good (takes into account CSS, among others) and GPL.

    0 讨论(0)
  • 2020-12-23 20:26

    This would be total overkill, but you could download and install mirth. It is a message routing engine, but it has the ability to convert html to pdf, so you could set it up to pick up an html file in a folder, convert to pdf, and drop the pdf in the same or other folder. Like I said, overkill, a bit of a learning curve, but it's free, and java so you can run it on linux if you like. And all your perl app would have to do is drop the html to a file.

    0 讨论(0)
  • 2020-12-23 20:27

    I have started to put together a tool to provide a simplified interface to common actions.

    You can convert an HTML to a PDF like this:

    $ npm install @lancejpollard/act -g
    $ act convert tmp/index.html -o tmp/index.pdf -w 2000px -h 3000px
    

    This will create a new PDF for the HTML file.

    If nothing else check out the source and see how to write your own script to do this in JavaScript.

    0 讨论(0)
  • 2020-12-23 20:28

    You can install the free Calibre, and use the ebook-convert command line utility it has, to convert many html documents into a single epub, or pdf.

    https://manual.calibre-ebook.com/generated/en/ebook-convert.html

    Idea comes from here

    I haven't used it, but this npm module wraps this process up like my following bash script, but probably better ;-)

    For me, on my mac, I use the following bash script to convert a local html website to a PDF:

    convert_html_to_pdf.sh

    function show_help()
    {
      ME=$(basename $0)
      IT=$(cat <<EOF
      
      Converts an html file to pdf, epub, mobi or more if you look!
    
      usage: input.html output.{pdf|epub|mobi}
      
      e.g. 
      
      $ME index.html output.pdf 
    
      Note: Requires Calibre be installed. more info here: https://ebooks.stackexchange.com/a/6285
    EOF
      )
      echo "$IT"
      exit
    }
    
    if [ "$1" == "help" ]
    then
      show_help
    fi
    if [ "$1" == "--help" ]
    then
      show_help
    fi
    
    /Applications/calibre.app/Contents/MacOS/ebook-convert $1 $2 --max-levels=1
    
    0 讨论(0)
提交回复
热议问题