1

I have a working script using Python, Selenium, and the Chrome webdriver to save webpages as PDFs. However, I need to save them on legal sized documents (216 x 356 mm), while my current script only saves files in letter size (216 x 279 mm).

Here's the code that I currently have:

    # Attach printing options to webdriver
    app_state = {
       "recentDestinations": [
            {
                "id": "Save as PDF",
                "origin": "local",
                "account": ""
            }
        ],
        "selectedDestinationId": "Save as PDF",
        "isCssBackgroundEnabled": True,
        "isHeaderFooterEnabled": False,
        "isLandscapeEnabled": True,
        "version": 2
    }
    prefs = {
        'printing.print_preview_sticky_settings.appState': json.dumps(app_state)
    }
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_experimental_option('prefs', prefs)
    chrome_options.add_argument('--kiosk-printing')
    driver = webdriver.Chrome(options=chrome_options)

Is there a way to save documents using legal size (or change the paper size in any way)?

I've been searching for other prefs and options to change the paper setting and/or dimensions, but haven't had any luck at all.

Thanks!

  • 1
    Does this answer your question? [Selenium Webdriver: How to Download a PDF File with Python?](https://stackoverflow.com/questions/43149534/selenium-webdriver-how-to-download-a-pdf-file-with-python) – Karthik Sep 03 '20 at 14:57
  • Unfortunately not, it doesn't mention how to change the paper size. – user14215745 Sep 03 '20 at 15:00

1 Answers1

0

if you add

"mediaSize": {"height_microns": 355600, "width_microns": 215900}

in the appState dictionary, you should get the paper size set to legal.

If you want to change to any other size (that is in the list) you can google the dimensions and convert to microns, or inspect the dropdown that allows you to choose the paper-size, and then search for your desired size in the options, and copy/paste the values.

For this solution to work, the values have to match exactly the ones in the dropdown, otherwise it won't select and default to Letter.