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

2 Answers2

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  
phuclv
  • 30,396
  • 15
  • 136
  • 260
0

Time changes abilities so modern Windows includes Chromium based MS Edge without needing to install a duplicate Chrome suite.

UPDATE Autumn 2024

The headless ability has been changed by Chromium team to a separate executable "Chrome-Headless-Shell" this impacts both MSEdge and chrome users. Needing "migration" to headless shell for version 128 onwards!

For my suggestion as to how to download and run headless shell see

https://stackoverflow.com/a/76079607/10802527

OLD answer

Still relevant as to how to use Landscape inside HTML

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.

Basically use

"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:

<head>
<meta http-equiv="Content-Style-Type" content="text/css">
<style>@media print { @page { margin: 0; size: 842pt 595pt ; } body { margin: 0; } }</style>
</head>

Taking this SuperUser page at default it will be "portrait". enter image description here

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

"chrome-headless-shell.exe" --no-pdf-header-footer --print-to-pdf="%cd%\landscape.pdf"  "%cd%\command line headless options - Super User.html"

enter image description here

K J
  • 1,248