Set Selenium ChromeDriver UserPreferences to Save as PDF
Asked Answered
G

1

4

I am using ChromeDriver 2.33 and am using kiosk printing to automatically click the Print button on the Print Preview dialog however it is sending the document to the printer instead of PDF.

I have attempted the solution at this stack overflow question to no avail.

Here is the code I am using:

ChromeOptions o = new ChromeOptions();
o.AddArgument("--kiosk-printing");
o.AddUserProfilePreference("printing.print_preview_sticky_settings.appState", "{\"version\":2,\"isGcpPromoDismissed\":false,\"selectedDestinationId\":\"Save as PDF\"");
chrome = new ChromeDriver(dir, o);

Can anyone tell me how I set the printer to PDF from the actual printer?

Garlinda answered 30/10, 2017 at 3:23 Comment(1)
I tried all kinds of stuff to get Selenium to work with "--kiosk-printing" but I couldn't get it to work. Instead I did this, which did work: #50167436Semiliquid
S
6

try adding Save as PDF on recentDestinations:

import json
settings = {
    "appState": {
        "recentDestinations": [{
            "id": "Save as PDF",
            "origin": "local"
        }],
        "selectedDestinationId": "Save as PDF",
        "version": 2
    }  
}
prefs = {'printing.print_preview_sticky_settings': json.dumps(settings)}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('--kiosk-printing')

driver = webdriver.Chrome(chrome_options=chrome_options)
Sporule answered 15/2, 2018 at 0:29 Comment(1)
Using the latest version of Chrome (78.0.3904.108), need to add "account": "" to "recentDestinations".Paulus

© 2022 - 2024 — McMap. All rights reserved.