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. Qt printing using html and QTextDocument
Forum Updated to NodeBB v4.3 + New Features

Qt printing using html and QTextDocument

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtquickqtextdocumentqprinterhtmlpdf
7 Posts 2 Posters 2.1k Views 1 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.
  • K Offline
    K Offline
    kuntio
    wrote on 24 Jan 2022, 18:06 last edited by kuntio
    #1

    I fight with printing pdf from QTextDocument object with html format. I generate QString formatted code of html page and it works fine. To test it i make using QFile a html file which have to be printed and it look greats.
    But pdf file looks completly different and i don't know why.

    Code:

        QString html = getHtmlPrintString(FVToPrint, currentCustomer);
        QTextDocument document;
        document.setHtml(html);
    
        QFile qHtmlFile("D:\\Qt\\projects\\FVGeneratorV2\\fv\\"+QString::fromStdString(FVToPrint.getFVId()) +".html");
        if(qHtmlFile.open(QIODevice::WriteOnly))
        {
            qHtmlFile.write(html.toUtf8());
            qHtmlFile.close();
        }
    
        QPrinter printer(QPrinter::PrinterResolution);
        printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setPageSize(QPageSize::A4);
        QString fvname = "D:\\Qt\\projects\\FVGeneratorV2\\fv\\" + QString::fromStdString(FVToPrint.getFVId()) + ".pdf";
        printer.setOutputFileName(fvname);
        printer.setPageMargins(QMarginsF(15, 15, 15, 15));
    
        document.print(&printer);
    

    Html file code:

    <!-- begin snippet: js hide: false console: true babel: false -->

    <!-- language: lang-html -->

    <!DOCTYPE html>

    <html>
    
    
    
    <head>
    
      <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
    
      <style type='text/css'>
    
        table,
    
        tr,
    
        td {
    
          border: 1px solid black;
    
          border-collapse: collapse;
    
        }
    
        
    
        td {
    
          padding: 1%;
    
        }
    
        
    
        html,
    
        body {
    
          th: 100%;
    
          height: 100%;
    
          margin: 0;
    
          padding: 0;
    
        }
    
      </style>
    
    </head>
    
    
    
    <body>
    
      <table style='width:100%; height:100%; border: 0px; margin:0; padding: 0;'>
    
        <tr>
    
          <td>
    
            <table style='float:left; border: 2px solid black; width:42%; margin-right:2%; margin-left:2%; padding:2%; height:5%;'>
    
              <tr>
    
                <td>
    
                  <h2><b>FV_24.01.2022_666_1_73</b></h2>
    
                </td>
    
              </tr>
    
            </table>
    
            <table style='float:left; padding:2%; margin-right:2%; border: 2px solid black; width:42%; height:5%;'>
    
              <tr>
    
                <td>24.01.2022</td>
    
              </tr>
    
            </table>
    
            <table style='float:none; border-style:none;'></table>
    
            <table style='float:left; border: 2px solid black; width:42%; margin-right:2%; margin-top:1%; margin-left:2%; padding:2%; height:15%;'>
    
              <tr>
    
                <td>kunito<br> Nip: 8712314<br> Phone: 9123412<br> Email: mail@com<br>miejska 12<br>47821 krakow, Polska<br></td>
    
              </tr>
    
            </table>
    
            <table style='float:left; margin-top:1%; padding:2%; margin-right:2%; border: 2px solid black; width:42%; height:15%;'>
    
              <tr>
    
                <td>Customer: <br> Name: test <br> Nip: 12398401<br>miejska 27<br>81293 Krakow, Polska<br></td>
    
              </tr>
    
            </table>
    
            <table style='float:none; border-style:none;'></table>
    
            <table style=' margin: 1% 2% 0 2%; padding:2%; border: 2px solid black; width:90.5%; height:10%;'>
    
              <tr>
    
                <td>Payment data: <br>online transfer<br> Account number: 8912384<br> Title: FV_24.01.2022_666_1_73</td>
    
              </tr>
    
            </table>
    
            <table style='border-style:none; margin: 1% 2% 0 2%; padding:2%; width:90.5%; height:30%;'>
    
              <tr style='border-style:none;'>
    
                <td style='border-style:none;'>
    
                  <table style='width:100%;'>
    
                    <tr style='background-color:darkgray;'>
    
                      <td>Nb.</td>
    
                      <td>Name</td>
    
                      <td>Um</td>
    
                      <td>Quantity</td>
    
                      <td>Price netto</td>
    
                      <td>Vat</td>
    
                      <td>Vat value</td>
    
                      <td>Price brutto</td>
    
                    </tr>
    
                    <tr>
    
                      <td>1</td>
    
                      <td>Bulka</td>
    
                      <td>pcs</td>
    
                      <td>2</td>
    
                      <td>2.024</td>
    
                      <td>8</td>
    
                      <td>0.176</td>
    
                      <td>2.2</td>
    
                    </tr>
    
                    <tr style='background-color:darkgray;'>
    
                      <td colspan='5'></td>
    
                      <td>Netto sum</td>
    
                      <td>Vat sum</td>
    
                      <td>Sum</td>
    
                    </tr>
    
                    <tr>
    
                      <td colspan='5'></td>
    
                      <td>2.024</td>
    
                      <td>0.176</td>
    
                      <td>2.2</td>
    
                    </tr>
    
                  </table>
    
                </td>
    
              </tr>
    
            </table>
    
            <table style='border-style:none; margin: 1% 2% 0 2%; padding:2%; width:90.5%; height:5%;'>
    
              <tr>
    
                <td>To pay: 2.2 zl <br> Payed: 2.2 zl</td>
    
              </tr>
    
            </table>
    
            <table style='border-style:none; margin: 1% 2% 0 2%; padding:2%; width:90.5%; height:5%;'>
    
              <tr style='border-style:none;'>
    
                <td style='border-style:none;'>
    
                  <table style='border-style:none; float:left; width: 20%; height:100%; '>
    
                    <tr style='border-style:none;'>
    
                      <td style='border-style:none;'>
    
                        <p align='center'>............................<br>podpis pracownika</p>
    
                      </td>
    
                    </tr>
    
                  </table>
    
                  <table style='border-style:none; float:left; width: 20%; height:100%; margin-left:50%; '>
    
                    <tr style='border-style:none;'>
    
                      <td style='border-style:none;'>
    
                        <p align='center'>............................<br>podpis klienta</p>
    
                      </td>
    
                    </tr>
    
                  </table>
    
                </td>
    
              </tr>
    
            </table>
    
          </td>
    
        </tr>
    
      </table>
    
    </body>
    
    
    
    </html>
    

    [Pdf printed file][1]

      [1]: https://i.stack.imgur.com/hEkT0.png
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 24 Jan 2022, 19:33 last edited by
      #2

      Hi and welcome to devnet,

      Do you mean that if you have that document printed on paper it works correctly but not as a PDF ?

      In any case, don't forget that QTextDocument does not support the full range of HTML capabilities.

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

      K S 2 Replies Last reply 24 Jan 2022, 20:28
      0
      • S SGaist
        24 Jan 2022, 19:33

        Hi and welcome to devnet,

        Do you mean that if you have that document printed on paper it works correctly but not as a PDF ?

        In any case, don't forget that QTextDocument does not support the full range of HTML capabilities.

        K Offline
        K Offline
        kuntio
        wrote on 24 Jan 2022, 20:28 last edited by
        #3

        @SGaist I mean i save file with QString html formatted text and when i open it in chrome it look as i want. Now i change div's to table because only table have border and float property in CSS Rich Text, but i still don't look as a html in web browser.

        1 Reply Last reply
        0
        • S SGaist
          24 Jan 2022, 19:33

          Hi and welcome to devnet,

          Do you mean that if you have that document printed on paper it works correctly but not as a PDF ?

          In any case, don't forget that QTextDocument does not support the full range of HTML capabilities.

          S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 24 Jan 2022, 20:29 last edited by
          #4

          This remark I wrote is really important:

          @SGaist said in Qt printing using html and QTextDocument:

          In any case, don't forget that QTextDocument does not support the full range of HTML capabilities.

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

          K 1 Reply Last reply 24 Jan 2022, 20:41
          0
          • S SGaist
            24 Jan 2022, 20:29

            This remark I wrote is really important:

            @SGaist said in Qt printing using html and QTextDocument:

            In any case, don't forget that QTextDocument does not support the full range of HTML capabilities.

            K Offline
            K Offline
            kuntio
            wrote on 24 Jan 2022, 20:41 last edited by
            #5

            @SGaist i know it and i think that now i have everything that QTextDocument support, but it still look like width property didn't work correctly. Check it how it looks now because i edit the post. Only clear property is not supporting and i don't know what i should use instead of them. I tried float:none but it didn't work as i want. I edit post check how it looks now

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 24 Jan 2022, 20:54 last edited by
              #6

              IIRC, there should be no px after border.

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

              K 1 Reply Last reply 24 Jan 2022, 21:03
              0
              • S SGaist
                24 Jan 2022, 20:54

                IIRC, there should be no px after border.

                K Offline
                K Offline
                kuntio
                wrote on 24 Jan 2022, 21:03 last edited by
                #7

                syntax from documentation for border <width>px <border-style> <border-color>
                That's mean it should be px after border.

                1 Reply Last reply
                0

                5/7

                24 Jan 2022, 20:41

                • Login

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