Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ChromeDriver --print-to-pdf after page load

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:

  • save the HTML in a local file
  • traverse the DOM, and download all file links (images, js, css, etc)

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?

like image 756
jankovd Avatar asked Nov 20 '17 08:11

jankovd


People also ask

How to print a webpage to PDF in chrome?

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:

How to print to PDF on iPad Safari or chrome?

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

How to save a PDF file on Chrome using Selenium WebDriver?

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.

How to export a PDF after multiple navigation requests from chrome?

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.


1 Answers

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

like image 168
Otto G Avatar answered Sep 20 '22 13:09

Otto G