Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved What are the units of QPdfDocument::pageSize?

    QtWebEngine
    2
    4
    361
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      mnbv last edited by

      QPdfDocument (5.15.2) seems to not be fully documented yet.

      What are the units of the size returned by pageSize()?

      It appears to be some reasonable pixel-like units except I'm not sure what it's assuming the document's DPI to be. 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 in physical units (e.g. inches) then determine a rendered output size in pixels given a user-specified DPI.

      jsulm 1 Reply Last reply Reply Quote 0
      • M
        mnbv last edited by mnbv

        I broke down and asked on SO, where user Mike dug into the source (which I guess I should've done).

        Long story short, the size returned by pageSize() is in points, and a little further research confirmed it uses the DTP definition where 72 points = 1 inch. The results I'm seeing agree with that scaling.


        #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) << pdf.pageSize(k) / 72.0f;
                QCoreApplication::exit();
            });
        
            return app.exec();
        
        }
        

        Outputs:

        QUrl("http://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf") :
        0 QSizeF(595, 842) QSizeF(8.26389, 11.6944)
        
        1 Reply Last reply Reply Quote 1
        • jsulm
          jsulm Lifetime Qt Champion @mnbv last edited by

          @mnbv What Qt version do you use? I can't find anything like size() or pageSize() in https://doc-snapshots.qt.io/qt5-5.15/qpdfdocument-members.html

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply Reply Quote 0
          • M
            mnbv @jsulm last edited by mnbv

            @jsulm said in What are the units of QPdfDocument::pageSize?:

            @mnbv What Qt version do you use? I can't find anything like size() or pageSize() in https://doc-snapshots.qt.io/qt5-5.15/qpdfdocument-members.html

            @mnbv said in What are the units of QPdfDocument::pageSize?:

            QPdfDocument (5.15.2) seems to not be fully documented yet.

            1 Reply Last reply Reply Quote 0
            • M
              mnbv last edited by mnbv

              I broke down and asked on SO, where user Mike dug into the source (which I guess I should've done).

              Long story short, the size returned by pageSize() is in points, and a little further research confirmed it uses the DTP definition where 72 points = 1 inch. The results I'm seeing agree with that scaling.


              #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) << pdf.pageSize(k) / 72.0f;
                      QCoreApplication::exit();
                  });
              
                  return app.exec();
              
              }
              

              Outputs:

              QUrl("http://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf") :
              0 QSizeF(595, 842) QSizeF(8.26389, 11.6944)
              
              1 Reply Last reply Reply Quote 1
              • First post
                Last post