What are the units of QPdfDocument::pageSize?

478 Views Asked by At

QPdfDocument (5.15.2), in the QtPDF module (part of QtWebEngine), seems to not be fully documented yet (presumably because it's fairly new, first appearing in 5.14).

What are the units of the size returned by QPdfDocument::pageSize() (don't bother checking those docs, it's not there)?

It appears to be some reasonable (albeit seemingly low-res) pixel-like units except I'm not sure how it is deducing the document's DPI. Also I've only tested on a limited set of PDF's all generated from the same source, so I'm not sure how normal my observations are, especially given it's a QSizeF rather than a QSize (which raises the possibility of e.g. other non-pixel units in other as-of-yet unencountered contexts).

Ultimately what I'd like to do is get the page size of a loaded document's page in physical units (e.g. inches) then determine a rendered output size in pixels given a user-specified DPI.


An example of the values I've observed:

#include <QCoreApplication>
#include <QDebug>
#include <QtNetwork>
#include <QtPdf>

int main (int argc, char *argv[]) {

    QCoreApplication app(argc, argv);

    QUrl url("http://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf");
    QNetworkAccessManager internet;
    QNetworkReply *reply = internet.get(QNetworkRequest(url));

    QObject::connect(reply, &QNetworkReply::finished, [reply] () {
        QPdfDocument pdf;
        pdf.load(reply);
        qDebug() << reply->url() << ":";
        for (int k = 0; k < pdf.pageCount(); ++ k)
            qDebug() << k << pdf.pageSize(k);
        QCoreApplication::exit();
    });

    return app.exec();

}

Outputs:

QUrl("http://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf") :
0 QSizeF(595, 842)
1

There are 1 best solutions below

0
On BEST ANSWER

Looks like the implementation is done with PDFium. Qt calls FPDF_GetPageSizeByIndex and the docs state the height/width is in points:

// Function: FPDF_GetPageSizeByIndex
//          Get the size of the page at the given index.
// Parameters:
//          document    -   Handle to document. Returned by FPDF_LoadDocument.
//          page_index  -   Page index, zero for the first page.
//          width       -   Pointer to a double to receive the page width
//                          (in points).
//          height      -   Pointer to a double to receive the page height
//                          (in points).
// Return value:
//          Non-zero for success. 0 for error (document or page not found).
// Note:
//          Prefer FPDF_GetPageSizeByIndexF() above. This will be deprecated in
//          the future.
FPDF_EXPORT int FPDF_CALLCONV FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
                                                      int page_index,
                                                      double* width,
                                                      double* height);

This is also noted in the PDFium SDK manual.