Re: Print preview and print PDF in PyQT5

Giuseppe Corbelli <[email protected]> Tue, 16 Jun 2020 10:52:21 +0200
Newsgroups gmane.comp.python.pyqt-pykde
Message-ID <[email protected]>
On 6/12/20 1:08 PM, Justin Giffard wrote:
> Good afternoon
>=20
> I'm looking for a way to make a print preview and print a PDF. I've=20
> tried various things including trying to use a webview and print from=20
> there without much success.
>=20
> Currently I am generating a PDF with=C2=A0reportlab but ideally=C2=A0what=
 I want=20
> is to be able view the document after the pdf is generated (based on=20
> data=C2=A0fetched from a database) and then if need be press cancel to al=
ter=20
> the parameters and then view it again then chose which printer I want=20
> to=C2=A0print to and then print. Thus far I haven't had much luck other t=
han=20
> making the app open the document in an external browser / external PDF=20
> viewer and then printing from there or blindly printing to the default=20
> printer.

Well these are 2 separate questions, and you're not mentioning what OSes=20
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=20
and print that using QPrinter / QPainter.

This has some more info
https://wiki.qt.io/Handling=5FPDF


[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

--=20
Giuseppe Corbelli