According to the docs, Chrome can be started in headless mode with --print-to-pdf
in order to export a PDF of a web page. This works well for pages accessible with a GET
request.
Trying to find a print-to-pdf solution that would allow me to export a PDF after executing multiple navigation request from within Chrome. Example: open google.com
, input a search query, click the first result link, export to PDF.
Looking at the [very limited amount of available] docs and samples, I failed to find a way to instruct Chrome to export a PDF, after a page loads. I'm using the Java chrome-driver
.
One possible solution not involving Chrome, is by using a tool like wkhtmltopdf. Going on this path would force me to - before sending the HTML to the tool - do the following:
Don't prefer this path as it would require a lot of tinkering [I assume] on my part to get downloads' file paths correct for wkhtmltopdf
to read correctly.
Is there a way to instruct Chrome to print to PDF, but only after a page loads?
Type chrome://print in the Address Bar 2. Click on the Wrench icon and then click on Print. 3. Right click on the webpage that you want to print to PDF and click on Print. You will get a screen similar to this:
After opening the web page, you want your iPad Safari print to pdf or Chrome iPad print to pdf; the next step is to choose PDF format. Tap on the Share icon and choose "Create PDF." Step 3. Print to PDF
We can save a pdf file on Chrome using the Selenium webdriver. To download the pdf file in a specific location we have to take the help of the Options class. We shall create an object of this class and apply add_experimental_option on it.
Trying to find a print-to-pdf solution that would allow me to export a PDF after executing multiple navigation request from within Chrome. Example: open google.com, input a search query, click the first result link, export to PDF.
This is indeed possible to do through Selenium Chromedriver, by means of the ExecuteChromeCommandWithResult
method. When executing the command Page.printToPDF
, a base-64-encoded PDF document is returned in the "data" item of the result dictionary.
A C# example, which should be easy to translate into Java, is available in this answer:
https://stackoverflow.com/a/58698226/2416627
Here is another C# example, which illustrates some useful options:
public static void Main(string[] args)
{
var driverOptions = new ChromeOptions();
// In headless mode, PDF writing is enabled by default (tested with driver major version 85)
driverOptions.AddArgument("headless");
using (var driver = new ChromeDriver(driverOptions))
{
driver.Navigate().GoToUrl("https://stackoverflow.com/questions");
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(d => d.FindElements(By.CssSelector("#questions")).Count == 1);
// Output a PDF of the first page in A4 size at 90% scale
var printOptions = new Dictionary<string, object>
{
{ "paperWidth", 210 / 25.4 },
{ "paperHeight", 297 / 25.4 },
{ "scale", 0.9 },
{ "pageRanges", "1" }
};
var printOutput = driver.ExecuteChromeCommandWithResult("Page.printToPDF", printOptions) as Dictionary<string, object>;
var pdf = Convert.FromBase64String(printOutput["data"] as string);
File.WriteAllBytes("stackoverflow-page-1.pdf", pdf);
}
}
The options available for the Page.printToPDF
call are documented here:
https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF
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