假设我在当前目录中有一个test.pdf文件,我想使用PyQt gui打印机将这个原始文件发送到打印机。
下面的Python3代码打印PDF源代码!我不希望Qt为我构建PDF,但只需将其发送到带有gui对话框的打印机。
这应该适用于打印机设备本机理解的任何操作系统(没有lp命令) ...assuming。
import sys, PyQt4.QtCore, PyQt4.QtGui
def pdf():
pdf = open('test.pdf', encoding='utf-8').read() # ascii PDF here
doc = PyQt4.QtGui.QTextDocument(pdf)
printer = PyQt4.QtGui.QPrinter()
dialog = PyQt4.QtGui.QPrintDialog(printer)
if dialog.exec_() == True:
doc.print_(printer)
if __name__ == '__main__':
app = PyQt4.QtGui.QApplication(sys.argv)
w = PyQt4.QtGui.QWidget()
but = PyQt4.QtGui.QPushButton('Print', w)
but.clicked.connect(pdf)
PyQt4.QtGui.QVBoxLayout(w).addWidget(but)
w.show()
sys.exit(app.exec_())发布于 2012-09-04 16:33:40
在支持中构建了一个PDF格式的文档,但不是用来阅读的。
要读取PDF文档,您必须首先使用第三方库,或者使用外部工具将pdf转换为另一种格式(例如文本或html)。
有关处理PDF文档的概述,请参见这里。
https://stackoverflow.com/questions/12262274
复制相似问题