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

Convert HTML to PDF

Scheduled Pinned Locked Moved Unsolved General and Desktop
48 Posts 9 Posters 9.2k Views
  • 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 Marcus Barnet
    8 Jun 2021, 15:14

    @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.

    A Offline
    A Offline
    artwaw
    wrote on 8 Jun 2021, 16:00 last edited by artwaw 6 Aug 2021, 16:01
    #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 8 Jun 2021, 16:48 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
      • J Offline
        J Offline
        JonB
        wrote on 8 Jun 2021, 17:37 last edited by JonB 6 Aug 2021, 17:37
        #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.

        A 1 Reply Last reply 8 Jun 2021, 18:00
        0
        • J JonB
          8 Jun 2021, 17:37

          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.

          A Offline
          A Offline
          artwaw
          wrote on 8 Jun 2021, 18:00 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
          • J Offline
            J Offline
            JoeCFD
            wrote on 8 Jun 2021, 18:02 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 8 Jun 2021, 21:08 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.

              A 1 Reply Last reply 9 Jun 2021, 08:09
              0
              • M Marcus Barnet
                8 Jun 2021, 21:08

                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.

                A Offline
                A Offline
                artwaw
                wrote on 9 Jun 2021, 08:09 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 9 Jun 2021, 13:11 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.

                  A 1 Reply Last reply 9 Jun 2021, 13:13
                  0
                  • M Marcus Barnet
                    9 Jun 2021, 13:11

                    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.

                    A Offline
                    A Offline
                    artwaw
                    wrote on 9 Jun 2021, 13:13 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
                    • M Offline
                      M Offline
                      Marcus Barnet
                      wrote on 9 Jun 2021, 14:25 last edited by
                      #21

                      thank you, I did as you suggested and now I get no errors when I try to call QWebPage.

                      Later, this evening, I will try to use it for the PDF and I will update you.

                      1 Reply Last reply
                      2
                      • M Offline
                        M Offline
                        Marcus Barnet
                        wrote on 9 Jun 2021, 21:50 last edited by Marcus Barnet 6 Sept 2021, 23:15
                        #22

                        I tried to convert the html to pdf, it works fine even if this process doesn't include a PNG image which is included in the HTML page.

                        Unfortunately, this image is very important since it is a logo which must be included also in the PDF file. (I use the <img> tag to insert the image)
                        Is there any specific reason for not including the image?

                        Moreover, I noticed that the table borders are displayed in grey color while they should be black.
                        This is the code I'm using:

                          ```
                               QWebPage page;
                               page.mainFrame()->setHtml(html_scheda);
                        
                               QPrinter printer(QPrinter::HighResolution);
                               printer.setOrientation(QPrinter::Portrait);
                               printer.setOutputFormat(QPrinter::PdfFormat);
                               printer.setPaperSize(QPrinter::A4);
                               printer.setPageMargins(QMarginsF(5, 5, 5, 5));
                               QString fileName = "C:/folder/test.pdf";
                               printer.setOutputFileName(fileName);
                        
                               page.mainFrame()->print(&printer);
                        
                        A 1 Reply Last reply 10 Jun 2021, 08:28
                        0
                        • M Marcus Barnet
                          9 Jun 2021, 21:50

                          I tried to convert the html to pdf, it works fine even if this process doesn't include a PNG image which is included in the HTML page.

                          Unfortunately, this image is very important since it is a logo which must be included also in the PDF file. (I use the <img> tag to insert the image)
                          Is there any specific reason for not including the image?

                          Moreover, I noticed that the table borders are displayed in grey color while they should be black.
                          This is the code I'm using:

                            ```
                                 QWebPage page;
                                 page.mainFrame()->setHtml(html_scheda);
                          
                                 QPrinter printer(QPrinter::HighResolution);
                                 printer.setOrientation(QPrinter::Portrait);
                                 printer.setOutputFormat(QPrinter::PdfFormat);
                                 printer.setPaperSize(QPrinter::A4);
                                 printer.setPageMargins(QMarginsF(5, 5, 5, 5));
                                 QString fileName = "C:/folder/test.pdf";
                                 printer.setOutputFileName(fileName);
                          
                                 page.mainFrame()->print(&printer);
                          
                          A Offline
                          A Offline
                          artwaw
                          wrote on 10 Jun 2021, 08:28 last edited by
                          #23

                          @Marcus-Barnet from documentation: External objects referenced in the content are located relative to baseUrl. I'd search in that direction.

                          For more information please re-read.

                          Kind Regards,
                          Artur

                          M 1 Reply Last reply 11 Jun 2021, 21:08
                          0
                          • A artwaw
                            10 Jun 2021, 08:28

                            @Marcus-Barnet from documentation: External objects referenced in the content are located relative to baseUrl. I'd search in that direction.

                            M Offline
                            M Offline
                            Marcus Barnet
                            wrote on 11 Jun 2021, 21:08 last edited by
                            #24

                            @artwaw said in Convert HTML to PDF:

                            @Marcus-Barnet from documentation: External objects referenced in the content are located relative to baseUrl. I'd search in that direction.

                            The problem is that the logo is loaded from the network, so I do not know what kind of base_url I should specify.

                            This is the HTML where I load the picture:

                            html_scheda = "<table align=\"center\" border=\"0\" style=\"border-collapse: collapse; width: 21cm; height: 29 cm;\"> <tbody> <tr> <td style=\"width: 100%;\"> <table border=\"0\" style=\"height: 146px; width: 100%; border-collapse: collapse; margin-left: auto; margin-right: auto;\"> <tbody> <tr style=\"height: 0px;\"> <td style=\"width: 50%; height: 146px; text-align: center;\" rowspan=\"4\"><img width=\"200\" align=\"center\" src=\"http://www.robo-dyne.com/pics/pump_logo.png\" alt=\"\" /></td>
                            

                            What should I use as baseurl in this situation?

                            A 2 Replies Last reply 12 Jun 2021, 12:28
                            0
                            • A Offline
                              A Offline
                              AlexMaly
                              wrote on 11 Jun 2021, 22:23 last edited by
                              #25

                              We had the same problem and while webkit is viable option it does not support everything we needed.
                              We tried a few options, but the best result was produced with using chrome:
                              It can do it in headless mode
                              google-chrome --headless --disable-gpu --print-to-pdf=file1.pdf http://www.example.com/

                              So if you could make sure chrome is installed on your system, this would be an approach I recommend.

                              M 1 Reply Last reply 13 Jun 2021, 18:11
                              0
                              • M Offline
                                M Offline
                                Marcus Barnet
                                wrote on 12 Jun 2021, 06:40 last edited by
                                #26

                                Does this work also on Windows?

                                Even if I would prefer to use QT functionalities to export the PDF.

                                J 1 Reply Last reply 12 Jun 2021, 07:55
                                0
                                • M Marcus Barnet
                                  12 Jun 2021, 06:40

                                  Does this work also on Windows?

                                  Even if I would prefer to use QT functionalities to export the PDF.

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 12 Jun 2021, 07:55 last edited by JonB 6 Dec 2021, 07:56
                                  #27

                                  @Marcus-Barnet
                                  I will throw this in. I do not know if it is right/will help, but....

                                  I previously suggested you use the supported/supplied Qt WebEngine, not Qt WebKit. But for whatever reason you have gone down the WebKit route :) I do not compile anything Qt (Linux). For Windows I do not know whether you have to compile or whether you download already-built. I gather WebEngine requires MSVC, not MinGW, but that seems to be what you are using anyway.

                                  Web Engine uses the chromium engine. From @AlexMaly's post, I'm thinking/hoping this would do the necessary "chrome" facility for you, and access external URLs without problem?

                                  You would have to check. I leave you with this possibility....

                                  M 1 Reply Last reply 12 Jun 2021, 11:05
                                  0
                                  • J JonB
                                    12 Jun 2021, 07:55

                                    @Marcus-Barnet
                                    I will throw this in. I do not know if it is right/will help, but....

                                    I previously suggested you use the supported/supplied Qt WebEngine, not Qt WebKit. But for whatever reason you have gone down the WebKit route :) I do not compile anything Qt (Linux). For Windows I do not know whether you have to compile or whether you download already-built. I gather WebEngine requires MSVC, not MinGW, but that seems to be what you are using anyway.

                                    Web Engine uses the chromium engine. From @AlexMaly's post, I'm thinking/hoping this would do the necessary "chrome" facility for you, and access external URLs without problem?

                                    You would have to check. I leave you with this possibility....

                                    M Offline
                                    M Offline
                                    Marcus Barnet
                                    wrote on 12 Jun 2021, 11:05 last edited by
                                    #28

                                    @JonB said in Convert HTML to PDF:

                                    @Marcus-Barnet
                                    I will throw this in. I do not know if it is right/will help, but....

                                    I previously suggested you use the supported/supplied Qt WebEngine, not Qt WebKit. But for whatever reason you have gone down the WebKit route :) I do not compile anything Qt (Linux). For Windows I do not know whether you have to compile or whether you download already-built. I gather WebEngine requires MSVC, not MinGW, but that seems to be what you are using anyway.

                                    Web Engine uses the chromium engine. From @AlexMaly's post, I'm thinking/hoping this would do the necessary "chrome" facility for you, and access external URLs without problem?

                                    You would have to check. I leave you with this possibility....

                                    I didn't use WebEngine because I thought that WebKit was easier! But it is not working as expected, so I'll give it a try! :)
                                    I think I should use this one: https://doc.qt.io/qt-5/qtwebengine-webenginewidgets-html2pdf-example.html as an example and make it fit my needs, is it correct?

                                    Thank you!

                                    J 1 Reply Last reply 12 Jun 2021, 11:11
                                    0
                                    • M Marcus Barnet
                                      12 Jun 2021, 11:05

                                      @JonB said in Convert HTML to PDF:

                                      @Marcus-Barnet
                                      I will throw this in. I do not know if it is right/will help, but....

                                      I previously suggested you use the supported/supplied Qt WebEngine, not Qt WebKit. But for whatever reason you have gone down the WebKit route :) I do not compile anything Qt (Linux). For Windows I do not know whether you have to compile or whether you download already-built. I gather WebEngine requires MSVC, not MinGW, but that seems to be what you are using anyway.

                                      Web Engine uses the chromium engine. From @AlexMaly's post, I'm thinking/hoping this would do the necessary "chrome" facility for you, and access external URLs without problem?

                                      You would have to check. I leave you with this possibility....

                                      I didn't use WebEngine because I thought that WebKit was easier! But it is not working as expected, so I'll give it a try! :)
                                      I think I should use this one: https://doc.qt.io/qt-5/qtwebengine-webenginewidgets-html2pdf-example.html as an example and make it fit my needs, is it correct?

                                      Thank you!

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 12 Jun 2021, 11:11 last edited by
                                      #29

                                      @Marcus-Barnet said in Convert HTML to PDF:

                                      I think I should use this one: https://doc.qt.io/qt-5/qtwebengine-webenginewidgets-html2pdf-example.html as an example and make it fit my needs, is it correct?

                                      Indeed. I didn't know that example existed. Looks like what you are wanting to do, so seems a good starting point!

                                      1 Reply Last reply
                                      0
                                      • M Marcus Barnet
                                        11 Jun 2021, 21:08

                                        @artwaw said in Convert HTML to PDF:

                                        @Marcus-Barnet from documentation: External objects referenced in the content are located relative to baseUrl. I'd search in that direction.

                                        The problem is that the logo is loaded from the network, so I do not know what kind of base_url I should specify.

                                        This is the HTML where I load the picture:

                                        html_scheda = "<table align=\"center\" border=\"0\" style=\"border-collapse: collapse; width: 21cm; height: 29 cm;\"> <tbody> <tr> <td style=\"width: 100%;\"> <table border=\"0\" style=\"height: 146px; width: 100%; border-collapse: collapse; margin-left: auto; margin-right: auto;\"> <tbody> <tr style=\"height: 0px;\"> <td style=\"width: 50%; height: 146px; text-align: center;\" rowspan=\"4\"><img width=\"200\" align=\"center\" src=\"http://www.robo-dyne.com/pics/pump_logo.png\" alt=\"\" /></td>
                                        

                                        What should I use as baseurl in this situation?

                                        A Offline
                                        A Offline
                                        artwaw
                                        wrote on 12 Jun 2021, 12:28 last edited by
                                        #30

                                        @Marcus-Barnet I will try to build basic webkit browser and check that for you later today. You got me curious. To all my knowledge and experience this should work just fine.

                                        For more information please re-read.

                                        Kind Regards,
                                        Artur

                                        1 Reply Last reply
                                        0
                                        • M Marcus Barnet
                                          11 Jun 2021, 21:08

                                          @artwaw said in Convert HTML to PDF:

                                          @Marcus-Barnet from documentation: External objects referenced in the content are located relative to baseUrl. I'd search in that direction.

                                          The problem is that the logo is loaded from the network, so I do not know what kind of base_url I should specify.

                                          This is the HTML where I load the picture:

                                          html_scheda = "<table align=\"center\" border=\"0\" style=\"border-collapse: collapse; width: 21cm; height: 29 cm;\"> <tbody> <tr> <td style=\"width: 100%;\"> <table border=\"0\" style=\"height: 146px; width: 100%; border-collapse: collapse; margin-left: auto; margin-right: auto;\"> <tbody> <tr style=\"height: 0px;\"> <td style=\"width: 50%; height: 146px; text-align: center;\" rowspan=\"4\"><img width=\"200\" align=\"center\" src=\"http://www.robo-dyne.com/pics/pump_logo.png\" alt=\"\" /></td>
                                          

                                          What should I use as baseurl in this situation?

                                          A Offline
                                          A Offline
                                          artwaw
                                          wrote on 12 Jun 2021, 12:55 last edited by artwaw 6 Dec 2021, 13:11
                                          #31

                                          @Marcus-Barnet Hi,
                                          I got some spare time right now and this is the result:
                                          07303d40-b166-4486-b49c-fb02ccc1fe67-image.png

                                          Code:

                                          #ifndef MAINWINDOW_H
                                          #define MAINWINDOW_H
                                          
                                          #include <QMainWindow>
                                          #include <QtWebKit>
                                          #include <QtWebKitWidgets>
                                          #include <QtPrintSupport>
                                          
                                          QT_BEGIN_NAMESPACE
                                          namespace Ui { class MainWindow; }
                                          QT_END_NAMESPACE
                                          
                                          class MainWindow : public QMainWindow
                                          {
                                              Q_OBJECT
                                          
                                          public:
                                              MainWindow(QWidget *parent = nullptr);
                                              ~MainWindow();
                                          
                                          private:
                                              Ui::MainWindow *ui;
                                              QWebPage *page;
                                          };
                                          #endif // MAINWINDOW_H
                                          
                                          #include "mainwindow.h"
                                          #include "ui_mainwindow.h"
                                          
                                          MainWindow::MainWindow(QWidget *parent)
                                              : QMainWindow(parent)
                                              , ui(new Ui::MainWindow)
                                          {
                                              ui->setupUi(this);
                                              page = new QWebPage(this);
                                              page->mainFrame()->setHtml("<table align=\"center\" border=\"0\" style=\"border-collapse: collapse; width: 21cm; height: 29 cm;\"> <tbody> <tr> <td style=\"width: 100%;\"> <table border=\"0\" style=\"height: 146px; width: 100%; border-collapse: collapse; margin-left: auto; margin-right: auto;\"> <tbody> <tr style=\"height: 0px;\"> <td style=\"width: 50%; height: 146px; text-align: center;\" rowspan=\"4\"><img width=\"200\" align=\"center\" src=\"http://www.robo-dyne.com/pics/pump_logo.png\" alt=\"\" /></td>");
                                              ui->view->setPage(page);
                                              QPrinter *printer = new QPrinter(QPrinter::HighResolution);
                                              printer->setPageSize(QPrinter::A4);
                                              printer->setOutputFormat(QPrinter::PdfFormat);
                                              printer->setOutputFileName(QFileDialog::getSaveFileName(this,"Save location",QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)));
                                              page->mainFrame()->print(printer);
                                              delete printer;
                                          }
                                          
                                          MainWindow::~MainWindow()
                                          {
                                              delete ui;
                                          }
                                          
                                          QT       += core gui webkit webkitwidgets printsupport
                                          
                                          greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                                          
                                          CONFIG += c++11
                                          CONFIG +=s dk_no_version_check
                                          
                                          # You can make your code fail to compile if it uses deprecated APIs.
                                          # In order to do so, uncomment the following line.
                                          #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                                          
                                          SOURCES += \
                                              main.cpp \
                                              mainwindow.cpp
                                          
                                          HEADERS += \
                                              mainwindow.h
                                          
                                          FORMS += \
                                              mainwindow.ui
                                          

                                          Resulting pdf file is here: https://trollnet.com.pl/dwn/webkitTest.pdf

                                          I'd say it works?

                                          EDIT:
                                          UI has one QWidget in the grid layout promoted to QWebView (named, surprisingly, view).
                                          I did additional tests where I removed the displayed part (which was added only to test the page anyway) and resulting pdf file is exactly the same, so we can exclude presence of QWebView form having any impact. QWebPage rendering and printout works perfectly fine with the sample you provided.

                                          For more information please re-read.

                                          Kind Regards,
                                          Artur

                                          M 1 Reply Last reply 13 Jun 2021, 17:09
                                          1

                                          21/48

                                          9 Jun 2021, 14:25

                                          • Login

                                          • Login or register to search.
                                          21 out of 48
                                          • First post
                                            21/48
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved