[Qtwebengine] printToPdf(): Controlling output dimensions/resolution

Akkana Peck akkana at shallowsky.com
Sat May 27 19:56:33 CEST 2017

Following up my own query:

>         margins = QMarginsF(0, 0, 0, 0)
>         layout = QPageLayout(QPageSize(QSizeF(1024, 768),
>                                        QPageSize.Millimeter),
>                              QPageLayout.Portrait, margins,
>                              QPageLayout.Millimeter, margins)
>         self.webpage.printToPdf(self.print_finished, layout)
> I know 1024x768 millimeters isn't right, but it doesn't matter:
> using QSizeF(2, 768), or just a QPageLayout() with no QSizeF at
> all, both produce a PDF that displays exactly like the one with
> QSizeF(1024, 768),

Actually not true about the 2, 768. Making the width much smaller
make a difference, and at about 290mm it comes close to matching the
1024 pixel width, implying that the QWebEnginePage thinks it's about
3.5 dots/mm or 90 DPI. Is that constant, or will it change depending
on the desktop where it's run even when the QWebEnginePage is
headless? (90 is pretty close to my current desktop resolution.)

Changing the QSizeF's height is less helpful. If I make it small,
e.g. QSizeF(290, 100) I get a PDF that matches the width nicely but
does strange things with the height, depending on what PDF app I use
to view it: mupdf shows me a window as tall as my screen with the
colored rectangle clipped to be only a fraction of the height,
evince gives me a 3-page document with the rectangle taking up all
of the first two pages and a little of the third page. So there's
something weird going on with specifying the layout height.


More information about the QtWebEngine mailing list