How to Print to Save as PDF from a command line with Chrome or Chromium

Instead of calling up an entire web-browser, why not use the HTML rendering engine only to do the work? Use wkhtmltopdf to perform the conversion.

wktohtml

You can also convert an existing website to PDF

$ wkhtmltopdf http://google.com google.pdf

Note: technically Google Chrome's rendering engine is Blink, which is a fork of Webkit. There is >90% of common code between Blink and Webkit, so you should get a similar result.


Chrome has started headless program.

With that, we can create a pdf. e.g. for windows navigate your commandline to

C:\Users\{{your_username}}\AppData\Local\Google\Chrome SxS\Application>

Then hit the command:

chrome --headless --print-to-pdf="d:\\{{path and file name}}.pdf" https://google.com

You must be using Google Chrome / Chromium 59 or later version & it’s only available for MAC OS and Linux users.

* Windows users still have to wait for some time till Version 60 *

Command :

$ google-chrome --headless --disable-gpu --print-to-pdf=file1.pdf http://www.example.com/

$ chromium-browser --headless --disable-gpu --print-to-pdf=file1.pdf http://www.example.com/

Reference : https://developers.google.com/web/updates/2017/04/headless-chrome

EDIT : Google Chrome / Chromium 60 has been rolled out for windows users.

Command usage in CMD :

C:\Program Files\Google\Chrome\Application> chrome.exe --headless --disable-gpu --print-to-pdf=file1.pdf http://www.example.com/

Your pdf file naming file1.pdf will be save in

"C:\Program Files or (x86)\Google\Chrome\Application\60.0.3112.113 (chrome-version)\file1.pdf"