I am using headless Chrome to export html documents to pdf
google-chrome --headless --disable-gpu --print-to-pdf='output_path' 'url'
How can I change paper size in generated pdf?
I have control on both Chrome parameters and html.
I always get US Letter.
There are no documented command-line options for this.
I've tried setting CSS: @page {size: A4;}
. No effect in headless mode, but works when I hit Ctrl+P in normal mode (option to choose paper size for Save as pdf
disappears, exported pdf has A4 page size).
I've tried this on Chrome versions 59, 60 and 61 on Ubuntu 16.04.
Step 1: Click the three dots on the upper right corner of your Google Chrome browser to expand the More Options list. Step 2: Select Print. Step 3: Click on More Settings. Step 4: Select the correct paper size from the dropdown.
1. In Chrome's Print window, expand the More Settings section. 2. Select the desired paper size, such as '4 in x 6 in'.
Open a webpage in Chrome, press Ctrl+P to open the Print dialog and change the destination printer to “Save as PDF”. Hit the “Print” button and the webpage will download as a PDF document.
The Headless mode is a feature which allows the execution of a full version of the Chrome Browser. It provides the ability to control Chrome via external programs. The headless mode can run on servers without the need for dedicated display or graphics.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With