PyQt打印原始PDF [英] PyQt Printing raw PDF

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

问题描述

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

Assuming I have a test.pdf file in the current directory, I would like to send this raw file to a printer using the PyQt gui Printer.

以下Python3代码可打印PDF源代码!我不希望Qt为我构建PDF,而只是通过GUI对话框将其发送到打印机.

The following Python3 code prints the PDF source code! I don't want Qt to build the PDF for me, but just send it to the printer with a gui dialog box.

这应该在任何操作系统上都可以使用(无lp命令)……假设打印机设备本身可以理解PDF.

This should work on any OS, (no lp command) ...assuming that the printer device understands PDF natively.

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

推荐答案

其中内置了支持编写 PDF格式的文档,但不支持读取的文档.

There is built in support for writing a document in PDF format, but not for reading one.

要阅读PDF文档,您必须首先使用第三方库,或使用外部工具将pdf转换为另一种格式(例如文本或html).

To read PDF documents, you will have to use a third-party library, or use an external tool to convert the pdf to another format (such as text, or html) first.

有关处理PDF文档的概述,请参见此处.

See here for an overview on handling PDF documents.

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

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