I think I am a little late for the answer but if you are still looking for an answer then you can use this method. This is a different approach but since you are okay with it so I am providing it. Here's the code:
import webbrowser
import pyautogui
import time
import pyperclip
import os
import win32print
def print_job_checker():
"""
Prints out all jobs in the print queue every 5 seconds
"""
jobs = [1]
while jobs:
jobs = []
time.sleep(5)
for p in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL, None, 1):
flags, desc, name, comment = p
phandle = win32print.OpenPrinter(name)
print_jobs = win32print.EnumJobs(phandle, 0, -1, 1)
if print_jobs:
jobs.extend(list(print_jobs))
for job in print_jobs:
print("printer name => " + name)
document = job["pDocument"]
print("Document name => " + document)
win32print.ClosePrinter(phandle)
print("No more jobs!")
# opens the input pdf in the web browser
webbrowser.open('in.pdf')
time.sleep(2)
# enters the password for your pdf
pyautogui.typewrite("your_pdf_password")
time.sleep(0.44)
pyautogui.press("enter")
time.sleep(1)
# press ctrl+p ket
pyautogui.hotkey('ctrl', 'p')
time.sleep(3)
# moves cursor to the coordinate where 'Print' button appeared
pyautogui.moveTo(338, 489, duration=2, tween=pyautogui.easeInOutQuad)
time.sleep(0.88)
# perform left mouse click on print button
pyautogui.leftClick()
time.sleep(2)
# types the filename of output pdf
pyautogui.typewrite("new_file_name")
time.sleep(1)
# directory where you will save your output file
save_dir = "D:/folder/name"
# creates the output dir if doesn't exists
if not os.path.exists(save_dir):
os.makedirs(save_dir)
time.sleep(1)
# copy output dir path (save_dir) to clipboard
pyperclip.copy(save_dir)
# moves cursor to the coordinate where address bar of directory appeared
pyautogui.moveTo(569, 65, duration=2, tween=pyautogui.easeInOutQuad)
# perform left mouse click on the bar to edit the path
pyautogui.leftClick()
time.sleep(0.41)
# click ctrl+v keys so which pastes the output dir (save_dir) path
pyautogui.hotkey('ctrl', 'v')
time.sleep(0.71)
# press enter
pyautogui.press("enter")
time.sleep(0.23)
# moves cursor to the coordinate where 'Save' button is
pyautogui.moveTo(747, 558, duration=2, tween=pyautogui.easeInOutQuad)
# perform left mouse click on 'Save' button
pyautogui.leftClick()
# waits until the print job is complete
print_job_checker()
time.sleep(0.96)
# performs ctrl+w key press which is shortcut for closing the current tab in browser but since it has only 1 tab
# it will close the complete browser
pyautogui.hotkey('ctrl', 'w')
Let me explain what the program does. First it opens the input pdf in a web browser, then presses ctrl+p to open the print dialog but here the system print dialog will appear not the browser's own dialog (I will explain below how you can get it beforehand) then the cursor will move on the button where 'Print' button is and then perform left click on it (I will explain below how you can get the exact coordinates of the buttons beforehand). Then the dialog box will appear asking you the path where to save the file and also the file name, so the program will move the cursor to the top bar in this dialog box where path is written and will edit it and paste the path where you want to save the file and then pres 'enter' so that it goes to that path and then move the cursor to 'Save' button and perform a left click on it and then wait till the print job is complete and then close the browser.
You can put it in a for loop to handle multiple files, just make sure that output file name should be different for each, otherwise it will ask (when you save the file) if you want to replace the file and you will have to manage that as well.
We will be using Firefox as browser. So kindly install it first. And in firefox open settings and tick 'Always ask you where to save files'. So that you can change the output file location and its name.
Q1) How the system print dialog will appear instead of browser's own dialog?
A) For that go to address bar on the top in the firefox browser and then type about:config and press enter it will ask you to be careful, just press 'accept the risk and continue'. We will be changing default preferences of the browser through this. In the 'Search preference name' bar type print_printer and make sure its value is Microsoft Print to PDF (this will use Microsoft Print to PDF as your default printer). Then type print.prefer_system_dialog in 'Search preference name' and set its value to true (this will make sure that system print dialog appears everytime you print something from the browser instead of browser's own print dilaog). There are other settings related to Microsoft Print to PDF that you can change as well if you want, they will all start with print.printer_Microsoft_Print_to_PDF.something. But these 2 are necessary. You can change them back later if you want.
Q2) How to get the exact coordinates where the buttons will appear beforehand?
A) For this you need to perform this process manually just once to get the coordinates. First run this program:
import pyautogui
pyautogui.displayMousePosition()
This will display the live position of your cursor in the output. Now open any pdf in your browser then press ctrl+p, the system print dialog will appear, hover your cursor over the print button and switch to the output of the above program (using alt+tab) without moving your cursor, note down these coordinates. Now click on it and the dialog box will appear asking you the file name of the output file and the directory where you want to save it. Now hover your cursor over the address bar at the top just before that drop down arrow in the address bar and get the coordinates. And now hover the cursor over 'Save' button and get the coordinates.
I used time.sleep() everywhere, it makes the automation a little human and avoid errors if some part takes a little extra time to complete.
Replace the coordinates I used in the program with the coordinates you got and now you can run the program.
I think I covered almost everything, still if you have any doubts and if the program does not work, you can let me know.