Tkinter Lover
Tkinter Lover

Reputation: 855

Print webpage in selenium

When I do ctrl P, it gives me a window to choose the file location, printing properties, etc. How do I make it so that I use Selenium to go to a webpage, make the printing properties a colored PDF, and save it in the same directory as where I have the Python file? Thank you for your help!

~helloworld

Upvotes: 0

Views: 1805

Answers (1)

PDHide
PDHide

Reputation: 19949

There are two ways to do it:

  1. Use headless mode and use Page.printToPDF:
import json
from base64 import b64decode
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

from datetime import datetime
    
options = webdriver.ChromeOptions()
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)
driver.get("https://emicalculator.net/")

a = driver.find_element_by_css_selector("#loanamountslider")
webdriver.ActionChains(driver).click(
    a).click_and_hold().move_by_offset(0, 0).perform()

a = driver.execute_cdp_cmd(
    "Page.printToPDF", {"path": 'html-page.pdf', "format": 'A4'})
print(a)
# Import only b64decode function from the base64 module

# Define the Base64 string of the PDF file
b64 = a['data']

# Decode the Base64 string, making sure that it contains only valid characters
bytes = b64decode(b64, validate=True)

# Perform a basic validation to make sure that the result is a valid PDF file
# Be aware! The magic number (file signature) is not 100% reliable solution to validate PDF files
# Moreover, if you get Base64 from an untrusted source, you must sanitize the PDF contents
if bytes[0:4] != b'%PDF':
    raise ValueError('Missing the PDF file signature')

# Write the PDF contents to a local file
f = open('file.pdf', 'wb')
f.write(bytes)
f.close()
  1. For non headless call window.print():
options = webdriver.ChromeOptions()
options.headless = False

options.add_argument("--kiosk-printing")
options.add_argument("--kiosk")

settings = {
    "recentDestinations": [{
        "id": "Save as PDF",
        "origin": "local",
        "account": "",
        # This is the folder where i want to place my PDF (in the same directory as this
        'default_directory': r'C:\Users\prave\Downloads\travelBA\folder'
        # file)
    }],
    "selectedDestinationId": "Save as PDF",
    "version": 2,
}

prefs = {
    'printing.print_preview_sticky_settings.appState': json.dumps(settings), 
    #"savefile.default_directory": "C:\Users\prave\Downloads\travelBA\folder",
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "download.safebrowsing.enabled": True
}
options.add_experimental_option('prefs', prefs)

driver = webdriver.Chrome(executable_path="chromedriver.exe", options=options)

driver.get("https://google.com")

# This gets saved in my downloads folder
driver.execute_script("window.print();")

input()

Upvotes: 3

Related Questions