Print preview and print PDF in PyQT5

Giuseppe Corbelli corbelligiuseppe at mesdan.it
Tue Jun 16 09:52:21 BST 2020


On 6/12/20 1:08 PM, Justin Giffard wrote:
> Good afternoon
> 
> I'm looking for a way to make a print preview and print a PDF. I've 
> tried various things including trying to use a webview and print from 
> there without much success.
> 
> Currently I am generating a PDF with reportlab but ideally what I want 
> is to be able view the document after the pdf is generated (based on 
> data fetched from a database) and then if need be press cancel to alter 
> the parameters and then view it again then chose which printer I want 
> to print to and then print. Thus far I haven't had much luck other than 
> making the app open the document in an external browser / external PDF 
> viewer and then printing from there or blindly printing to the default 
> printer.

Well these are 2 separate questions, and you're not mentioning what OSes 
you want this stuff to run on.

QtWebEngine supports viewing and printing PDFs [1].

PDF preview:
* poppler [2][3]
* qpdfview (or any other) that have a DBUS interface

As for the printing stuff, if you use poppler you can render to QImage 
and print that using QPrinter / QPainter.

This has some more info
https://wiki.qt.io/Handling_PDF


[1] https://doc.qt.io/qt-5/qtwebengine-features.html
[2] https://pypi.org/project/python-poppler-qt5/#description
[3] http://shallowsky.com/blog/programming/qt5-poppler-pdf.html

-- 
Giuseppe Corbelli


More information about the PyQt mailing list