Additional options to print to pdf using chrome headless options

I am using the command line in Windows to print a PDF using Google Chrome with the headless options Print to PDF. I want to know how can I use the other options available as the margins and pages size or even orientation. I notice the options are available in https://chromedevtools.github.io/devtools-protocol/tot/Page#method-printToPDF but base on this question, it seems doesnt work How can I print a webpage in landscape mode using Headless-Chromium on the command line in Linux? Has anyone use any of the options available and what is the correct sintaxis as the code below generates the pdf but ignores page size?

chrome.exe --headless --disable-gpu --print-to-pdf=C:\\Spotfire_Export\\'+filename+'.pdf --paperWidth=15 '+tempFolder+filename+'.html 
asked Jul 5, 2019 at 17:00 Jorge Madrigal Jorge Madrigal 11 1 1 silver badge 2 2 bronze badges

2 Answers 2

I think it is not possible through command line. But using Puppeteer (NodeJS library) you can do more sophisticated things like this:

const puppeteer = require('puppeteer'); (async() => < const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://www.chromestatus.com', ); await page.pdf(); await browser.close(); >)(); 

FYI there is an executable tool called PhantomJS that you can setup (page size, width, height, header, footer, etc.) through JavaScript files, example:

C:\phantomjs-2.1.1-windows\bin>phantomjs.exe ../examples/rasterize.js https://www.google.com sample.pdf 
answered Aug 29, 2019 at 22:56 Luis Hernandez Luis Hernandez 101 1 1 bronze badge

PhantomJS is no longer maintained and does have an issue with different DPI rendering between Windows and Linux, so for most that has been the driving force to switch to headless chrome

Commented Jul 28, 2020 at 15:58 @hndcrftd any references to the no longer maintained part? Thanks! Commented Sep 10, 2020 at 10:42 @Kolyunya Sure. The official website, first line under the header phantomjs.org Commented Sep 13, 2020 at 4:27

@hndcrftd thanks! Got confused by the fact that they keep on contributing to the official repo, which is kind of weird for a project which is not maintained any more.

Commented Sep 13, 2020 at 10:00

@Kolyunya I saw that too. One thing to note is that it's a different developer, not the original author. It's entirely possible that in the future they'll get the project back on track, but so far they are not planning to produce "stable" releases, the 2.5 branch is a dev branch. We'll see I guess.

Commented Sep 14, 2020 at 18:56

Time changes abilities so modern Windows includes Chromium based MS Edge without needing to install a duplicate Chrome suite. And over time many switches have been updated or changed. So disabling the GPU was not required after 2018. Also "New" is now the default so not needed either, but header and footer margin controls have also changed.

"c:\ path to \MSedge.exe" --headless --no-pdf-header-footer --print-to-pdf="C:\ path \filename.pdf" "drive:\Folder\filename.html" 

Note: There cannot (in the command line) be any attempt to alter the internal Media/CSS to Landscape it must be done by edits inside the HTML.

However, additional switches may help with timing so one example is --run-all-compositor-stages-before-draw

For my example of HTML edit, to switch the content to landscape, we can add in the HTML head style, for A4 Landscape:

    

enter image description here

Taking this SuperUser page at default it will be "portrait".

However by adding my style over-ride inside the HTML it will become "landscape". (NOTE: There were oddly 3 x entries so I simply added that find and replace line a total of 3 times for overkill).

enter image description here

"%programfiles%\Microsoft\Edge\Application\msedge.exe" --headless --no-pdf-header-footer --print-to-pdf="%cd%\landscape.pdf" "%cd%\command line headless options - Super User.html"