用Python静默打印PDF [英] Silent printing of a PDF in Python

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

问题描述

我试图用Python打印PDF,而不打开PDF查看器应用程序(Adobe,Foxit等).我还需要知道打印何时完成(删除文件).

I'm trying to print a PDF with Python, without opening the PDF viewer application (Adobe, Foxit etc.). I need also to know when printing has finished (to delete the file).

此处我发现了此实现 :

import win32ui, dde, os.path, time
from win32api import FindExecutable
from os import spawnl, P_NOWAIT
...
pd = "C:\\temp\\test.pdf"
pdbits = os.path.split(pd)
readerexe = FindExecutable(pdbits[1],pdbits[0])

spawnl(P_NOWAIT,readerexe[1],"DUMMY") #I added "DUMMY" to avoid a weird error

time.sleep(2)

s = dde.CreateServer()
s.Create('')
c = dde.CreateConversation(s)
c.ConnectTo('acroview', 'control')

c.Exec('[FilePrintSilent("%s")]' % (pd,))

s.Destroy()

但是它会在ConnectTo行抛出此异常:

But it throws this exception at the ConnectTo line:

dde.error: ConnectTo failed

有人知道如何解决吗?还是对于静默打印有不同的解决方案?还是at list可以提供指向 ConnectTo 的引用的链接?在网上找不到关于它的任何信息.

Someone knows how to solve it? Or has a different solution for silent printing? Or at list can give a link to a reference for ConnectTo? Could find nothing on the web about it.

使用:Python 2.7,Windows 7,Acrobat Reader 10.0

Working with: Python 2.7, Windows 7, Acrobat Reader 10.0

推荐答案

我建议您安装 GSPrint 并打包为gsprint.exe打印pdf.

I suggest you install GSView and GSPrint and shell out to gsprint.exe to print the pdf.

p = subprocess.Popen([r"p:\ath\to\gsprint.exe", "test.pdf"], 
                     stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
print stdout
print stderr

我已经在工业标签打印解决方案中使用了它,效果很好.

I have used this in a industrial label printing solution, works great.

gsprint.exe程序退出时(即在调用communicate之后),您可以删除pdf文件.

When the gsprint.exe program exits (i.e. after the call to communicate), you can delete the pdf file.

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

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