68

Can I print to destination "Save as PDF" from a command line with Chrome or Chromium? I'd like to be able to automatically convert html files to PDF with Chrome's built-in functionality.

ipavlic
  • 1,397
  • 1
  • 13
  • 19
  • @golimar It is not a virtual printer. Chrome has a built-in option to export to pdf. – ipavlic May 08 '13 at 19:23
  • 1
    I don't see any built-in Chrome switches for saving as PDF. – Karan May 10 '13 at 01:07
  • @Karan When you go to `Print` there should be a `Save to PDF` destination available for you to choose. It's also clearly stated on Google's support page: http://support.google.com/chrome/bin/answer.py?hl=en&answer=1379552 – ipavlic May 10 '13 at 08:56
  • 2
    Perhaps my previous comment wasn't clear. You wanted to know how to do this from the command line, and what I wanted to say was that Chrome/Chromium seem to have no command-line switches/params to do this, although I know you can do it from the UI. You'll need to find some way of triggering the Save As option, perhaps by sending mouse clicks or key strokes. – Karan May 10 '13 at 14:36

7 Answers7

69

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
Vikas
  • 874
  • 1
  • 7
  • 7
  • Just a heads up, if you have any existing instance of Chrome running on Windows the command won't work. Kill all Chrome processes first, then it will work. There might be a flag to work around this inconvenience. – John Leidegren Feb 08 '18 at 17:17
  • 2
    @JohnLeidegren (at least) as of Chrome for Windows Version 66.0.3359.139 (Official Build) (64-bit), this works without killing any Chrome processes. – naitsirhc May 16 '18 at 18:50
  • 7
    Note that you can use `--user-data-dir="C:\Users\...\AppData\Local\Google\Chrome\User Data` to run under your user's Chrome profile. This is useful, for example, for exporting content from a website that requires users to be logged in, since session cookies are available. – naitsirhc May 16 '18 at 18:54
  • @naitsirhc FYI, I've found puppeteer to be a really good alternative to the command line stuff, if you trying to do something more elaborate. It has a nice API to remote chromium to do various tasks, it also manages versions for you. Very nice. – John Leidegren May 17 '18 at 07:39
  • SVGs are rendered incorrectly – Marco Sulla Mar 31 '19 at 13:42
25

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"

Rahul_Dange
  • 429
  • 1
  • 5
  • 10
4

https://github.com/fraserxu/electron-pdf was designed exactly for this purpose.

The CLI looks like this: $ electron-pdf http://fraserxu.me ~/Desktop/fraserxu.pdf

Nate Good
  • 141
  • 1
  • I installed it, it hangs indefinitively without any output. I tried `electron-pdf http://fraserxu.me test.pdf` as suggested in the help, but I tried gooogle.com too – Marco Sulla Mar 31 '19 at 14:19
  • I opened issue https://github.com/electron/electron/issues/17631#issue-427405049 – Marco Sulla Mar 31 '19 at 14:59
  • See bugs https://github.com/electron/electron/issues/20634 https://github.com/electron/electron/issues/20634 - any app using Electron since version 6.1 (which you have to use to print untrusted HTML) will hang if page contains an ` – Mikko Rantalainen Dec 04 '20 at 18:22
4

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.

hanxue
  • 2,940
  • 4
  • 29
  • 53
  • Note from having tried this and getting subtle things missing or broken: wkhtmltopdf's source contains QtWebkit patches that enable important features such as clickable links. Your distribution's package is likely to be missing such features if it lists your distribution's usual qtwebkit package as a dependency. Installing wkhtmltopdf from source takes 3.7GiB of disk and some hours. – Anko - inactive in protest Sep 18 '15 at 10:41
  • 61
    -1 This answer does not correspond to the question, wkhtmltox is a great tool but it does not perform as well as chrome or firefox on exporting to PDF. – Carlos C Soto Jun 16 '16 at 00:25
  • @Anko Not sure I understood. What am I to expect by building wkhtmltopdf from source? – VH-NZZ Oct 12 '16 at 11:27
  • This isn't great for Javascript based websites. I've been trying it with https://stage-plan.com and it just doesn't work... Even raised bugs on github – Dave Jan 23 '18 at 11:39
  • 10
    can't recommend it either, if you have sophisticated CSS wkhtmltopdf is useless. – MushyPeas Mar 18 '18 at 19:24
  • 3
    SVGs are not rendered. – Marco Sulla Mar 31 '19 at 13:42
  • 1
    -1 wkhtmltopdf is severely lacking for CSS3 documents – arielnmz Mar 18 '20 at 04:58
  • https://blog.rebased.pl/2018/07/12/wkhtmltopdf-considered-harmful.html – Déjà vu Jun 05 '20 at 01:01
  • -1. I have hundreds of local HTML files that have Print stylesheets Chrome can handle. wkhtmltopdf takes almost 70 seconds per HTML file (unacceptable) and fails to render the page as it should with the proper Print stylesheet. This is an old answer, but even the updated version of this software is useless for this. – bryc Nov 15 '21 at 19:00
  • wkhtmltopdf is garbage. Doesn't print emoji, forgot about supporting chinese fonts... yes you can try to get one or the another but... it doesn't work. – Kamil Dziedzic Dec 07 '21 at 14:53
