Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to generate .pdf files properly
Qt 6.12 LTS is out! See what's new in the release blog

How to generate .pdf files properly

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 6.3k Views 3 Watching
  • 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.
  • A Offline
    A Offline
    Aronax
    wrote on last edited by
    #1

    Hello, I want to generate a .pdf file which should contain text, images and tables. I tried reading .html file and format .pdf based on it using QTextDocument class, but when .pdf is generated - not all styles are in the page. (padding, border-bottom etc). Some people are generating .pdf files using QPainter class, but I really don't think it is a right move. So which tool is the most suitable for generating .pdf files?

    JonBJ 1 Reply Last reply
    0
    • A Aronax

      Hello, I want to generate a .pdf file which should contain text, images and tables. I tried reading .html file and format .pdf based on it using QTextDocument class, but when .pdf is generated - not all styles are in the page. (padding, border-bottom etc). Some people are generating .pdf files using QPainter class, but I really don't think it is a right move. So which tool is the most suitable for generating .pdf files?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Aronax
      Use QWebEngine::printToPdf(), or QPdfWriter may be all you need.

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Aronax
        wrote on last edited by
        #3

        @JNBarchan I tried QWebEnginePage, but it prints empty .pdf page. My code:

        QWebEnginePage page;
        page.setHtml(html); // QString html
        page.printToPdf(src, QPageLayout( QPageSize( QPageSize::A4 ), QPageLayout::Portrait, QMarginsF() )); 
        
        JonBJ 1 Reply Last reply
        0
        • A Aronax

          @JNBarchan I tried QWebEnginePage, but it prints empty .pdf page. My code:

          QWebEnginePage page;
          page.setHtml(html); // QString html
          page.printToPdf(src, QPageLayout( QPageSize( QPageSize::A4 ), QPageLayout::Portrait, QMarginsF() )); 
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Aronax
          QWebEnginePage.setHtml() (http://doc.qt.io/qt-5/qwebenginepage.html#setHtml) is, or may be, asynchronous. This means that you may be calling printToPdf() too early, before the HTML is fully there. I have (effectively ) put my call into slot for QWebEnginePage.loadFinished() signal. You should try that.

          If still "empty", examine actual PDF generated or try on very simple small HTML first to verify whether it's anything about your actual content that is causing a problem.

          P.S.
          See also https://forum.qt.io/topic/83480/qt-5-9-1-does-qprinter-really-work-to-create-pdf-documents, and you may only need to use QPdfWriter, I don't know.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Aronax
            wrote on last edited by
            #5

            @JNBarchan Yeah. It works now, but i don't think that I should use QWebEnginePage just because I want to generate .pdf file is the optimal solution. QPdfWriter uses QPainter functions which is not good in my opinion..

            JonBJ 1 Reply Last reply
            0
            • A Aronax

              @JNBarchan Yeah. It works now, but i don't think that I should use QWebEnginePage just because I want to generate .pdf file is the optimal solution. QPdfWriter uses QPainter functions which is not good in my opinion..

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Aronax
              I sort of agree with you, or did initially, but this seems to be how you do it in Qt. That's where the Qt PDF generator code is located, unless someone knows better that's all that's on offer. I'm not sure, but using QPdfWriter may be more lightweight, I wouldn't get hung up on the fact that it uses QPainter, it's just producing PDF not actually drawing things....

              1 Reply Last reply
              0
              • Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on last edited by Pablo J. Rogina
                #7

                @Aronax and what about Poppler PDF library? which has a companion wrapper Poppler Qt5 interface library.

                As you will see there, the wrapper is used in KDE's document viewer Okular.

                You even have this well detailed old article about displaying PDF files with Qt using Poppler.

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt
                • Unsolved