Automate print/save web page as pdf in chrome - python 2.7

16,286

Solution 1

This worked for me using Chrome 62.0.3202.94, ChromeDriver 2.33.506120, Selenium 3.4.3, and Python 2.7.14 or 3.6.3, on Windows 7 x64:

import json
from selenium import webdriver

appState = {
    "recentDestinations": [
        {
            "id": "Save as PDF",
            "origin": "local",
            "account": ""
        }
    ],
    "selectedDestinationId": "Save as PDF",
    "version": 2
}

profile = {'printing.print_preview_sticky_settings.appState': json.dumps(appState)}

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('prefs', profile)
chrome_options.add_argument('--kiosk-printing')

driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://www.google.com/')
driver.execute_script('window.print();')

Solution 2

I was able to find a possible solution.

The code saves an html file to pdf which is my ultimate goal.

The original post is:

Python + Selenium + PhantomJS render to PDF

Best.

Share:
16,286

Related videos on Youtube

Diego
Author by

Diego

Updated on September 15, 2022

Comments

  • Diego
    Diego about 1 year

    I am trying to automate Print Save Web Page as pdf in chrome.

    I have checked the webbrowser module, but it does not seem to be intended for this purpose.

    I explored wkhtmltopdf as an alternative but when downloading the file it seems to be infected by a virus.

    Thank you for the suggestions.

  • Diego
    Diego over 8 years
    @MTuner, I would like to ask you about how to alter the line render = '''this.render("test.pdf")'''so I can render several pages using a variable name (list) and a counter and saving the file names accordingly.
  • Martin Thoma
    Martin Thoma about 6 years
    Please write something useful - besides the link - in your answer. So that if the link breaks the answer is not useless.
  • TTT
    TTT almost 6 years
    Nice workflow. Can you specify the path and rename created PDF?
  • Ross Smith II
    Ross Smith II almost 6 years
    @tao.hong: no, I find the newest PDF in the download directory, and move/rename it.
  • jhenya-d
    jhenya-d over 5 years
    excellent solution, works fine for me. For java put in preference like this: preferences.put("printing.print_preview_sticky_set‌​tings.appState", "{\"recentDestinations\":{\"id\":\"Save as PDF\",\"origin\":\"local\"},\"selectedDestinationId\":\"Save as PDF\",\"version\":2}");
  • Gokul
    Gokul over 5 years
    The same script doesn't work in headless mode. Any idea why?
  • Ross Smith II
    Ross Smith II over 5 years
    @Gokul No, sorry. I didn’t test headless mode.
  • robertspierre
    robertspierre over 5 years
    I used the same script in Opera, but it shows up the "Save as" dialog, asking for the filename of the resulting PDF file. Any idea on how to get rid of that?
  • Ross Smith II
    Ross Smith II over 5 years
    @raffamaiden Sorry, no idea. I haven't played with Opera.
  • Franck Freiburger
    Franck Freiburger almost 5 years
  • jahmed31
    jahmed31 about 4 years
    @Gokul any update for headless mode, i am also stuck at this point.
  • TAH
    TAH almost 4 years
    Using the latest version of Chrome (78.0.3904.108), need to add "account": "" to "recentDestinations".
  • Ross Smith II
    Ross Smith II almost 4 years
    @TAH Thanks for the heads up! I updated the answer.
  • Dharmendra Singh Negi
    Dharmendra Singh Negi about 2 years
    any help on unix, it's not working with it.