Selenium Chrome driver Print to PDF, can't change the paper size with MediaSize's microns

Viewed 312

I'm trying to save web page design into pdf file. I've searched the solution and they all saying about dealing MediaSize.

I want to save it as "legal" size, and it is 355600 height microns, and 215900 width microns. So this is the code I used:

chrome_options = webdriver.ChromeOptions()

settings = {
    "recentDestinations": [{
        "id": "Save as PDF",
        "origin": "local",
        "account": ""
    }],
    "selectedDestinationId": "Save as PDF",
    "version": 2,
    "mediaSize": {
        "height_microns": 355600,
        "width_microns": 215900,
        "name": "NA_legal",
        "custom_display_name": "legal"
    },
    "isCssBackgroundEnabled": True,
    "isHeaderFooterEnabled": True,
}


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

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

driver = webdriver.Chrome("chromedriver.exe", options=chrome_options)
driver.get('page')
driver.execute_script('window.print();')

And I compared it with the pdf which I made by manual. It has different height(and maybe width as well).

I changed the height_microns and width_microns dramatically, and found out it isn't affected at all.

Did I miss something? Or was it deprecated somehow? Thank you in advance!

Edit: I checked the spec and I think it somehow got recognized as unavailable value, and get width_microns: 215900, height_microns: 279400. But not sure why is that so... (https://github.com/chromium/chromium/blob/eadef3f685cd9e96e94fcb9645b6838b6d0907a8/chrome/browser/resources/print_preview/data/model.js)

0 Answers
Related