PDF printing using Python, win32api and Acrobat Reader 9

I have reports that I submit to a system that requires the reports to be in a readable PDF. I tried all free libraries and applications and the only one I found was the Adobe Acrobat family.

I wrote a quick script in python that uses win32api to print a pdf file to my printer with the app registered by default (Acrobat Reader 9), then to complete the task after completing, as the acrobat likes to keep the window open when called from the command line.

I compiled it into an executable and passed the values ​​through the command line (like printer.exe% OUTFILE %% PRINTER%) then it is called in a batch file

import os,sys,win32api,win32print,time

# Command Line Arguments.  
pdf = sys.argv[1]
tempprinter = sys.argv[2]

# Get Current Default Printer.  
currentprinter = win32print.GetDefaultPrinter()
# Set Default printer to printer passed through command line.  
# Print PDF using default application, AcroRd32.exe
win32api.ShellExecute(0, "print", pdf, None, ".", 0)
# Reset Default Printer to saved value
# Timer for application close
# Kill application and exit scipt
os.system("taskkill /im AcroRd32.exe /f")


This seemed to work well for high volume, ~ 2000 reports in 3-4 hours, but I have some that go away and I'm not sure if this w370> is overloaded, or if I look into multithreading or something yet.

The fact that it is handling such a large amount without interruption leads me to believe that the problem is not in the script, but I'm not sure if the problem is with the host system or Adobe Reader or whatever.

Any suggestions or opinions are greatly appreciated.


source to share

1 answer

Based on your feedback ( win32api.ShellExecute()

probably out of sync), your problem is a timeout: if your computer or print queue is busy, the kill command may arrive too early.

If your script runs at the same time (i.e. you print all documents at the same time, rather than one after the other), the kill command might even kill the wrong process (i.e. the acrobat process started by another call to the script).

So what you need is the best sync. There are several things you can try:

  • Convert it to a server script that starts Acrobat once, then sends many print commands to the same process, and exits afterwards.

  • Use global locking to ensure that only one script is ever executed. I suggest creating a folder somewhere; it is an atomic operation for every filesystem. If the folder exists, the script is active somewhere.

Also, you need to know when the quest will be completed. Use win32print.EnumJobs()

for this.

If that fails, another solution might be to install a Linux server somewhere. You can start a Python server in this box, which accepts print jobs that you submit using a small Python script on your client machine. The server can then print the PDF files for you in the background.

This approach allows you to add any kind of monitoring that you like (sending emails if something fails or sending status mail after completing all tasks).



All Articles