1

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

1 Answers1

0

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  
  • 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 – hndcrftd Jul 28 '20 at 15:58
  • @hndcrftd any references to the `no longer maintained` part? Thanks! – Kolyunya Sep 10 '20 at 10:42
  • @Kolyunya Sure. The official website, first line under the header https://phantomjs.org/ – hndcrftd Sep 13 '20 at 04: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. – Kolyunya Sep 13 '20 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. – hndcrftd Sep 14 '20 at 18:56