小编use*_*847的帖子

PyQt 打印原始 PDF

假设我在当前目录中有一个test.pdf文件,我想使用PyQt gui Printer将此原始文件发送到打印机。

下面的Python3代码打印PDF源代码!我不希望 Qt 为我构建 PDF,而只是使用 gui 对话框将其发送到打印机。

这应该适用于任何操作系统(无lp命令)...假设打印机设备本身理解 PDF。

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_())
Run Code Online (Sandbox Code Playgroud)

python printing pdf pyqt4

5
推荐指数
1
解决办法
7228
查看次数

标签 统计

pdf ×1

printing ×1

pyqt4 ×1

python ×1