16

In 2016, the best way to convert HTML files to PDF from the command line was using wkhtmltopdf. Unfortunately, it seems that that is not really maintained anymore. It doesn't support a lot of things like flexbox.

One can use headless chrome/chromium to do it:

chrome --headless --print-to-pdf="path/to/pdf" https://your_url

but that has no options such as margins, paper type, control over header/footer, screen size, etc.

It appears that there is no plan to add those into headless chrome as command line options (one needs to use the dev tools interface): https://bugs.chromium.org/p/chromium/issues/detail?id=603559#c89

How can one convert HTML files to PDF from the command line that gives control over how the document prints (margins, etc., from above), and supports modern html/css? Of course, once one can convert from the command line, you can also convert using a programming language of your choice.

juacala
  • 2,155
  • 1
  • 21
  • 22

3 Answers3

12

Here is a command line tool that you can use to convert HTML pages to PDF just as they would be in chrome. Once you have it installed, you can use it with whatever programming language you want (Python, Java, PHP, etc.) to automatically generate PDFs from HTML web pages or documents. All of the dependencies should be well maintained well into the future, so it shouldn't have the same issues as things like wkhtmltopdf that were difficult to maintain.

URLs:

https://www.npmjs.com/package/chromehtml2pdf

https://github.com/dataverity/chromehtml2pdf

To install it, you'll need npm, and type:

npm install chromehtml2pdf

or to make it globally available to all users on the system

npm install -g chromehtml2pdf

Command line usage:

chromehtml2pdf --out=file.pdf --landscape=1 https://www.npmjs.com/package/chromehtml2pdf

For help (to view all possible options), type:

chromehtml2pdf --help

Feel free to make pull requests on github.

juacala
  • 2,155
  • 1
  • 21
  • 22
  • `UNMET PEER DEPENDENCY webpack@3.6.0` and `Not compatible with your operating system or architecture: fsevents@1.2.4` That's on Ubuntu 18.04 – Stewart Dec 14 '18 at 21:16
  • $ chromehtml2pdf --help /home/miguelfg/.nvm/versions/node/v6.4.0/lib/node_modules/chromehtml2pdf/index.js:144 (async () => { ^ SyntaxError: Unexpected token (... – miguelfg Feb 11 '19 at 11:54
  • I've updated the dependencies. You can give it a try now; if you're still having issues, you can report it on github. – juacala Mar 29 '19 at 22:46
  • Hi @juacala, I'm using your app with a problem of click-to-expand contents [here](https://stackoverflow.com/questions/61900799/how-to-print-a-webpage-with-all-click-to-expand-contents). Could you please leave me some suggestions on this issue? Thank you so much for your help! – Akira May 19 '20 at 21:21
1

If all you wanted to do was get rid of Chrome's default header/footer, and you control the page in question, you can do that with CSS, without having to use something more complex than a simple command line call.

@media print {
  @page { margin: 0; }
}

Of course, you probably do want margins on your pages, so you'll need to fake those. The complexity of doing so varies depending on how many pages you meant to emit to PDF. The recommended body margins in the linked Answer will work if you're emitting a 1-pager. If not, you'll need to use other methods. For example, for multiple pages, you can add body padding on the left and right, then wrap each page's content in a tag with margin for top and bottom.

https://stackoverflow.com/a/15150779/176877

Chris Moschini
  • 36,764
  • 19
  • 160
  • 190
1

Project Gotenberg does this and a bit more. Including margin manipulation as well as WebHooks, timeouts, merging, and other formats.

To try it

docker run --rm -p 3000:3000 thecodingmachine/gotenberg:6

Example

curl --request POST \
    --url http://localhost:3000/convert/url \
    --header 'Content-Type: multipart/form-data' \
    --form remoteURL=https://brave.com \
    --form marginTop=0 \
    --form marginBottom=0 \
    --form marginLeft=0 \
    --form marginRight=0 \
    -o result.pdf

- HTML and Markdown conversions using Google Chrome headless

Alex Nolasco
  • 18,750
  • 9
  • 86
  • 81
  • You seem to know Gotenberg well. Do you know if it can create fillable form inputs that can then be filled in and saved using Adobe Reader? – Hashim Aziz Jun 05 '20 at 19:23
  • 1
    @Prometheus I don't think it will turn your HTML forms into Form Data Format. See issue https://github.com/thecodingmachine/gotenberg/issues/198 – Alex Nolasco Jun 05 '20 at 20:00
  • Thanks for that, no idea why my search didn't pick that up. – Hashim Aziz Jun 05 '20 at 20:03