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



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


  • Qt Champions 2016

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



  • 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).


  • Qt Champions 2016

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



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


  • Qt Champions 2016

    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 :)



  • 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).



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



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


  • Qt Champions 2016

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



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



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


  • Lifetime Qt Champion

    Hi,

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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.