2

I wrote a little wrapper script for Chrome{,ium} headless, called html2pdf.
Ghostscript is in there to reduce the file size, and to select a range of pages:

#!/bin/sh -eu
in=$1 out=$2 page0=${3:-} page1=${4:-$page0}
${CHROME:-chromium} --headless --disable-gpu \
    --run-all-compositor-stages-before-draw --print-to-pdf-no-header \
    --print-to-pdf="$out" "$in"
GS_ARGS=
if [ -n "$page0" ]; then
        GS_ARGS="-dFirstPage=$page0 -dLastPage=$page1"
fi
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH $GS_ARGS -sOutputFile="$2.tmp" "$2"
mv "$2.tmp" "$2"

Example usage:

html2pdf https://ucm.dev/resume.html ucm.pdf 1
Sam Watkins
  • 859
  • 9
  • 15
  • 1
    Thanks for the `--print-to-pdf-no-header`, that's one parameter I still missed. Is there is list somewhere with the other parameters we know from the print dialog (like Margins, Scale, Paper size)? – mbx Jun 03 '21 at 12:21
  • 2
    From looking at [headless_shell_switches.cc](https://github.com/chromium/chromium/blob/master/headless/app/headless_shell_switches.cc) and [chrome_switches.cc](https://github.com/chromium/chromium/blob/master/chrome/common/chrome_switches.cc) it seems like those parameters aren't (yet?) mapped. For non-trivial scenraios [puppeteer](https://github.com/puppeteer/puppeteer) seems the way to go. – mbx Jun 03 '21 at 12:48
  • 1
    @mbx, the switches are indeed documented. First, for the original/classic chrome headless, they're at https://developer.chrome.com/blog/headless-chrome/#command-line-features. For the "new" engine (since 2021, accessed with `--headless=new`), they're at https://developer.chrome.com/articles/new-headless/#headless-specific-command-line-flags. – charlie arehart Mar 25 '23 at 18:19
1

Successfully did a batch conversion of local html files to PDF -- sharing the approach.

Navigate to a folder containing a batch of html files you want to convert...

for %f in (*.html) do (
start /wait chrome --headless --disable-gpu --print-to-pdf="C:/[DESTINATION FOLDER PATH]%f.pdf" "C:/[SOURCE HTML FILE FOLDER PATH -- ALSO CURRENT FOLDER]%f"
)

Note -- must use forward slash to avoid negating the %f in the file path.

Zac
  • 11
  • 1
0

Can use this simple library from nuget package

For .Net Framework https://www.nuget.org/packages/Sats.HTMLtoPdf

For Core 3.1 https://www.nuget.org/packages/Sats.Core.HTMLToPdf

Usage#

var url = @"d:\convert.html";
var chromePath = @"C:\Program Files\Google\Chrome\Application\chrome.exe";

var output = new ChromeOptions().AddOptions(b =>
                    {
                        b.Headless();
                        b.DisableGPU();
                        b.WithoutHeader();

                    }).ToPdf(new ChromeDetails()
                    {
                        ChromePath = chromePath,
                        HtmlPath = url,
                        DeleteOutputFile = true, //optional
                       // OutputPath = @"d:\print.pdf" // (add if Environment.CurrentDirectory does not have access rights)
                    });


   File.WriteAllBytes(@"d:\print.pdf", output.FileDetails.File);

For web application Set Process Model to LocalSystem

enter image description here

Satish Patil
  • 101
  • 1