GCuser99 / SeleniumVBA

A comprehensive Selenium wrapper for browser automation developed for MS Office VBA running in Windows

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to get kiosk printing to work for Chrome/Edge?

GCuser99 opened this issue · comments

commented

Can't get Edge or Chrome to print to a pdf file via kiosk printing [sound of head banging against wall]...

Sub test_kiosk_printing()
    Dim driver As SeleniumVBA.WebDriver
    Dim caps As SeleniumVBA.WebCapabilities
    Dim appState As String
    
    Set driver = SeleniumVBA.New_WebDriver
    
    driver.StartEdge
    
    Set caps = driver.CreateCapabilities
    
    caps.AddArguments "--kiosk-printing"

    appState = "{'recentDestinations': [{'id': 'Save as PDF', 'origin': 'local', 'account': ''}], 'selectedDestination': 'Save as PDF', 'version': 2}"
    
    caps.SetPreference "printing.print_preview_sticky_settings.appState", appState
    
    Debug.Print caps.ToJson 'qc in intermediate window

    driver.OpenBrowser caps
    
    driver.NavigateTo "https://www.google.com/"
    
    driver.Wait 1000

    'this is printing only to my windows default printer, not to a pdf file in Downloads folder
    driver.ExecuteScript ("window.print();")
    
    driver.Wait 2000
    
    driver.CloseBrowser
    driver.Shutdown
End Sub

Notes: using Edge Browser Version 104.0.1293.70

commented

oops - escape character issue - will add an appropriate example in test_Capabilities module...

Sub test_kiosk_printing()
    'this test uses kiosk printing to save a webpage to pdf file (Chrome/Edge only)
    Dim driver As SeleniumVBA.WebDriver
    Dim caps As SeleniumVBA.WebCapabilities
    
    Set driver = SeleniumVBA.New_WebDriver
    
    driver.StartEdge
    
    Set caps = driver.CreateCapabilities
    
    caps.AddArguments "--kiosk-printing"
    
    Dim settings As New Dictionary
    settings.Add "appState", "{""recentDestinations"": [{""id"": ""Save as PDF"", ""origin"": ""local"", ""account"": """"}], ""selectedDestination"": ""Save as PDF"", ""version"": 2}"
    caps.SetPreference "printing.print_preview_sticky_settings", settings 
    
    'another way that works too - without the intermediate dictionary
    'Dim settings As String
    'settings = "{'appState': '{""recentDestinations"": [{""id"": ""Save as PDF"", ""origin"": ""local"", ""account"": """"}], ""selectedDestination"": ""Save as PDF"", ""version"": 2}'}"
    'caps.SetPreference "printing.print_preview_sticky_settings", settings
    
    caps.SetPreference "printing.print_header_footer", False
    caps.SetPreference "savefile.default_directory", ".\"
    
    Debug.Print caps.ToJson

    driver.OpenBrowser caps:=caps
    
    driver.NavigateTo "https://www.google.com/"
    
    driver.Wait 1000
    
    driver.DeleteFiles ".\" & driver.GetTitle & ".pdf"

    'now print the page
    driver.ExecuteScript ("window.print();")
    
    driver.Wait 2000
    
    driver.CloseBrowser
    driver.Shutdown
End Sub