Python 静默打印 PDF 到特定打印机 [英] Python silent print PDF to specific printer

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

问题描述

我有一个 PDF 文档,我想用我的 Python 应用程序打印它.

我在 here (Print PDF document with python's win32print module)中尝试了解决方案?) 但是当我安装实际版本的 Ghostscript 9.15 时,它没有 gsprint.exe

我使用的方法是使用命令 os.startfile('PDFfile.pdf', "print") 但它打开默认查看器(我的是 Adob​​e Reader)并在打印后仍然打开,试图用 os.system("TASKKILL/F/IM AcroRD32.exe") 终止进程会杀死其他打开的窗口,我不想要它.

用下一个命令,它也打印,但它让Adobe Reader也打开了

currentprinter = win32print.GetDefaultPrinter()win32api.ShellExecute(0, "print", 'PDFfile.pdf', '/d:"%s"' % currentprinter, ".", 0)

我也看过这个答案,但他们建议使用 <再次代码>gsprint.exe

有人有 gsprint.exe 文件或任何其他解决方案吗?.

注意:当我使用另一个默认程序打开 PDF 文件(如 Chrome 或 Windows 阅读器)时,我总是在执行上述命令时遇到异常 '(31, 'ShellExecute', '连接到系统的设备不起作用.')'[错误 1155] 没有应用程序与此操作的指定文件相关联:'PDFfile.pdf'startfile 命令

解决方案

经过数小时的寻找合适的文件,我终于找到了问题的答案.

您可以在这里

下载GSPRINT

您可以在 HERE

下载 Ghostscript GPL

在您的 PC (Windows) 中使用此提取的文件,您可以使用此命令打印 PDF

GHOSTSCRIPT_PATH = "C:\path\to\GHOSTSCRIPT\bin\gswin32.exe"GSPRINT_PATH = "C:\path\to\GSPRINT\gsprint.exe"# 您可以在此处输入特定打印机的名称而不是默认值当前打印机 = win32print.GetDefaultPrinter()win32api.ShellExecute(0, 'open', GSPRINT_PATH, '-ghostscript "'+GHOSTSCRIPT_PATH+'" -printer "'+currentprinter+'" "PDFFile.pdf"', '.', 0)

GhostScript 也可以在官方页面 HERE

中找到

我找到了 64 位的 gsprint.exe 这里

我希望这会有所帮助.

I have a PDF document and I want to print it with my python app.

I have tried the solution in here (Print PDF document with python's win32print module?) but when I install Ghostscript 9.15 that is the actual version, it has no gsprint.exe

The way I am using that works is with the command os.startfile('PDFfile.pdf', "print") but it opens default viewer (mine is Adobe Reader) and after printing it stills open, trying to kill the process with os.system("TASKKILL /F /IM AcroRD32.exe") kills other opened windows and I dont want it.

With the next command, it also prints, but it let the Adobe Reader opened too

currentprinter = win32print.GetDefaultPrinter()
win32api.ShellExecute(0, "print", 'PDFfile.pdf', '/d:"%s"' % currentprinter, ".", 0)

I have seen this answer too but they recommend using gsprint.exe again

Anybody has the gsprint.exe file or any other solution?.

NOTE: When I used another default program to open PDF files like Chrome or Windows Reader, I always get an Exception in the execution of the commands above '(31, 'ShellExecute', 'A device attached to the system is not functioning.')' or [Error 1155] No application is associated with the specified file for this operation: 'PDFfile.pdf' with the startfile command

解决方案

Finally after hours and hours of searching for the right files, i have found the answer to my problem.

You can download the GSPRINT in HERE

You can download the Ghostscript GPL in HERE

With this extracted files in your PC (Windows) you can print your PDF with this command

GHOSTSCRIPT_PATH = "C:\path\to\GHOSTSCRIPT\bin\gswin32.exe"
GSPRINT_PATH = "C:\path\to\GSPRINT\gsprint.exe"

# YOU CAN PUT HERE THE NAME OF YOUR SPECIFIC PRINTER INSTEAD OF DEFAULT
currentprinter = win32print.GetDefaultPrinter()

win32api.ShellExecute(0, 'open', GSPRINT_PATH, '-ghostscript "'+GHOSTSCRIPT_PATH+'" -printer "'+currentprinter+'" "PDFFile.pdf"', '.', 0)

The GhostScript can also be found in the Official page HERE

I found the gsprint.exe for 64bits HERE

I hope this helps.

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

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