1

I am using this answer to download and save webpages as a pdf.

Although the method works, it is saving the output files in a different directory.

What could I add to the code to specify a custom output directory?

In addition to that, is there a way to change the output file name?

This is the code so far:

import json
from selenium import webdriver

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')
Josie
  • 79
  • 8

2 Answers2

2

You can provide default directory in your preferences by adding 'savefile.default_directory' with a path to your folder, like this:

prefs = {
    'printing.print_preview_sticky_settings.appState': json.dumps(settings),
    'savefile.default_directory': '/path_to_folder/'
}

(Also, the answer here has an example of how the path should look for Windows)

As far, as saving the file with a custom name, I couldn't find an easy solution myself, but I used this example from SO when I was building something similar. I hope this helps. Good luck!

Svetlana Levinsohn
  • 1,550
  • 3
  • 10
  • 19
0

This is how you can specify your download directory.This is Java solution.

    downloadPath=System.getProperty("user.dir")+"\\tempDownloadedFiles";
Gaj Julije
  • 1,538
  • 15
  • 32