windows 在 Python 中静默打印 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4498099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:50:05  来源:igfitidea点击:

Silent printing of a PDF in Python

pythonwindowspdfprintingsilent

提问by bluish

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).

我正在尝试使用 Python 打印 PDF,而无需打开 PDF 查看器应用程序(Adobe、Foxit 等)。我还需要知道打印何时完成(删除文件)。

HereI found this implementation:

在这里我找到了这个实现

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()

But it throws this exception at the ConnectToline:

但它在这一ConnectTo行抛出这个异常:

dde.error: ConnectTo failed

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

有人知道如何解决吗?或者有不同的静音打印解决方案?或者在列表中可以给出一个参考ConnectTo的链接?在网上找不到任何关于它的信息。

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

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

回答by codeape

I suggest you install GSViewand GSPrintand shell out to gsprint.exeto print the pdf.

我建议你安装GSViewGSPrint并 shell outgsprint.exe来打印 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.

我在工业标签打印解决方案中使用过它,效果很好。

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

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