80

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,527

7 Answers7

78

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
  • 964
26

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"

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
3

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
  • 958
  • 11
  • 15
3

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
  • 3,134
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
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