automation - How to Print to Save as PDF from a command line with Chrome or Chromium - Super User
automation - How to Print to Save as PDF from a command line with Chrome or Chromium - Super User
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.
78
google-chrome automation chromium print-to-pdf
@golimar It is not a virtual printer. Chrome has a built-in option to export to pdf. – ipavlic May 8,
2013 at 19:23
1 I don't see any built-in Chrome switches for saving as PDF. – Karan May 10, 2013 at 1: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: support.google.com/chrome/bin/… – ipavlic
May 10, 2013 at 8: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, 2013 at
14:36
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.
4
https://superuser.com/questions/592974/how-to-print-to-save-as-pdf-from-a-command-line-with-chrome-or-chromium 1/6
05/12/2024 09:21 automation - How to Print to Save as PDF from a command line with Chrome or Chromium - Super User
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.
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 Sep 18,
2015 at 10:41
64 -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, 2016 at 0:25
11 can't recommend it either, if you have sophisticated CSS wkhtmltopdf is useless. – MushyPeas Mar 18,
2018 at 19:24
3 SVGs are not rendered. – Marco Sulla Mar 31, 2019 at 13:42
2 -1 wkhtmltopdf is severely lacking for CSS3 documents – arielnmz Mar 18, 2020 at 4:58
With that, we can create a pdf. e.g. for windows navigate your commandline to
78
C:\Users\{{your_username}}\AppData\Local\Google\Chrome SxS\Application>
https://superuser.com/questions/592974/how-to-print-to-save-as-pdf-from-a-command-line-with-chrome-or-chromium 2/6
05/12/2024 09:21 automation - How to Print to Save as PDF from a command line with Chrome or Chromium - Super User
Share Improve this answer Follow answered May 22, 2017 at 11:32
Vikas
964 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 8, 2018 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, 2018 at 18:50
@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, 2018 at 7:39
SVGs are rendered incorrectly – Marco Sulla Mar 31, 2019 at 13:42
You must be using Google Chrome / Chromium 59 or later version & it’s only available for
MAC OS and Linux users.
26
* Windows users still have to wait for some time till Version 60 *
Command :
Reference : https://developers.google.com/web/updates/2017/04/headless-chrome
EDIT : Google Chrome / Chromium 60 has been rolled out for windows users.
Share Improve this answer Follow edited Jan 4, 2019 at 14:25 answered Jul 13, 2017 at 6:50
https://superuser.com/questions/592974/how-to-print-to-save-as-pdf-from-a-command-line-with-chrome-or-chromium 3/6
05/12/2024 09:21 automation - How to Print to Save as PDF from a command line with Chrome or Chromium - Super User
Rahul_Dange
439 1 5 10
1 --disable-gpu should not be necessary anymore in most recent versions. – Déjà vu Sep 11, 2017 at
6:47
1 this creates a MUCH better/consistent output than wkhtmltopdf or weasyprint – ierdna Oct 12, 2017 at
14:53
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, 2019 at 14:19
Example usage:
https://superuser.com/questions/592974/how-to-print-to-save-as-pdf-from-a-command-line-with-chrome-or-chromium 4/6
05/12/2024 09:21 automation - How to Print to Save as PDF from a command line with Chrome or Chromium - Super User
Sam Watkins
928 10 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 3, 2021 at 12:21
1 @mbx, the switches are indeed documented. First, for the original/classic chrome headless, they're at
developer.chrome.com/blog/headless-chrome/…. For the "new" engine (since 2021, accessed with --
headless=new ), they're at developer.chrome.com/articles/new-headless/…. – charlie arehart Mar 25,
2023 at 18:19
Successfully did a batch conversion of local html files to PDF -- sharing the approach.
Note -- must use forward slash to avoid negating the %f in the file path.
Share Improve this answer Follow answered Jun 26, 2019 at 15:28
Zac
11 1
Usage#
}).ToPdf(new ChromeDetails()
{
ChromePath = chromePath,
HtmlPath = url,
DeleteOutputFile = true, //optional
https://superuser.com/questions/592974/how-to-print-to-save-as-pdf-from-a-command-line-with-chrome-or-chromium 5/6
05/12/2024 09:21 automation - How to Print to Save as PDF from a command line with Chrome or Chromium - Super User
// OutputPath = @"d:\print.pdf" // (add if
Environment.CurrentDirectory does not have access rights)
});
File.WriteAllBytes(@"d:\print.pdf", output.FileDetails.File);
https://superuser.com/questions/592974/how-to-print-to-save-as-pdf-from-a-command-line-with-chrome-or-chromium 6/6