Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

HTML tables cut in half on print to pdf



  • Hi all. Sorry to bother again. I'm having an issue with printing to pdf from a QWebEngineView to a QPrinter. The code is as follows:

    void PrintConfigurationDialog::onPreviewButtonClicked()
    {
        m_printer = new QPrinter(QPrinter::ScreenResolution);
        m_printer->setPaperSize(QPrinter::A4);
        m_printer->setFullPage(true);
    
        QPrintDialog printDialog(m_printer, this);
        if (printDialog.exec() != QDialog::Accepted) {
            delete m_printer;
            m_printer = nullptr;
            return;
        }
    
        // OK: print full doc
        m_webEnginePage->page()->print(m_printer, [this] (bool result) {
            // ignore the result for now. Decide what to do with it later on
            delete m_printer;
            m_printer = nullptr;
        });
    }
    

    And this is the output (pdf):

    0_1543418026862_cut.png

    As you can see, the table is truncated right in the middle (taken from a big html with a long series of tables in it).

    Is there any way to not have the table cut in half like this? I know there's a QPrinter::newPage method to be used, but I don't do everything by hand (as you can see from the code). Any suggestions?


  • Lifetime Qt Champion

    Hi
    is it not that web page is much WIDER than the A4?
    so im not sure newPage() will help at all.
    You could try
    http://doc.qt.io/qt-5/qwebenginepage.html#zoomFactor-prop
    or
    m_printer->setPaperSize(QPrinter::A3);
    PDF can easy scale back to A4 when printed. ( has fit to page option)
    so its not big issue that media is A3.
    You could also try to flip orientation
    http://doc.qt.io/qt-5/qprinter.html#Orientation-enum



  • @mrjj said in HTML tables cut in half on print to pdf:

    Hi
    is it not that web page is much WIDER than the A4?
    so im not sure newPage() will help at all.
    You could try
    http://doc.qt.io/qt-5/qwebenginepage.html#zoomFactor-prop
    or
    m_printer->setPaperSize(QPrinter::A3);
    PDF can easy scale back to A4 when printed. ( has fit to page option)
    so its not big issue that media is A3.
    You could also try to flip orientation
    http://doc.qt.io/qt-5/qprinter.html#Orientation-enum

    No, the image shown is just an extract. My problem is vertical, not horizontal. The width is perfect. I just want to find out a way to not have the newpage truncate my tables (vertically) like the image shows.


  • Lifetime Qt Champion

    Hi
    Ok, the image cheated a little.
    I was under the impression it would handle newPage by itself.
    https://stackoverflow.com/questions/19211337/qwebview-not-printing-multiple-pages-on-real-printer

    did you try other resolution
    QPrinter(QPrinter::ScreenResolution);
    to see if that makes any difference.



  • @nwoki
    Sorry, I still don't understand what you want. At first like @mrjj I thought you were showing a horizontally-truncated table. Now I understand it's vertical, what are you looking to happen? What is being "truncated"? Do you want the whole table to go to a new page if it's not going to fit where it is (in which case, what if the table exceeds a whole page)? Do you not like it dividing in the middle of a row, and you want any row to start on a new page if you're at the bottom? Or what??



  • @JonB said in HTML tables cut in half on print to pdf:

    @nwoki
    Sorry, I still don't understand what you want. At first like @mrjj I thought you were showing a horizontally-truncated table. Now I understand it's vertical, what are you looking to happen? What is being "truncated"? Do you want the whole table to go to a new page if it's not going to fit where it is (in which case, what if the table exceeds a whole page)? Do you not like it dividing in the middle of a row, and you want any row to start on a new page if you're at the bottom? Or what??

    Yes, I do not like the fact that the table is being split in the middle of the row. What I'd like to achieve is that the page ends or begins with the table and not having it truncated in half.

    I'm starting to think that maybe my html might have something to do with this. Example below

    <h2>System Parameters</h2>
    <h3>Generator Data</h3>
    <table class=registers>
       <tr>
          <th style=text-align:left>DESCRIPTION</th>
          <th>UM</th>
          <th>MIN</th>
          <th>MAX</th>
          <th>VALUE</th>
       </tr>
       <tr>
          <td class=description>Rated voltage</td>
          <td class=center>V</td>
          <td class=center>100</td>
          <td class=center>20000</td>
          <td class=center>100</td>
       </tr>
       <tr>
          <td class=description>Rated Active Power</td>
          <td class=center>KW</td>
          <td class=center>1</td>
          <td class=center>50000</td>
          <td class=center>1</td>
       </tr>
       <tr>
          <td class=description>Rated PF</td>
          <td class=center></td>
          <td class=center>1</td>
          <td class=center>2</td>
          <td class=center>0</td>
       </tr>
       <tr>
          <td class=description>Rated Frequency</td>
          <td class=center>Hz</td>
          <td class=center>10</td>
          <td class=center>100</td>
          <td class=center>10</td>
       </tr>
       <tr>
          <td class=description>Base exciter field voltage</td>
          <td class=center>V</td>
          <td class=center>0.1</td>
          <td class=center>200</td>
          <td class=center>0.1</td>
       </tr>
       <tr>
          <td class=description>Rated field current</td>
          <td class=center>A</td>
          <td class=center>0.1</td>
          <td class=center>10</td>
          <td class=center>0.1</td>
       </tr>
    ...
    ...
    ...
    </table>
    

    As it's a full wall text of tables, there's no way that the printer can figure out for itself where and when to trim the page correctly.



  • @nwoki

    Yes, I do not like the fact that the table is being split in the middle of the row. What I'd like to achieve is that the page ends or begins with the table and not having it truncated in half.

    Again, you have contradicted yourself. Either you want to ensure that whole table is not broken across a page, or you want to ensure that a row is not broken a page. They are not the same thing.

    Anyway, I would say you have a chance that you might be able to prevent table split across page, and no chance to prevent a row split.

    Having said that, when you are exporting HTML to PDF/printer there is nothing I know of which you can specify to alter the behaviour that it's just a stream of HTML and page breaks will occur where the page ends. Unless there is some option to PDF exporter which lets you say "I want such-and-such behaviour on tables", which I don't know of.



  • Re: HTML tables cut in half on print to pdf
    If you have option for editing html output, you can write below code to arrange page-breaks.

    <style>
    @media print
    {
      table { page-break-after:auto }
      tr    { page-break-inside:avoid; page-break-after:auto }
      td    { page-break-inside:avoid; page-break-after:auto }
      thead { display:table-header-group }
      tfoot { display:table-footer-group }
    }
    </style>
    

    If not, you can add your custom stylesheet. You can find info at this page.
    If you want more customization, you can make it with javascript.



  • Hi, sorry for the late response. Had a lot on my plate lately. For now, the tables are still truncated but modifying the html, i've made it so that the new pages (where the tables get truncated) show the table headers using the css suggested by @CKurdu .


  • Banned

    This post is deleted!

Log in to reply