使用 Python、win32api 和 Acrobat Reader 9 打印 PDF [英] Printing PDF's using Python,win32api, and Acrobat Reader 9

查看:182
本文介绍了使用 Python、win32api 和 Acrobat Reader 9 打印 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将报告发送到要求报告采用可读 PDF 格式的系统.我尝试了所有免费的库和应用程序,我发现唯一可以工作的是 Adob​​e 的 acrobat 系列.

I have reports that I am sending to a system that requires the reports be in a readable PDF format. I tried all of the free libraries and applications and the only one that I found worked was Adobe's acrobat family.

我在 python 中编写了一个快速脚本,它使用 win32api 使用默认注册的应用程序 (Acrobat Reader 9) 将 pdf 打印到我的打印机,然后在完成后终止任务,因为 acrobat 喜欢在从命令行.

I wrote a quick script in python that uses the win32api to print a pdf to my printer with the default registered application (Acrobat Reader 9) then to kill the task upon completion since acrobat likes to leave the window open when called from the command line.

我把它编译成可执行文件并通过命令行传入值(例如,printer.exe %OUTFILE% %PRINTER%)然后在批处理文件中调用它

I compiled it into an executable and pass in the values through the command line (for example printer.exe %OUTFILE% %PRINTER%) this is then called within 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.  
win32print.SetDefaultPrinter(tempprinter)
# Print PDF using default application, AcroRd32.exe
win32api.ShellExecute(0, "print", pdf, None, ".", 0)
# Reset Default Printer to saved value
win32print.SetDefaultPrinter(currentprinter)
# Timer for application close
time.sleep(2)
# Kill application and exit scipt
os.system("taskkill /im AcroRd32.exe /f")

这似乎适用于在 3-4 小时内处理大约 2000 个报告的大量报告,但我有一些下降,我不确定脚本是否不堪重负,或者我是否应该研究多线程或别的东西.

This seemed to work well for a large volume, ~2000 reports in a 3-4 hour period but I have some that drop off and I'm not sure if the script is getting overwhelmed or if I should look into multithreading or something else.

事实上,它处理如此大的数量而没有下降,这让我相信问题不在于脚本,但我不确定是主机系统或 Adob​​e Reader 的问题,还是其他什么问题.

The fact that it handles such a large amount with no drop off leads me to believe that the issue is not with the script but I'm not sure if its an issue with the host system or Adobe Reader, or something else.

任何建议或意见将不胜感激.

Any suggestions or opinions would be greatly appreciated.

推荐答案

根据您的反馈(win32api.ShellExecute() 可能同步),您的问题是超时:如果您的计算机或打印队列繁忙,kill 命令可能会过早到达.

Based on your feedback (win32api.ShellExecute() is probably not synchronous), your problem is the timeout: If your computer or the print queue is busy, the kill command can arrive too early.

如果您的脚本同时运行(即您一次打印所有文档而不是一个接一个打印),kill 命令甚至可能会终止错误的进程(即另一个脚本调用启动的 acrobat 进程).

If your script runs concurrently (i.e. you print all documents at once instead of one after the other), the kill command could even kill the wrong process (i.e. an acrobat process started by another invocation of the script).

所以你需要更好的同步.您可以尝试以下几种方法:

So what you need it a better synchronization. There are a couple of things you can try:

  1. 将其转换为启动 Acrobat 一次的服务器脚本,然后将许多打印命令发送到同一进程并随后终止.

  1. Convert this into a server script which starts Acrobat once, then sends many print commands to the same process and terminates afterwards.

使用全局锁来确保只有一个脚本在运行.我建议在某处创建一个文件夹;这是对每个文件系统的原子操作.如果文件夹存在,则脚本在某处处于活动状态.

Use a global lock to make sure that ever only a single script is running. I suggest to create a folder somewhere; this is an atomic operation on every file system. If the folder exists, the script is active somewhere.

最重要的是,您需要知道工作何时完成.为此使用 win32print.EnumJobs().

On top of that, you need to know when the job is finished. Use win32print.EnumJobs() for this.

如果失败,另一种解决方案可能是在某处安装 Linux 服务器.你可以在这个机器上运行一个 Python 服务器,它接受你在客户端机器上的一个小的 Python 脚本的帮助下发送的打印作业.然后服务器可以在后台为您打印 PDF.

If that fails, another solution could be to install a Linux server somewhere. You can run a Python server on this box which accepts print jobs that you send with the help of a small Python script on your client machine. The server can then print the PDFs for you in the background.

这种方法允许您添加您喜欢的任何类型的监控(如果出现问题就发送邮件或在所有作业完成后发送状态邮件).

This approach allow you to add any kind of monitoring you like (sending mails if something fails or send a status mail after all jobs have finished).

这篇关于使用 Python、win32api 和 Acrobat Reader 9 打印 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