2

I have a script that converts html to pdf using selenium(Have tried all other methods of converting it but the html file is a bit complicated. So no other methods are working). It's working perfectly. But the problem is that it opens a new window every time I want to print the page. I want it to be opened minimized. If anyone knows how to do it, I would appreciate it.(printing pdf in headless mode doesn't works)

My code:

from selenium import webdriver

options = webdriver.ChromeOptions()
settings = {
    "recentDestinations": [{
        "id": "Save as PDF",
        "origin": "local",
        "account": ""
    }],
    "selectedDestinationId": "Save as PDF",
    "version": 2,
    "isHeaderFooterEnabled": False,
    "isCssBackgroundEnabled": True
}
prefs = {
    'printing.print_preview_sticky_settings.appState': json.dumps(settings),
    'savefile.default_directory': "./"
}
options.add_experimental_option('prefs', prefs)
options.add_argument('--kiosk-printing')
options.add_argument('--enable-print-browser')
options.add_argument("--disable-popup-blocking")
options.add_argument('--disable-extensions')
driver = webdriver.Chrome('chromedriver', options=options)
full_path = "file:///home/zubayer/test.html"
driver.get(full_path)
time.sleep(2)
driver.execute_script('window.print();')
driver.quit()

Example html file:

<!DOCTYPE html>
<html>
</head>
    <title>Test</test>
</head>
<body>
    <h1>This is a test</h1>
    <p>Help me</p>
</body>
</html>
Zubayer
  • 571
  • 1
  • 8
  • 16
  • Hello Zubayer, ,have you explored this URL - https://stackoverflow.com/questions/46077392/additional-options-in-chrome-headless-print-to-pdf – Swaroop Humane Apr 10 '21 at 21:57

1 Answers1

-2

There is a method to minimize window. Add this line to your code.

driver.minimize_window()
Monirul Islam
  • 140
  • 1
  • 5
  • Thanks for the answer. But look at my question carefully. I said how to "open" a browser as minimized in selenium. The code you gave will at first open the browser then minimize it. But I won't it to be opened as minimized. – Zubayer Apr 10 '21 at 13:34