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
QtWS25 Last Chance

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, 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 Online
                      J Online
                      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 Online
                          J Online
                          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
                              • A artwaw
                                12 Jun 2021, 12:55

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

                                M Offline
                                M Offline
                                Marcus Barnet
                                wrote on 13 Jun 2021, 17:09 last edited by
                                #32

                                @artwaw said in Convert HTML to PDF:

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

                                I really do not know what to say, but I tried your code and it doesn't work for me!
                                It doesn't display the image in the PDF file.

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  Marcus Barnet
                                  wrote on 13 Jun 2021, 17:59 last edited by
                                  #33

                                  it's strange because everything else works fine, but the images are not displayed.

                                  1 Reply Last reply
                                  0
                                  • A AlexMaly
                                    11 Jun 2021, 22:23

                                    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 Offline
                                    M Offline
                                    Marcus Barnet
                                    wrote on 13 Jun 2021, 18:11 last edited by
                                    #34

                                    @AlexMaly said in Convert HTML to PDF:

                                    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.

                                    This solution works and it is able to generate the PDF by including also the images, but I can't scale the result PDF.
                                    I need to fit all the content in a A4 format and to do so I need to scale the print to 93%. It seems that chrome doesn't provide this feature.

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      Marcus Barnet
                                      wrote on 13 Jun 2021, 20:42 last edited by
                                      #35

                                      At the moment, I solved the conversion problem by using wkhtmltopdf.

                                      However, I was trying to generated a PDF file to be able to send it by email as an attachment since sometimes the HTML webpages are blocked by the antivirus (I guess) and they cannot be read by the user.
                                      I was thinking to solve the problem by sending PDF files instead of HTML, but the attachments keep to be blocked on some smartphones!!

                                      A 1 Reply Last reply 14 Jun 2021, 11:53
                                      0
                                      • M Marcus Barnet
                                        13 Jun 2021, 20:42

                                        At the moment, I solved the conversion problem by using wkhtmltopdf.

                                        However, I was trying to generated a PDF file to be able to send it by email as an attachment since sometimes the HTML webpages are blocked by the antivirus (I guess) and they cannot be read by the user.
                                        I was thinking to solve the problem by sending PDF files instead of HTML, but the attachments keep to be blocked on some smartphones!!

                                        A Offline
                                        A Offline
                                        artwaw
                                        wrote on 14 Jun 2021, 11:53 last edited by
                                        #36

                                        @Marcus-Barnet That, I believe, is a problem with settings of the said smartphone mail client or account and I am not sure if you can do anything to prevent that.
                                        Ugly solution might be to ditch pdf and render a4 size png image and include that as an attachment (but I am totally not sure if that would work with mail filters).

                                        For more information please re-read.

                                        Kind Regards,
                                        Artur

                                        1 Reply Last reply
                                        0
                                        • M Offline
                                          M Offline
                                          Marcus Barnet
                                          wrote on 14 Jun 2021, 14:04 last edited by
                                          #37

                                          But it's strange because if I sent the same PDF file as attachment by using my mail client, then they receive it.

                                          The problem occurs only when I send it by my QT application.

                                          A 1 Reply Last reply 14 Jun 2021, 15:01
                                          0

                                          27/48

                                          12 Jun 2021, 07:55

                                          • Login

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