HTML2PDF Table content is overflowing out of page
Asked Answered
G

3

5

I am using HTML2PDF library and I am getting an issue while I am trying to to output my pdf. Actually, my table is always truncated, unable to display the whole content of my table without having it cut. I defined css style, but it seems having to effect.Moreover, If I don't defined into my php script on echo statement table border="1px" it doesn't take style css defined previously.

Here my code:

<!DOCTYPE html>
<html>
<head>
<title>Test PDF</test>
<style>
table{width:100%; border-collapse:collapse; table-layout:auto; vertical-align:top; margin-bottom:15px; border:1px solid #CCCCCC;}
table thead th{font-size: 2px; color:#FFFFFF; background-color:#666666; border:1px solid #CCCCCC; border-collapse:collapse; text-align:center; table-layout:auto; vertical-align:middle;}
table tbody td{vertical-align:top; border-collapse:collapse; border-left:1px solid #CCCCCC; border-right:1px solid #CCCCCC; font-size: 2px;}
table thead th, table tbody td{padding:5px; border-collapse:collapse;}
table tbody tr.light{color:#979797; background-color:#F7F7F7;}
table tbody tr.dark{color:#979797; background-color:#E8E8E8;}
</style>
</head>
<body>
<html>
<?php
require_once "config.php";

//Start html2pdpf session
ob_start();
//Get tablename
$tablename=$_POST["tablename"];
$ShowPivotTableResult='SELECT * FROM '.$tablename.'';
    $resultLeast=mysqli_query($conn,$ShowPivotTableResult) or die(mysqli_error($conn));
if (!$resultLeast) {
    die("Query to show fields from table failed");
    }
//Display Pivot Table content - script
$fields_num = mysqli_num_fields($resultLeast);
    echo "<table border='1px' CELLSPACING='0' cellpadding='2'><thead><tr>";
    // printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysqli_fetch_fields($resultLeast);
    echo '<th>'.$field[$i]->name.'</th>';
}
echo "</tr></thead><tbody>\n";
// printing table rows
while($rowLEAST = mysqli_fetch_row($resultLeast))
{
    echo "<tr>";
    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($rowLEAST as $cellPIVOT)
    echo "<td>$cellPIVOT</td>";
    echo "</tr>\n";
    }
    echo '</tbody></table><br/><br/>';

$content = ob_get_clean();

    // convert
    require_once('html2pdf.class.php');
    try
    {
        $html2pdf = new HTML2PDF('L', 'A4', 'fr', true, 'UTF-8', 0);
        $html2pdf->pdf->SetDisplayMode('fullpage');
        $html2pdf->writeHTML($content, isset($_GET['vuehtml']));
        ob_end_clean();
        $html2pdf->Output(''.$tablename.'.pdf');
    }
    catch(HTML2PDF_exception $e) {
        echo $e;
        exit;
    }

mysqli_close($conn);
?>
</body>
</html>
Guenon answered 4/3, 2016 at 10:34 Comment(0)
R
5

Try to use absolute lengths on table width like pt. A4 has 595pt width. About css attribute, try to use class instead of the tag. example:

HTML

<style>
    .tbl{
     width: 595pt;
     border: 1px solid #000;
    }
</style>

PHP

echo "<table class='tbl' CELLSPACING='0' cellpadding='2'><thead><tr>";
Rebekah answered 4/3, 2016 at 10:53 Comment(2)
Hi @rmondesilva, thanks for your help. Your solution is working for table border. But My table is still truncated on my A4 pdf. I tried to fit $html2pdf = new HTML2PDF('L', 'A4', 'fr', true, 'UTF-8', 0); in Landscape mode and .tbl{ width: 842pt;} but my table content is overflowing out of pageGuenon
You're welcome, I'm glad it works. In your another problem, Did you try to use CSS attribute white-space: nowrap; on table cell/header? and also try to use word-wrap: break-word; or word-break: break-all;. Put a table-layout: fixed; on table and set a fix/absolute length for your table headers/cells. Possible reason of your problem is because of your content, so try those solutions.Rebekah
M
2

I have solved word wrap in a TD by putting a DIV inside the TD with the wordwrap style, and setting a table width. The two things combined worked. E.g.

table {
  width: 295pt;
}

<td>
  <div style="word-wrap: break-word;">Some long html posibly-joined-text-that-will-overflow-off-of-the-page</div>
</td>

Edit: This sometimes works and sometimes doesn't. I ended up switching to dompdf and that solved all of my problems.

Marciano answered 12/4, 2023 at 7:32 Comment(1)
This worked for me combined with: html2pdf->setTestTdInOnePage(false); Thank you!Hardtop
O
0

If you have specified table cell width in percentage, try to remove it and control the width using style attribute. The width should be in mm unit proportional to the page width.

E.g. if your page is A4 - portrait which is 595.276 wide. Your 50% width of table cell will take (595.276/2)-margins width in style attribute.

Ottoottoman answered 27/12, 2018 at 9:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.