Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

additional options in Chrome headless print-to-pdf

I need help one more time. I am trying to print a page to pdf using headless feature of the chrome. However, header and footer is present in the pdf. I found that this option as been implemented in Devtools.

https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF

However, i can't find how can i use these options in CLI. Also is it possible to invoke the Devtools from selenium?

Additionally how can i invoke Page.PrintToPDF in Dev tools. I tried to run the command in Console. It is showing Page is undefined.

like image 359
user2580925 Avatar asked Sep 06 '17 14:09

user2580925


People also ask

What is Chrome option headless?

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.

How do I print to PDF in Chrome?

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. You can also use Chrome's print-to-PDF function to convert to .

Can not print PDF in Chrome?

1. Open the Chrome browser and type (or copy and paste) this url: chrome://settings/content /pdfDocuments. 2. Slide the button option to the right to enable Download of PDF files instead of automatically opening them in Chrome.


2 Answers

Add this CSS to the page your creating into a PDF to remove Chrome Headless's implemented Header and Footer.

CSS:

@media print {   @page { margin: 0; }   body { margin: 1.6cm; } } 

You should format your command like below to create the PDF:

"C:\PATH\TO\CHROME\EXECUTABLE\FILE", "--headless","--disable-gpu","--print-to-pdf=" + directory path to where you want the file to go followed by the desired file name/fileName.pdf,"--no-margins", "the path to the file you want turned into a pdf" 

Example 1:

C:\chrome-win/chrome --headless --disable-gpu --print-to-pdf=C:\user\fileName.pdf --no-margins C:\Projects\index.html 

Example 2:

You can also test this functionality by navigating in your command line to the folder containing Chrome executable file, and running this command:

chrome --headless --disable-gpu --print-to-pdf https://www.chromestatus.com/ 
like image 55
nuccio Avatar answered Oct 03 '22 13:10

nuccio


"/path/to/google-chrome" : This is the path of Google Chrome.

'--headless' : Chrome browser in a headless environment without the full browser UI

'--run-all-compositor-stages-before-draw' : It Prevents the Pdf Creation Before all the data is rendered(After all data is rendered the pdf will be created).

'--virtual-time-budget = x: It Delays the Process of creation of Pdf, here x will be the miliseconds.

'--print-to-pdf' : This Flag creates the pdf of the given Url.

URL : The url of webpage.

PDF Page Formatting (Using CSS) Adding this(to css files):

 @media print {             @page { margin: 0mm 0mm 0mm 0mm;             size:8in 9.5in;             }             } 

The Above CSS code has no effect on the Webpage Rendering,But effect on the formatting of page in PDF only.

like image 41
Ayaz Ur Rashid Avatar answered Oct 03 '22 13:10

Ayaz Ur Rashid