'selenium with geckodriver does not respect download dir and download filename when executing window.print();
I have a set up where I print the page as pdf using selenium+gecko. But, whatever I do, it does not seem to respect the download.dir
option I set nor the download filename.
Here are my settings:
self.profile = webdriver.FirefoxProfile()
self.headers = {'User-Agent' : uagent, \
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', \
'Accept-Language' : 'en-gb,en;q=0.5', \
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', \
'Keep-Alive' : '115', \
'Connection' : 'keep-alive', \
'Cache-Control' : 'max-age=0'}
self.profile.set_preference('browser.link.open_newwindow', 1)
self.profile.set_preference("general.useragent.override", uagent)
self.profile.set_preference("browser.download.manager.showWhenStarting", False)
self.profile.set_preference("browser.download.dir", '/home/foo/')
self.profile.set_preference("print_printer", "Mozilla Save to PDF")
self.profile.set_preference("print.always_print_silent", True)
self.profile.set_preference("print.show_print_progress", False)
self.profile.set_preference('print.save_as_pdf.links.enabled', True)
self.profile.set_preference("print.printer_Mozilla_Save_to_PDF.print_to_file", True)
self.profile.set_preference("print.printer_Mozilla_Save_to_PDF.print_to_filename", "file.pdf")
self.binary = FirefoxBinary('/path/to/Downloads/dir/firefox/firefox')
I have also tried:
self.profile.set_preference("print.print_to_filename", "file.pdf")
To print I do:
browser = webdriver.Firefox(firefox_profile=self.profile, \
firefox_binary=self.binary, executable_path='/some/path/dir/and/orm/driver/geckodriver')
browser.execute_script("window.print();")
When I execute this I get the PDF in the directory I execute the script from and always as mozilla.pdf
I am not sure what settings to use to change this.
I tried changing the page name to see if this will have any effect, but still, it prints out as mozilla.pdf
So yea, I am looking for a solution which can:
- set the directory of the pdf
- set the name of the pdf
:(
Solution 1:[1]
You were almost there. Once you have configured all the settings through set_preference()
, finally you have to use update_preferences()
as follows:
self.profile.update_preferences()
References
You can find a couple of relevant detailed discussions in:
Solution 2:[2]
To resolve, you need to include the path & filename as one.
- Below is using
options
, your example is usingself
, but the idea is the same!
important to note that printing will rasterize the PDF as an image, you cannot then highlight text...
url = '<__some_public_pdf_URL_here__>'
out_dir = './data/' # whatever your outdir is for the printed file
out_filename = 'file.pdf'
out_path = out_dir + out_filename
options = FirefoxOptions()
options.add_argument("--start-maximized")
options.set_preference("print.always_print_silent", True)
options.set_preference("print.printer_Mozilla_Save_to_PDF.print_to_file", True)
# this line should guarantee export to correct dir & filename
options.set_preference('print.printer_Mozilla_Save_to_PDF.print_to_filename', '{}'.format(out_path))
options.set_preference("print_printer", "Mozilla Save to PDF")
driver = webdriver.Firefox(options=options)
try:
driver.get(url)
sleep(3) # give the browser a chance to load PDF
driver.execute_script('window.print();')
sleep(10) # give the PDF a chance to print, 10 seconds should be enough
driver.close()
except:
print('oops! failed for {}'.format(url)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | undetected Selenium |
Solution 2 | Etienne Jacquot |