Google-chrome – Additional options to print to pdf using chrome headless options

cmd.execommand linegoogle-chromepdf

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

Best Answer

Same question over here: https://stackoverflow.com/questions/44970113/how-can-i-change-paper-size-in-headless-chrome-print-to-pdf

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', {waitUntil: 'networkidle2'});
await page.pdf({path: 'page.pdf', format: 'A4'});

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  
Related Question