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

Akkana Peck akkana at shallowsky.com
Sat May 27 19:31:21 CEST 2017


I need to take HTML that has CSS to fit a particular screen size,
e.g. 1024x768, and turn it into PDF set for the same screen size.
I'm using printToPdf() from a headless QWebEnginePage.

With the old QPrinter API, it used to be possible to do
    QPrinter.setPaperSize(QSizeF(1024, 768), QPrinter.DevicePixel)

With QPageLayout, used by QWebEnginePage.printToPdf(), there's no more
DevicePixel, and all the options correspond to physical measurements:
    enum Unit { Millimeter, Point, Inch, Pica, Didot, Cicero }

So, okay, the new API is more limited and I have to choose some
physical measurement for the PDF now. But what DPI does a headless
QWebEnginePage have? And in practice, the dimensions I choose are
ignored anyway.

For instance, the simple test script
http://shallowsky.com/tmp/qwebengineprint.py
with:

        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), showing the yellow/red rectangle plus lots of extra
unwanted space at the right and bottom.

Given that I can't specify DevicePixels in the PDF, how can I print
a PDF where the dimensions correspond to the limits of the web page
(1024x768) without extra space?

(This is for presentations, in case you're curious. HTML/CSS/JS
works great for giving presentations, but it's helpful to have a PDF
version available in case of emergency, and some conferences insist
on submitted slides being .ppt or .pdf.)

Thanks,

        ...Akkana



More information about the QtWebEngine mailing list