PyQt Printing Raw Materials PDF

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

The following Python3 code prints the PDF source code! I do not want Qt to create a PDF for me, but just send it to a printer with the gui dialog box.

This should work on any OS ( lp ) ... provided 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_()) 
+4
source share
1 answer

The support document is written in PDF format, but not for reading.

To read documents in PDF format, you will have to use a third-party library or use an external tool to convert PDF to another format (for example, text or html).

See here for an overview of PDF documents.

+1
source

Source: https://habr.com/ru/post/1432249/


All Articles