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. HTML fixed width table in qtextdocument converted in pdf file
Forum Update on Monday, May 27th 2025

HTML fixed width table in qtextdocument converted in pdf file

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.6k 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.
  • Z Offline
    Z Offline
    zandarina
    wrote on 20 Apr 2020, 14:50 last edited by zandarina
    #1

    Hello everybody,

    I have a question, i want to generate a pdf report from tables and images.
    And i would like to have a fixed width of the table. I know width is allowed
    in the html subset permitted by qtextdocument. But if the text is larger
    than the width, the cell expands instead of breaking the text into lines so the width is not considered.

    I know that table-layout: fixed; is ignored by qtextdocument. So i get the table beyond the margins even if they are defined because the text is wider than the cell width.
    I have tried to add <br> /n... in the middle of the text but nothing happens
    Screenshot_1.png

    How can i really limit the cell size?.

    I heard about qtwebengine available for my current qt 5.12.3 version but i also read that it is very heavy.

    How do you limit a cell with qtextdocument or break it into lines? Or I should add the qtwebengine module because it is the only way?

    Thanks

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on 21 Apr 2020, 03:43 last edited by Bonnie
      #2

      Can you show you HTML code? You can replace you content with meaningless characters like "11111111111111".
      When I create a table in a QTextEdit with a fixed width, seems it do break the text into lines.
      UPDATE
      Small tip: when setting a fixed length in Qt rich text, units are not allowed. So instead of 'width="100px"', you can only use 'width="100"'.

      Z 1 Reply Last reply 21 Apr 2020, 09:59
      1
      • B Bonnie
        21 Apr 2020, 03:43

        Can you show you HTML code? You can replace you content with meaningless characters like "11111111111111".
        When I create a table in a QTextEdit with a fixed width, seems it do break the text into lines.
        UPDATE
        Small tip: when setting a fixed length in Qt rich text, units are not allowed. So instead of 'width="100px"', you can only use 'width="100"'.

        Z Offline
        Z Offline
        zandarina
        wrote on 21 Apr 2020, 09:59 last edited by zandarina
        #3

        Hi thank you for answering

        My html code is the following. I have tried many different sizes of width
        even percentage 100% total (20% each) but i always got the same result without
        breaking lines and then going out of the margin of my page.
        I also defined a margin in the html additionally to the one
        defined in pdfWritter but nothing happened.

           QPdfWriter pdfWriter("pdf_generated.pdf");
           pdfWriter.setPageSize(QPagedPaintDevice::A4);
           pdfWriter.setPageMargins(QMargins(30, 30, 30, 30));
        
        QString html =
        	"<head>"
        	"<meta charset=utf-8/>"
        	"< / head>"
        	"<body>"
        	"<h2 align=center>Header</h2>"
        	"<ul>"
        	"<li>111111111111< / li>"
        	"<li>11111111111111< / li>"
        	"<li>111111111111111< / li>"
        	"<li>111111111111111< / li>"
        	"< / ul>"
        	"<br><br>"
        	"<p>"
        	"<table width= 400 border=0.1>"
        	"<tr>"
        	"<th width=100 > 111111  11111111  11111111 < / th>"
        	"<th width=100 >1111  111  1111111  11111111< / th>"
        	"<th width=100>11111111  111  11111111 1111< / th>"
        	"<th width=100>11111  111111111  11111   1111< / th>"
        	"< / tr>"
        	"<tr>"
        	"<td>11111< / td>"
        	"<td>11111< / td>"
        	"<td>11111< / td>"
        	"<td>11111< / tr>"
                    "< / tr>"
        	"< / table>"
        	"<br><br>"
        	"</p>"
        	"<p>"
        	"<table width=500 border=0.1>"
        	"<tr>"
        	"<th width=100>111111   111111  11< / th>"
        	"<th width=100>111111  111111  11< / th>"
        	"<th width=100>111111  111111  111< / th>"
        	"<th width=100>111111  111111  111< / th>"
        	"<th width=100>11111  111111  111< / th>"
        	"< / tr>"
        	"<tr>"
        		"<td>1111< / td>"
        		"<td>111< / td>"
        		"<td>111< / td>"
        		"<td>111< / td>"
        		"<td>1111< / td>"
        	"< / tr>"
        	"< / table>"
        	"<br><br>"
        	"</p>"
        	"</body>";
        
                  QTextDocument document;
              document.setHtml(html);
         
               QPainter painter(&pdfWriter);
                  document.drawContents(&painter);
        

        And if I changed it to

        	"<table width=125 border=0.1>"
        	"<tr>"
        	"<th width=25>N&uacute;mero de implante< / th>"
        	"<th width=25>Di&aacute;metro del pilar (mm)< / th>"
        	"<th width=25>Altura del pilar (mm)< / th>"
        	"<th width=25>ID de pieza< / th>"
        	"<th width=25>&Aacute;ngulo< / th>"
        	"< / tr>";
        

        the same thing. It is a bit weird

        Thanks

        B 1 Reply Last reply 21 Apr 2020, 13:53
        0
        • Z zandarina
          21 Apr 2020, 09:59

          Hi thank you for answering

          My html code is the following. I have tried many different sizes of width
          even percentage 100% total (20% each) but i always got the same result without
          breaking lines and then going out of the margin of my page.
          I also defined a margin in the html additionally to the one
          defined in pdfWritter but nothing happened.

             QPdfWriter pdfWriter("pdf_generated.pdf");
             pdfWriter.setPageSize(QPagedPaintDevice::A4);
             pdfWriter.setPageMargins(QMargins(30, 30, 30, 30));
          
          QString html =
          	"<head>"
          	"<meta charset=utf-8/>"
          	"< / head>"
          	"<body>"
          	"<h2 align=center>Header</h2>"
          	"<ul>"
          	"<li>111111111111< / li>"
          	"<li>11111111111111< / li>"
          	"<li>111111111111111< / li>"
          	"<li>111111111111111< / li>"
          	"< / ul>"
          	"<br><br>"
          	"<p>"
          	"<table width= 400 border=0.1>"
          	"<tr>"
          	"<th width=100 > 111111  11111111  11111111 < / th>"
          	"<th width=100 >1111  111  1111111  11111111< / th>"
          	"<th width=100>11111111  111  11111111 1111< / th>"
          	"<th width=100>11111  111111111  11111   1111< / th>"
          	"< / tr>"
          	"<tr>"
          	"<td>11111< / td>"
          	"<td>11111< / td>"
          	"<td>11111< / td>"
          	"<td>11111< / tr>"
                      "< / tr>"
          	"< / table>"
          	"<br><br>"
          	"</p>"
          	"<p>"
          	"<table width=500 border=0.1>"
          	"<tr>"
          	"<th width=100>111111   111111  11< / th>"
          	"<th width=100>111111  111111  11< / th>"
          	"<th width=100>111111  111111  111< / th>"
          	"<th width=100>111111  111111  111< / th>"
          	"<th width=100>11111  111111  111< / th>"
          	"< / tr>"
          	"<tr>"
          		"<td>1111< / td>"
          		"<td>111< / td>"
          		"<td>111< / td>"
          		"<td>111< / td>"
          		"<td>1111< / td>"
          	"< / tr>"
          	"< / table>"
          	"<br><br>"
          	"</p>"
          	"</body>";
          
                    QTextDocument document;
                document.setHtml(html);
           
                 QPainter painter(&pdfWriter);
                    document.drawContents(&painter);
          

          And if I changed it to

          	"<table width=125 border=0.1>"
          	"<tr>"
          	"<th width=25>N&uacute;mero de implante< / th>"
          	"<th width=25>Di&aacute;metro del pilar (mm)< / th>"
          	"<th width=25>Altura del pilar (mm)< / th>"
          	"<th width=25>ID de pieza< / th>"
          	"<th width=25>&Aacute;ngulo< / th>"
          	"< / tr>";
          

          the same thing. It is a bit weird

          Thanks

          B Offline
          B Offline
          Bonnie
          wrote on 21 Apr 2020, 13:53 last edited by Bonnie
          #4

          @zandarina
          Why are there some weird spaces in your code?
          Anyway, you should write to pdf like this

          document.print(&pdfWriter);
          
          Z 1 Reply Last reply 21 Apr 2020, 14:19
          0
          • B Bonnie
            21 Apr 2020, 13:53

            @zandarina
            Why are there some weird spaces in your code?
            Anyway, you should write to pdf like this

            document.print(&pdfWriter);
            
            Z Offline
            Z Offline
            zandarina
            wrote on 21 Apr 2020, 14:19 last edited by zandarina
            #5

            I added spaces to simulate words like "name of something". I do not have installed the printer
            module. But i am using QPdfWriter instead. It can be a bug of QPdfWriter ? I will install
            the printer module.

            Thanks

            B 1 Reply Last reply 21 Apr 2020, 14:26
            0
            • Z zandarina
              21 Apr 2020, 14:19

              I added spaces to simulate words like "name of something". I do not have installed the printer
              module. But i am using QPdfWriter instead. It can be a bug of QPdfWriter ? I will install
              the printer module.

              Thanks

              B Offline
              B Offline
              Bonnie
              wrote on 21 Apr 2020, 14:26 last edited by Bonnie
              #6

              @zandarina
              I don't think this need the printer module.
              QPdfWriter is fine to use.
              This is my full code of the "printing".

                  QPdfWriter pdfWriter(name);
                  pdfWriter.setPageSize(QPagedPaintDevice::A4);
                  pdfWriter.setPageMargins(QMargins(30, 30, 30, 30));
                  QTextDocument document;
                  document.setHtml(html);
                  document.print(&pdfWriter);
              

              Actually, the "print" function uses QPagedPaintDevice class which is also part of gui module.

              1 Reply Last reply
              0

              1/6

              20 Apr 2020, 14:50

              • Login

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