I am trying to Save as pdf using the print dialog, a web page where I have already logged on using Selenium and Headless Chrome (v81). All the articles state I should be able to print/save the document as pdf using kiosk mode where the print to pdf happens automatically so the preview dialog is surpressed e.g.
I cannot get Chrome to default to saving as a PDF when using Selenium Selenium Chrome save as pdf change download folder
So far I have the following code:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("--headless", "--disable-infobars", "--disable-extensions", "--test-type", "--allow-insecure-localhost", "--ignore-certificate-errors", "--ignore-ssl-errors=yes", "--disable-gpu", "--kiosk-printing", "--allow-running-insecure-content");
chromeOptions.AcceptInsecureCertificates = acceptInsecureCertificates;
chromeOptions.UnhandledPromptBehavior = UnhandledPromptBehavior.Ignore;
chromeOptions.AddUserProfilePreference("print.always_print_silent", true);
chromeOptions.AddUserProfilePreference("download.default_directory", @"C:\Temp");
chromeOptions.AddUserProfilePreference("savefile.default_directory", @"C:\Temp");
chromeOptions.AddUserProfilePreference("download.prompt_for_download", false);
chromeOptions.AddUserProfilePreference("printing.default_destination_selection_rules", "{\"kind\": \"local\", \"namePattern\": \"Save as PDF\"}");
chromeOptions.AddUserProfilePreference("printing.print_preview_sticky_settings.appState", "{\"recentDestinations\": [{\"id\": \"Save as PDF\", \"origin\": \"local\", \"account\": \"\" }],\"version\":2,\"isGcpPromoDismissed\":false,\"selectedDestinationId\":\"Save as PDF\"}");
webDriver = new ChromeDriver(ChromeDriverService.CreateDefaultService(), chromeOptions, TimeSpan.FromMinutes(timeoutMinutes));
Then I print the page using this:
var driver = FeatureContext.Current.GetWebDriver();
driver.ExecuteJavaScript("window.print();");
But I get the print preview dialog, always and my default printer is set and not Save as PDF.
I would use the chrome command line however for the fact I need to be logged on to the site. What is wrong with the code above?