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. How to print a simple HTML table with QWebView and QPrinter without cutting off the right side?

How to print a simple HTML table with QWebView and QPrinter without cutting off the right side?

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 6 Posters 5.1k Views 4 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.
  • P Offline
    P Offline
    Piotr S.
    wrote on last edited by
    #1

    Since my update from Qt 4.7.4 to Qt 4.8.7 it isn't possible for me to print a simple HTML table without cutting off the right side. Here is the code that shows my HTML table:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QWebView *webView = new QWebView();
        webView->load(QUrl("table.html"));
    
        QPrinter *printer = new QPrinter();
        printer->setPaperSize(QPrinter::A4);
    
        QPrintPreviewDialog *printPreview = new QPrintPreviewDialog(printer);
        QObject::connect(printPreview, SIGNAL(paintRequested(QPrinter*)), webView, SLOT(print(QPrinter*)));
    
        a.processEvents();
        printPreview->show();
    
        return a.exec();
    }
    

    This is my simple HTML table:

    <!DOCTYPE html>  
    <html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">    
        <style>
            table {
                width:100%;
                border-collapse:collapse;
            }
            table, td, tr, th{
                border:0.091em solid grey;                 
            }                        
        </style>
    </head>
    
    <body>
     <table>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>Lastname</th>
        <th>ENDENAME</th>
    
      </tr>
      <tr>
        <td>Jill</td>
        <td>Smith</td>
        <td>Smith</td>    
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>Smith</td>
        <td>ENDE</td>
      </tr>
    </table>
    </body> 
    </html>
    

    And this is the ugly result with Qt 4.8.7:

    alt text

    This was the result with the same table and code in Qt 4.7.4:

    alt text

    What can I do to solve this issue? I think there is some strange bug in qtwebkit 2.x. I hope someone could help me. Thank you.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Did you try using your own font ?
      And set a size.
      It seems the font is bigger in 4.8

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Piotr S.
        wrote on last edited by
        #3

        Yes, it's bigger because the webkit versions have different default fonts. And yes I tried different font sizes etc. Sure, I can shrink the table size (e.g. via a smaller font size) to manually fit onto the page. But this is not a solution for this issue. I have dynamic generated HTML tables of different widths and the table should always fit on my A4 page (via automatic scaling by QtWebView/QtWebFrame::print). This is the normal behavior in every modern browser if you try to print a wide HTML table and it is the old behavior of Qt 4.7.4 (QtWebkit 1.x).

        mrjjM 1 Reply Last reply
        0
        • P Piotr S.

          Yes, it's bigger because the webkit versions have different default fonts. And yes I tried different font sizes etc. Sure, I can shrink the table size (e.g. via a smaller font size) to manually fit onto the page. But this is not a solution for this issue. I have dynamic generated HTML tables of different widths and the table should always fit on my A4 page (via automatic scaling by QtWebView/QtWebFrame::print). This is the normal behavior in every modern browser if you try to print a wide HTML table and it is the old behavior of Qt 4.7.4 (QtWebkit 1.x).

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Piotr-S.
          Well i guess its a bug maybe in v2 or some of the logic changed.
          Using 1.x seems best option then.

          P 1 Reply Last reply
          0
          • mrjjM mrjj

            @Piotr-S.
            Well i guess its a bug maybe in v2 or some of the logic changed.
            Using 1.x seems best option then.

            P Offline
            P Offline
            Piotr S.
            wrote on last edited by
            #5

            @mrjj Unfortunately I need Qt 4.8.7. and downgrading to Qt 4.7.4 is not an option for me. So you want to tell me that Qt 4.8.7. is not able to print a simple HTML table? Maybe there is some workaround? I hope someone in this forum has a solution for me. Thank you.

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi
              Oh. well there might be bugs reports on
              https://bugreports.qt.io
              or give a day or two to see if some have work around.
              We can hope :)

              P 1 Reply Last reply
              0
              • sneubertS Offline
                sneubertS Offline
                sneubert
                wrote on last edited by
                #7

                Maybe a solution would be to use a QTextDocument. You can use setHTML to insert your table and print to a QPrinter where you setOutputFormat(QPrinter::PdfFormat). The supporte html subset of QTextDocument can be found here (http://doc.qt.io/qt-4.8/richtext-html-subset.html).

                P 1 Reply Last reply
                1
                • sneubertS sneubert

                  Maybe a solution would be to use a QTextDocument. You can use setHTML to insert your table and print to a QPrinter where you setOutputFormat(QPrinter::PdfFormat). The supporte html subset of QTextDocument can be found here (http://doc.qt.io/qt-4.8/richtext-html-subset.html).

                  P Offline
                  P Offline
                  Piotr S.
                  wrote on last edited by Piotr S.
                  #8

                  @sneubert Yes I tried this solution. Unfortunately the supported HTML subset is too small for my tables.

                  1 Reply Last reply
                  0
                  • mrjjM mrjj

                    Hi
                    Oh. well there might be bugs reports on
                    https://bugreports.qt.io
                    or give a day or two to see if some have work around.
                    We can hope :)

                    P Offline
                    P Offline
                    Piotr S.
                    wrote on last edited by
                    #9

                    @mrjj Yeah. I already tried to find a bug report but I can't find one that describes my problem.

                    mrjjM 1 Reply Last reply
                    0
                    • P Piotr S.

                      @mrjj Yeah. I already tried to find a bug report but I can't find one that describes my problem.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Piotr-S.
                      nah. Not really. I also had a fast search.
                      I dont have webkit for 5.7 installed so hard to test if its was later fixed but
                      i doubt it.

                      VRoninV 1 Reply Last reply
                      0
                      • sneubertS Offline
                        sneubertS Offline
                        sneubert
                        wrote on last edited by
                        #11

                        The only option I see is to create a custom slot for paintRequested signal and use QPainter with QWebFrame::renderand zoomFactor. But this might be a nasty task.

                        1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @Piotr-S.
                          nah. Not really. I also had a fast search.
                          I dont have webkit for 5.7 installed so hard to test if its was later fixed but
                          i doubt it.

                          VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #12

                          @mrjj said in How to print a simple HTML table with QWebView and QPrinter without cutting off the right side?:

                          I dont have webkit for 5.7 installed so hard to test if its was later fixed but
                          i doubt it.

                          Qt 5.5 still affected

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          1 Reply Last reply
                          1
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            Hi,

                            @Konstantin-Tokarev QtWebKit reboot might be worth a try.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            1
                            • K Offline
                              K Offline
                              Konstantin Tokarev
                              wrote on last edited by
                              #14

                              I would be very surprised if it works (provided Qt version is the same), but it would be better if you tried anyway :)

                              1 Reply Last reply
                              0

                              • Login

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