Forum


Replies: 8   Views: 7337
Setting fixed width to first column in an html table
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by davelavinsky  · 21-12-2017 - 08:41

Im inserting a table using HTML in my DOCX.

$html .= "<table class='tbl' style='font-size:12px'>";
$html .= "<thead>
            <th width='250'></th>                
            <th>FY 1</th>
            <th>FY 2</th>
            <th>FY 3</th>
            <th>FY 4</th>
            <th>FY 5</th>
          </thead>
          ";

$html .= "<tbody>";    
          
$html .= "<tr>";
$html .= "<td width='250'>";
.
.
.
.
$docx->embedHTML($html);

But as you can see, the width is not 250 for column 1 and column 5 has a bigger width.

https://imgur.com/a/EJnQu

How do I change this ?

Posted by admin  · 21-12-2017 - 11:40

Hello,

If you don't set a fixed width value then the table grows to fit the content.

Please set widths for all cols of the first row:

$html = '<table>
            <tbody>
                <tr>
                    <td width="180">FY 1</td>
                    <td width="180">FY 2</td>
                    <td width="180">FY 3</td>
                    <td width="180">FY 4</td>
                    <td width="100">FY 5</td>
                </tr>
                <tr>
                    <td>Content A1</td>
                    <td>Content A2</td>
                    <td>Content A3</td>
                    <td>Content A4</td>
                    <td>Content A5</td>
                </tr>

Regards.

Posted by davelavinsky  · 21-12-2017 - 15:08

I did that but the last column is still wider than the rest.

    $html .= "<table class='tbl-summary' style='font-size:12px'>";
    $html .= "<thead>
                <th width='500'></th>                
                <th width='250'>FY 1</th>
                <th width='250'>FY 2</th>
                <th width='250'>FY 3</th>
                <th width='250'>FY 4</th>
                <th width='250'>FY 5</th>
              </thead>
              ";

Im using phpdocx-corporate-5.5

Posted by admin  · 21-12-2017 - 16:16

Hello,

We have done some tests and everything is working as expected.

If we transform this simple HTML:

$html = '<table>
            <tbody>
                <tr>
                    <td width="180">FY 1</td>
                    <td width="180">FY 2</td>
                    <td width="180">FY 3</td>
                    <td width="180">FY 4</td>
                    <td width="100">FY 5</td>
                </tr>
                <tr>
                    <td>Content A1</td>
                    <td>Content A2</td>
                    <td>Content A3</td>
                    <td>Content A4</td>
                    <td>Content A5</td>
                </tr>
                <tr>
                    <td>Content B1</td>
                    <td>Content B2</td>
                    <td>Content B3</td>
                    <td>Content B4</td>
                    <td>Content B5</td>
                </tr>
                <tr>
                    <td>Content C1</td>
                    <td>Content C2</td>
                    <td>Content C3</td>
                    <td>Content C4</td>
                    <td>Content C5</td>
                </tr>
                <tr>
                    <td>Content D1</td>
                    <td>Content D2</td>
                    <td>Content D3</td>
                    <td>Content D4</td>
                    <td>Content D5</td>
                </tr>
            </tbody>
        </table>';

$docx->embedHTML($html);

and then we open the DOCX output with MS Word, all cells have the correct sizes. Please test the previous HTML standalone and check the output with MS Word (are you using another program to open the DOCX?).

We think there may be two issues in your code: the class tbl-summary is adding some extra style or some content has a very long word without spaces. By default, MS Word increases a cell size to fit the content if there's some word without spaces that doesn't fit in the cell width (such as 'thisisaverylongwordaddedtoacell').

If you use the addTable method you could set the fitText option to true to avoid this default behavior of MS Word, but the transformation from HTML to Word doesn't support it. DOCXCustomizer can inject this tag to any cell in a DOCX, but it's only available in Premium licenses.

If after following these recommendations you still have issues, please send to contact[at]phpdocx.com the most simple script that illustrates your issue (without doing external connections such as databases or web services) and the DOCX output you get. Also please attach the template if using any.

Regards.

Posted by davelavinsky  · 21-12-2017 - 16:44

I see - I've been checking the PDF output so far, not the DOCX output.

$html .= "
<style type='text/css'>
.tbl-summary { border-collapse:collapse; }
.tbl-summary thead tr th,
.tbl-summary thead tr td,
.tbl-summary tbody tr td
{
    border:1px solid #000000;
    padding:5px;
    text-align:center;
    width:100px;
}
.tbl-summary tbody tr td:first-child
{
    text-align:left;        
}
</style>
";    


$html .= "<table class='tbl-summary' style='font-size:12px'>";
$html .= "<thead>
            <th width='250'></th>
            <th>FY 1</th>
            <th>FY 2</th>
            <th>FY 3</th>
            <th>FY 4</th>
            <th>FY 5</th>
          </thead>
          ";

$html .= "<tbody>";  

 

Posted by admin  · 21-12-2017 - 18:03

Hello,

We have tested your HTML using the conversion plugin and the output is the same than the DOCX, it has the correct widths. We have tested it using the conversion plugin based on LibreOffice (version 5.4) that is the recommended one since phpdocx 4.5.

Please check that Tidy for PHP is installed and enabled, and we also recommend you to read the information available on https://www.phpdocx.com/documentation/conversion-plugin/preparing-the-templates-for-its-conversion, that explains how to get the best output when transforming the DOCX; such as fixed widths for tables, rows and cells. A table with a width of 620px is equivalent to a 100% for an A4 page, so you shouldn't exceed that value in an A4 document.

Regards.

Posted by davelavinsky  · 22-12-2017 - 03:49

Thanks for your response.

The table shows perfectly in Microsoft Word but not in Libre Office and the PDF generated has the widths different. So for now this is resolved. Thanks.

Posted by admin  · 22-12-2017 - 07:08

Hello,

We have tested that using the latest version of the conversion plugin, the output of both files (DOCX and PDF) is the same and they have the correct cell widths.

Regards.

Posted by davelavinsky  · 22-12-2017 - 11:03

Im using phpdocx-corporate-5.5

Does 7.5 have any updates related to this (table) and graphs ? If so, I'll update to the latest.