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

查看:28
本文介绍了在 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

有人知道怎么解决吗?或者对于静音打印有不同的解决方案?或者在列表中可以提供指向 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

推荐答案

我建议你安装 GSViewGSPrintgsprint.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	ogsprint.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天全站免登陆