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

HTML fixed width table in qtextdocument converted in pdf file



  • 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



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



  • 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



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

    document.print(&pdfWriter);
    


  • 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



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


Log in to reply