Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Convert HTML to PDF
Forum Updated to NodeBB v4.3 + New Features

Convert HTML to PDF

Scheduled Pinned Locked Moved Unsolved General and Desktop
48 Posts 9 Posters 10.2k Views 2 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.
  • M Offline
    M Offline
    Marcus Barnet
    wrote on last edited by
    #1

    I'm trying to convert an html web page to PDF.

    I found out that there is this library in C++: https://wkhtmltopdf.org/downloads.html that can be used in QT.

    However, I downloaded the source code and I opened it with Qt Creator, but when I try to build it, it gives me this error:

    Project ERROR: Unknown module(s) in QT: webkit webkitwidgets
    

    I found a topic on this forum that explains how to install the webkitwidgets package, but I can't find the source code for my Qt version which should be Qt 5.15.0 with msvc2019_64.

    Or, if it is too difficult to install and use this library, can you suggest me another solution, please?

    At the moment, my program generates a HTML page with the content produced by the user and send it as attachment by email. It works fine most of the times, but I noticed that some smartphones or email clients automatically remove the attached webpage so I would like to convert the html page to PDF to send the content as a PDF file.

    Thank you!

    artwawA 1 Reply Last reply
    0
    • M Marcus Barnet

      I'm trying to convert an html web page to PDF.

      I found out that there is this library in C++: https://wkhtmltopdf.org/downloads.html that can be used in QT.

      However, I downloaded the source code and I opened it with Qt Creator, but when I try to build it, it gives me this error:

      Project ERROR: Unknown module(s) in QT: webkit webkitwidgets
      

      I found a topic on this forum that explains how to install the webkitwidgets package, but I can't find the source code for my Qt version which should be Qt 5.15.0 with msvc2019_64.

      Or, if it is too difficult to install and use this library, can you suggest me another solution, please?

      At the moment, my program generates a HTML page with the content produced by the user and send it as attachment by email. It works fine most of the times, but I noticed that some smartphones or email clients automatically remove the attached webpage so I would like to convert the html page to PDF to send the content as a PDF file.

      Thank you!

      artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      @Marcus-Barnet webkit can be downloaded here https://download.qt.io/snapshots/ci/qtwebkit/5.212/latest/qtwebkit/ in the flavour you need.

      From your post it looks like it might be worth extra effort and remove the html generator parts completely (if you don't intend to use it, do I get that part right?) and simply use QPdfWriter with QTextDocument - QTextDocument also accepts subset of HTML to do rich text.

      Also worth your time: https://doc.qt.io/qt-5/richtext.html

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      2
      • M Offline
        M Offline
        Marcus Barnet
        wrote on last edited by Marcus Barnet
        #3

        Unfortunately, I can't get rid of the HTML part since the webpage is formatted in a very specific way.

        I need to add an extra step: load the html page saved on the hard drive, convert it to PDF and send it as attachment by email.

        I couldn't find any other smarter way so far.

        After having downloaded the package, should I just build the source code of the wkhtmltopdf library in QT or do I need some extra setup? I'm sorry, but I usually use Linux and I'm not very confident with Windows.

        artwawA 1 Reply Last reply
        0
        • M Marcus Barnet

          Unfortunately, I can't get rid of the HTML part since the webpage is formatted in a very specific way.

          I need to add an extra step: load the html page saved on the hard drive, convert it to PDF and send it as attachment by email.

          I couldn't find any other smarter way so far.

          After having downloaded the package, should I just build the source code of the wkhtmltopdf library in QT or do I need some extra setup? I'm sorry, but I usually use Linux and I'm not very confident with Windows.

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          @Marcus-Barnet maybe first try to import your HTML to QTextDocument? It might be that it will look just right. Worth checking, might save you a bit of a time.

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Marcus Barnet
            wrote on last edited by
            #5

            Thank you! Yes, I could try! I have all the html code store into a QString html_page variable.

            What is the fastest way to convert it to QTextDocument?

            QTextDocument doc;
            doc.setHtml(html_page );
            

            The web page is formatted to fit in a A4 format as it is.
            I couldn't find any simple code to understand how to use QPdfWriter, by the way!

            JonBJ artwawA 2 Replies Last reply
            0
            • JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by JoeCFD
              #6

              Another way is to convert the web page to a xml file which can be used to make web pages. You may be able to find tools to do that. Then it may be easier to convert xml file to PDF or QTextDocument.

              1 Reply Last reply
              0
              • M Marcus Barnet

                Thank you! Yes, I could try! I have all the html code store into a QString html_page variable.

                What is the fastest way to convert it to QTextDocument?

                QTextDocument doc;
                doc.setHtml(html_page );
                

                The web page is formatted to fit in a A4 format as it is.
                I couldn't find any simple code to understand how to use QPdfWriter, by the way!

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

                @Marcus-Barnet
                Be aware that QTextDocument's support for HTML is "limited". To that subset specified by Qt.

                When you talk about

                Unfortunately, I can't get rid of the HTML part since the webpage is formatted in a very specific way.

                it makes me think you may need full, unadulterated HTML support. Like that in a web browser.

                You can use the older Qt webkit that you mention. However, that is no longer supplied with Qt, and you'll likely have to build it yourself. If you do not want to do that, are you aware that Qt replaced that with Qt WebEngine, which can also be used to display a web page and print it, including to PDF?

                M 1 Reply Last reply
                0
                • M Marcus Barnet

                  Thank you! Yes, I could try! I have all the html code store into a QString html_page variable.

                  What is the fastest way to convert it to QTextDocument?

                  QTextDocument doc;
                  doc.setHtml(html_page );
                  

                  The web page is formatted to fit in a A4 format as it is.
                  I couldn't find any simple code to understand how to use QPdfWriter, by the way!

                  artwawA Offline
                  artwawA Offline
                  artwaw
                  wrote on last edited by
                  #8

                  @Marcus-Barnet setHTML() is a way to go.
                  QPdfWriter operates as usual printer, difference is that it produces well formed PDF document.

                  The way to do it is use QTextDocument::drawContents() method, like on the printer. You might need to take care of setting the resolution of the "printer" and so on (when you notice that image is scaled up/down, etc, my usual setting is 300dpi).

                  If that works then it works and you have less work.
                  It it does not... Like @JonB said, only cumbersome solutions like webkit or QtWebEngine.

                  On that note I fully endorse your decision of using lightweight webkit over a heavy cow of web engine. For this particular task should suffice.
                  But I hope you don't have to use any of them.

                  For more information please re-read.

                  Kind Regards,
                  Artur

                  1 Reply Last reply
                  1
                  • JonBJ JonB

                    @Marcus-Barnet
                    Be aware that QTextDocument's support for HTML is "limited". To that subset specified by Qt.

                    When you talk about

                    Unfortunately, I can't get rid of the HTML part since the webpage is formatted in a very specific way.

                    it makes me think you may need full, unadulterated HTML support. Like that in a web browser.

                    You can use the older Qt webkit that you mention. However, that is no longer supplied with Qt, and you'll likely have to build it yourself. If you do not want to do that, are you aware that Qt replaced that with Qt WebEngine, which can also be used to display a web page and print it, including to PDF?

                    M Offline
                    M Offline
                    Marcus Barnet
                    wrote on last edited by
                    #9

                    @JonB said in Convert HTML to PDF:

                    @Marcus-Barnet
                    Be aware that QTextDocument's support for HTML is "limited". To that subset specified by Qt.

                    When you talk about

                    Unfortunately, I can't get rid of the HTML part since the webpage is formatted in a very specific way.

                    it makes me think you may need full, unadulterated HTML support. Like that in a web browser.

                    You can use the older Qt webkit that you mention. However, that is no longer supplied with Qt, and you'll likely have to build it yourself. If you do not want to do that, are you aware that Qt replaced that with Qt WebEngine, which can also be used to display a web page and print it, including to PDF?

                    Yes, it's for this reason that I skipped the QTextDocument months ago.
                    I remember that someone in this forum (may be you) told me that is supports only a limited set of HTML tags while my webpage is highly formatted.

                    I do not think I have the skills to be able to build my myself the package, may be I should try Qt WebEngine, I need to check if there is any code example about that on google.

                    JonBJ artwawA 2 Replies Last reply
                    0
                    • M Marcus Barnet

                      @JonB said in Convert HTML to PDF:

                      @Marcus-Barnet
                      Be aware that QTextDocument's support for HTML is "limited". To that subset specified by Qt.

                      When you talk about

                      Unfortunately, I can't get rid of the HTML part since the webpage is formatted in a very specific way.

                      it makes me think you may need full, unadulterated HTML support. Like that in a web browser.

                      You can use the older Qt webkit that you mention. However, that is no longer supplied with Qt, and you'll likely have to build it yourself. If you do not want to do that, are you aware that Qt replaced that with Qt WebEngine, which can also be used to display a web page and print it, including to PDF?

                      Yes, it's for this reason that I skipped the QTextDocument months ago.
                      I remember that someone in this forum (may be you) told me that is supports only a limited set of HTML tags while my webpage is highly formatted.

                      I do not think I have the skills to be able to build my myself the package, may be I should try Qt WebEngine, I need to check if there is any code example about that on google.

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

                      @Marcus-Barnet
                      Note that @artwaw has just posted before you. He suggests that QPdfWriter (didn't yet exist when I did my stuff for this a few years ago) is preferable to WebKit/Engine, on the cumbersome-ness front. He may well know what he is talking about! But for QTextDocument I'm not sure it will support the raw HTML you seem to have? Maybe @artwaw will comment further?

                      artwawA 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Marcus-Barnet
                        Note that @artwaw has just posted before you. He suggests that QPdfWriter (didn't yet exist when I did my stuff for this a few years ago) is preferable to WebKit/Engine, on the cumbersome-ness front. He may well know what he is talking about! But for QTextDocument I'm not sure it will support the raw HTML you seem to have? Maybe @artwaw will comment further?

                        artwawA Offline
                        artwawA Offline
                        artwaw
                        wrote on last edited by
                        #11

                        @JonB /me will.
                        It all depends on the document. Supported subset is documented here: https://doc.qt.io/qt-5/richtext-html-subset.html

                        If that doesn't work, well... Further up I posted links to binaries of QtWebkit, just download and put into your Qt tree. As for printing - I'd need about half an hour to dig out how I did it with webkit in the past.

                        For more information please re-read.

                        Kind Regards,
                        Artur

                        1 Reply Last reply
                        0
                        • M Marcus Barnet

                          @JonB said in Convert HTML to PDF:

                          @Marcus-Barnet
                          Be aware that QTextDocument's support for HTML is "limited". To that subset specified by Qt.

                          When you talk about

                          Unfortunately, I can't get rid of the HTML part since the webpage is formatted in a very specific way.

                          it makes me think you may need full, unadulterated HTML support. Like that in a web browser.

                          You can use the older Qt webkit that you mention. However, that is no longer supplied with Qt, and you'll likely have to build it yourself. If you do not want to do that, are you aware that Qt replaced that with Qt WebEngine, which can also be used to display a web page and print it, including to PDF?

                          Yes, it's for this reason that I skipped the QTextDocument months ago.
                          I remember that someone in this forum (may be you) told me that is supports only a limited set of HTML tags while my webpage is highly formatted.

                          I do not think I have the skills to be able to build my myself the package, may be I should try Qt WebEngine, I need to check if there is any code example about that on google.

                          artwawA Offline
                          artwawA Offline
                          artwaw
                          wrote on last edited by artwaw
                          #12

                          @Marcus-Barnet Got it. Not overly complicated.

                          I write this under the assumption that you do not need to display the page.
                          Once you have QtWebkit installed, you take QWebPage, usual QWebPage page; or something.

                          Important concept with webkit is that (in our context) page manipulation like printing is not done on QWebPage object but on frames. Each QWebPage has at least one frame accessible via mainFrame() method. That is the key.

                          So, to set the content we do page->mainFrame()->setHtml(QString html);
                          To print, we need to setup the printer (plot twist! NOT QPdfWriter) and simply call page->mainFrame()->print(QPrinter *printer);
                          To print to pdf from QPrinter you need to call during the setup QPrinter::setOutputFormat(QPrinter::PdfFormat); and QPrinter::setOutputFileName(QString fname);

                          Documentation is here:

                          https://qtwebkit.github.io/doc/qtwebkit/qwebpage.html
                          https://qtwebkit.github.io/doc/qtwebkit/qwebframe.htm
                          https://doc.qt.io/qt-5/qprinter.html

                          At least that's what I did in my last project and I have to say it worked rather well.

                          The reason I opt so often towards QtWebkit is that for solutions like this it is really lightweight and fast. Say, for headless processing or static html. For full fledged web applications it might be not enough but for like the above? Ideal.

                          Have fun!

                          Edit: I corrected calls to QWebFrame methods since I forgot that mainFrame() returns pointer.

                          For more information please re-read.

                          Kind Regards,
                          Artur

                          1 Reply Last reply
                          1
                          • M Offline
                            M Offline
                            Marcus Barnet
                            wrote on last edited by
                            #13

                            Thank you a lot! I'll check everything this evening and I'll let you know if it worked!

                            1 Reply Last reply
                            0
                            • JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #14

                              You can do the same as @artwaw's approach of using PDF printer from QWebEnginePage. Absolutely nothing wrong with QtWebKit QWebPage. Just I don't compile anything Qt, just so you know if you have any compile problems.

                              artwawA 1 Reply Last reply
                              0
                              • JonBJ JonB

                                You can do the same as @artwaw's approach of using PDF printer from QWebEnginePage. Absolutely nothing wrong with QtWebKit QWebPage. Just I don't compile anything Qt, just so you know if you have any compile problems.

                                artwawA Offline
                                artwawA Offline
                                artwaw
                                wrote on last edited by
                                #15

                                @JonB side note and part explanation: for me QWebEngine is a no go on Windows as it requires Microsoft environment. I never managed to set it up properly, for some reason I saw my programs working well on mingw failing to compile I checked no further. Call me spoiled but...

                                For more information please re-read.

                                Kind Regards,
                                Artur

                                1 Reply Last reply
                                1
                                • JoeCFDJ Offline
                                  JoeCFDJ Offline
                                  JoeCFD
                                  wrote on last edited by
                                  #16

                                  https://doc.qt.io/qt-5/qtwebengine-webenginewidgets-html2pdf-example.html
                                  does this one help?

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    Marcus Barnet
                                    wrote on last edited by
                                    #17

                                    Thank you to all, guys, for your support!

                                    Unfortunately, I think using qtwebkit requires some skills, since I downloaded this one: https://download.qt.io/snapshots/ci/qtwebkit/5.212/latest/qtwebkit/qtwebkit-Windows-Windows_10-MSVC2019-Windows-Windows_10-X86_64.7z and I got a folder that I moved to my QT directory:

                                    C:\Qt\5.15.0\qtwebkit-Windows-Windows_10-MSVC2019-Windows-Windows_10-X86_64
                                    
                                    08/06/2021  22:50    <DIR>          .
                                    08/06/2021  22:50    <DIR>          ..
                                    08/06/2021  22:50    <DIR>          bin
                                    08/06/2021  22:50    <DIR>          include
                                    08/06/2021  22:50    <DIR>          lib
                                    24/09/2020  14:15    <DIR>          mkspecs
                                    08/06/2021  22:50    <DIR>          qml
                                    

                                    and I do not know how to proceed from this step. I tried to search on the forum, but I didn't found any topic about this (only one, but it was for Linux while I'm on Windows 10).
                                    My current program uses msvc2019_64.

                                    I can't understand how I can build this package.

                                    artwawA 1 Reply Last reply
                                    0
                                    • M Marcus Barnet

                                      Thank you to all, guys, for your support!

                                      Unfortunately, I think using qtwebkit requires some skills, since I downloaded this one: https://download.qt.io/snapshots/ci/qtwebkit/5.212/latest/qtwebkit/qtwebkit-Windows-Windows_10-MSVC2019-Windows-Windows_10-X86_64.7z and I got a folder that I moved to my QT directory:

                                      C:\Qt\5.15.0\qtwebkit-Windows-Windows_10-MSVC2019-Windows-Windows_10-X86_64
                                      
                                      08/06/2021  22:50    <DIR>          .
                                      08/06/2021  22:50    <DIR>          ..
                                      08/06/2021  22:50    <DIR>          bin
                                      08/06/2021  22:50    <DIR>          include
                                      08/06/2021  22:50    <DIR>          lib
                                      24/09/2020  14:15    <DIR>          mkspecs
                                      08/06/2021  22:50    <DIR>          qml
                                      

                                      and I do not know how to proceed from this step. I tried to search on the forum, but I didn't found any topic about this (only one, but it was for Linux while I'm on Windows 10).
                                      My current program uses msvc2019_64.

                                      I can't understand how I can build this package.

                                      artwawA Offline
                                      artwawA Offline
                                      artwaw
                                      wrote on last edited by
                                      #18

                                      @Marcus-Barnet It should be already build. Just reopen QtCreator and add Qt += webkit in your .pro file.
                                      I have also Qt += webkitwidgets but I don't think you need them.

                                      At any rate you can then do #include <QWebPage> in the header files.

                                      For more information please re-read.

                                      Kind Regards,
                                      Artur

                                      1 Reply Last reply
                                      1
                                      • M Offline
                                        M Offline
                                        Marcus Barnet
                                        wrote on last edited by
                                        #19

                                        where should I copy the files contained in the archive I downloaded?

                                        This is the path where my qmake is:

                                        C:\Qt\5.15.0\msvc2019_64\bin\qmake.exe
                                        

                                        should I copy the content of:

                                        C:\Qt\5.15.0\qtwebkit-Windows-Windows_10-MSVC2019-Windows-Windows_10-X86_64
                                        08/06/2021  22:50    <DIR>          .
                                        08/06/2021  22:50    <DIR>          ..
                                        08/06/2021  22:50    <DIR>          bin
                                        08/06/2021  22:50    <DIR>          include
                                        08/06/2021  22:50    <DIR>          lib
                                        24/09/2020  14:15    <DIR>          mkspecs
                                        08/06/2021  22:50    <DIR>          qml
                                        

                                        in this directory?

                                        C:\Qt\5.15.0\msvc2019_64
                                        
                                        19/07/2020  21:36    <DIR>          .
                                        19/07/2020  21:36    <DIR>          ..
                                        29/07/2020  10:15    <DIR>          bin
                                        19/07/2020  21:34    <DIR>          doc
                                        19/07/2020  21:36    <DIR>          include
                                        19/07/2020  21:39    <DIR>          lib
                                        19/07/2020  21:34    <DIR>          mkspecs
                                        19/07/2020  21:34    <DIR>          phrasebooks
                                        19/07/2020  21:36    <DIR>          plugins
                                        19/07/2020  21:36    <DIR>          qml
                                        19/07/2020  21:36    <DIR>          resources
                                        19/07/2020  21:36    <DIR>          translations
                                        

                                        Sorry for the stupid question, but I can't understand how to use these files.
                                        Usually, in linux, there is /src and /lib folders that you can pass as parameter to the compiler.

                                        artwawA 1 Reply Last reply
                                        0
                                        • M Marcus Barnet

                                          where should I copy the files contained in the archive I downloaded?

                                          This is the path where my qmake is:

                                          C:\Qt\5.15.0\msvc2019_64\bin\qmake.exe
                                          

                                          should I copy the content of:

                                          C:\Qt\5.15.0\qtwebkit-Windows-Windows_10-MSVC2019-Windows-Windows_10-X86_64
                                          08/06/2021  22:50    <DIR>          .
                                          08/06/2021  22:50    <DIR>          ..
                                          08/06/2021  22:50    <DIR>          bin
                                          08/06/2021  22:50    <DIR>          include
                                          08/06/2021  22:50    <DIR>          lib
                                          24/09/2020  14:15    <DIR>          mkspecs
                                          08/06/2021  22:50    <DIR>          qml
                                          

                                          in this directory?

                                          C:\Qt\5.15.0\msvc2019_64
                                          
                                          19/07/2020  21:36    <DIR>          .
                                          19/07/2020  21:36    <DIR>          ..
                                          29/07/2020  10:15    <DIR>          bin
                                          19/07/2020  21:34    <DIR>          doc
                                          19/07/2020  21:36    <DIR>          include
                                          19/07/2020  21:39    <DIR>          lib
                                          19/07/2020  21:34    <DIR>          mkspecs
                                          19/07/2020  21:34    <DIR>          phrasebooks
                                          19/07/2020  21:36    <DIR>          plugins
                                          19/07/2020  21:36    <DIR>          qml
                                          19/07/2020  21:36    <DIR>          resources
                                          19/07/2020  21:36    <DIR>          translations
                                          

                                          Sorry for the stupid question, but I can't understand how to use these files.
                                          Usually, in linux, there is /src and /lib folders that you can pass as parameter to the compiler.

                                          artwawA Offline
                                          artwawA Offline
                                          artwaw
                                          wrote on last edited by
                                          #20

                                          @Marcus-Barnet yes. exactly there, all folders' content to the respective folders.

                                          For more information please re-read.

                                          Kind Regards,
                                          Artur

                                          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 Extensions
                                          • Unsolved