How can I change paper size in headless Chrome --print-to-pdf
You can run headless chrome from Node environment.
Then you would be able to pass additional parameters to printToPdf
function including pageWidth
and pageHeight
.
The page size could be set in inches/mm. I haven't tested with a size in pixels. Here is a set of CSS rules which did the trick for me:
@page { margin: 0; padding: 0; size: 5in 6.5in;}
My exact case is rendering svg-to-pdf, not html; For svg, you may also need to add width
and height
attributes to <svg>
tag:
<svg width="5in" height="6.5in" ...>
That's all! Output PDF won't have margins, and will preserve the desired size - 5"x6.5" in my case.
There was a patch created sometime ago that enables page sizing configuration https://codereview.chromium.org/2829973002/
It is now closed and available in the unstable version of chrome, so you can use it as you suggested @page { size: A4 }
.
I tested, it works on the unstable build I have installed (Google Chrome 61.0.3141.7 dev
). I am not sure, though, how to check when it will be available in the stable build ...