Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NODRIVER] How to print to pdf in print preview? #2112

Open
argovaerts opened this issue Dec 30, 2024 · 2 comments
Open

[NODRIVER] How to print to pdf in print preview? #2112

argovaerts opened this issue Dec 30, 2024 · 2 comments

Comments

@argovaerts
Copy link

I'm trying to automate the download of a quarterly PDF. The built-in printing function of the webapp opens an HTML view in a new window and goes to print preview (print() function).

Is there a way to automate "save to PDF"?

@MuriloCSeidenstucker
Copy link

Hi! I had a similar experience trying to automate printing web pages as PDF. In my case, I used Selenium with the standard ChromeDriver. Maybe this can help you solve your problem.

Chrome Options Configuration to Save as PDF

I used Chrome options to configure the printing behavior. Specifically, I set the preferences to make "Save as PDF" the default destination and enabled automatic printing. Here's an example:

from time import sleep
from selenium import webdriver
import json

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

This configuration allows Chrome to skip the "Print Preview" interface and save directly as a PDF.

Script to Print the Page

After configuring ChromeDriver with these options, I used the following command in Selenium to trigger the page print:

driver.execute_script('window.print();')
sleep(0.5)  # Added a slight delay to ensure the print action completes.

Notes

  • Delay: The sleep(0.5) is essential because printing might take a few milliseconds to process. Without it, the script might proceed before the action is completed.
  • Default destination: I use Windows 10, and the PDF is always saved in the system's default Downloads folder. So far, I haven't learned how to change the location where the PDF is automatically saved.
  • Limitations: This approach worked well for me because I knew the print destination would always be "Save as PDF." If the default print destination varies, you may need to adjust it manually.
  • Compatibility with undetected-chromedriver: I haven't tested this with undetected-chromedriver, but the Chrome preferences configuration should be similar since both tools use ChromeDriver under the hood.

I hope this helps or gives you some ideas to solve your issue! Let me know if you need more details. 😊

@MuriloCSeidenstucker
Copy link

Hello again! I tested the method mentioned earlier using undetected-chromedriver and successfully made it work.

When using undetected-chromedriver, the only change required was replacing the creation of Chrome options. I substituted:

chrome_options = webdriver.ChromeOptions()

with:

chrome_options = uc.ChromeOptions()

This change is essential because undetected-chromedriver uses its own implementation for handling browser options, compatible with detection bypass.

Note on the detach Option.
It’s worth mentioning that the following option, used in standard ChromeDriver to keep the browser open after execution, does not work with undetected-chromedriver:

chrome_options.add_experimental_option('detach', True)

If you are using this setting, you’ll need to remove it when switching to undetected-chromedriver.
I hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants