29

I am creating a large HTML table and I have problem with page breaks as you can see in the following image:
enter image description here
Is there a method settle down the problem automatically? Or what is the way to do it?

user3335966
  • 2,458
  • 4
  • 26
  • 32
Koney
  • 291
  • 1
  • 3
  • 3
  • What exactly is the problem? A cell is split over 2 pages? – alex May 01 '11 at 03:27
  • check out my answer below. It should help anyone out who still struggles with this problem. – FastTrack Oct 02 '12 at 14:47
  • Correct Answer in this question should help: [manual-page-break-in-tcpdf](https://stackoverflow.com/questions/1605860/manual-page-break-in-tcpdf) – taufardh Jan 24 '21 at 12:53

8 Answers8

74

Try adding this to your <tr> tags: nobr="true".

So a quick example would be:

<table>
    <tr nobr="true">
        <td>Test</td>
        <td>Test 2</td>
    </tr>
</table>

The nobr="true" prevents the table rows from ever breaking apart. You can also put this on <td> and <th> tags.

FastTrack
  • 8,191
  • 14
  • 52
  • 75
13

I know it's rather old question, but I had this same issue today, my table was splitted between pages and I investigated a little bit further on the method from FastTrack's answer and it turned out that you can also use the nobr="true" attribute also to the <table> tag. That is, for me such code solved this problem:

<table nobr="true">
    <tr>
        <td>Test</td>
        <td>Test 2</td>
    </tr>
</table>

Warning - this code makes sense only when your tables are smaller than one page.

7

I had the same problem with overlapping headers. I tried yevgeny solution, but that required some more editions to my PDF generator code (I have lots of PDFs outputs written in FPDF and I wanted to minimize the process of miograting them to TCPDF), so I used this more simple solution

class MYPDF extenfs TCPDF {
    //your initialization code
    function header(){
        //your code here

        //we change only the top margin and this executes for every header in every page, even the frst one
        $this->SetTopMargin($this->GetY());
    }
}
  • this solution gives me the almost right postion for the first pages, but page after page the cell height increases and gives me one page cell height – linuxatico Oct 29 '13 at 09:32
  • this looks promising. Is there a way to dynamicly repeat table head? – alex Nov 17 '14 at 10:25
3

roney, thank you so much, writing HTML generated overlaps with the header for pages 2,3 .. this worked for me:

class your_PDF extends TCPDF
{
   var $top_margin = 20;

   function Header() {
       // set top margin to style pages 2, 3..
       //title goes here
       $this->top_margin = $this->GetY() + 5; // padding for second page
    }
}

in your code

// set top margin to style pages 2, 3..
$pdf->SetMargins(15, $pdf->top_margin, 15); 
Emre Yazici
  • 9,708
  • 6
  • 46
  • 54
yevgeny
  • 39
  • 2
  • When I turned Autopagebreak on in TCPDF, all the subsequent pages had content overlapped with the header. Thanks, this worked for me. – janenz00 Mar 31 '12 at 21:20
1

For the interested, just do as follows and it will work like a charm:

$pdf->SetMargins(0, 0, 0);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(0);
roney
  • 19
  • 1
1

Strangely enough the solutions mentioned here didn't work for me. Well, it did sortof, but content inside of tags would get repeated (as desired) but would then cause layout issues for the cell above or below if it was rowspanned. As I experimented, it just got worse.

My solution, while inelegant, was to set AutoPageBreak to false, put a row incrementer counter up, incrementing for each row, then checked if it had exceeded a certain value. If so, I closed the table, used writeHTML(), called addPage() and then continued , having rebuilt it as a new table, headers and all.

Like I said, inelegant, but it worked. I hope this helps someone ... it's a fairly obvious solution but execution is not always quite so obvious. Also, there may be a better way that works for your particular situation, but if it doesn't, give mine a try. :)

David
  • 538
  • 2
  • 6
  • 16
  • even with the lastest updates, the same repeated rows occurs. Did you find a solution ? I don't know how big my rows will be, included text can be either short or long: http://stackoverflow.com/questions/15689278/tcpdf-writehtml-repeats-rows-on-pagebreaks – chriscatfr Mar 29 '13 at 12:26
  • @chriscatfr haven't tried since I responded to the question. My original 'solution' did the job at the time. These days I don't use the HTML functionality, so I'm sorry I can't be more help :) – David Mar 30 '13 at 13:08
0

Some CSS solved for me:

// Include the main TCPDF library (search for installation path).
require_once('tcpdf/tcpdf.php');

// create new PDF document
$pdf = new TCPDF('R', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Author');
$pdf->SetTitle('TCPDF HTML Table');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, html,table, example, test, guide');

// set default header data
$pdf->SetHeaderData('', '', ' HTML table', '');

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
//$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(15, 15, 15);
$pdf->SetHeaderMargin(15);
$pdf->SetFooterMargin(15);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, 15);

// set image scale factor
//$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// ---------------------------------------------------------
// set font
$pdf->SetFont('times', '', 10);

// add a page
$pdf->AddPage();

$start = 1;
$end = 254;
$step = 1;

$arr = range($start, $end, $step);


$table_header .= sprintf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>", 'IP', 'Computer', 'User', 'Fone');

foreach ($arr as $ar) {
    $row[] = $ar;
}
foreach ($row as $r):
    if (($r % 40) === 0):
        $table_header;
    endif;
    $table .= sprintf("<tr>\n<td>%s</td>\n<td>%s</td>\n<td>%s</td>\n<td>%s</td>\n</tr>\n", $r, $r, $r, $r);
endforeach;

$now = date("d/m/Y");
$caption = "<caption>IP addresses <em>$now</em></caption>\n";
$n = "\n";

$tbl = <<<EOD
<style>
table{
    font-family: serif;
    font-size: 11pt;
}
table tr {

}
table tr td {
    padding:3px;
    border:#000000 solid 1px;
}
em {
    font-size: 4pt;
}
tr { white-space:nowrap; }
</style>

        <h1>{$caption}</h1>
        {$table_begin}
        {$table_header}
        {$table}
</table>
EOD;

$pdf->writeHTML($tbl, true, false, false, false, '');


// reset pointer to the last page
//$pdf->lastPage();
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('html_table.pdf', 'I');

//============================================================+
// END OF FILE
//============================================================+
msmafra
  • 1,654
  • 3
  • 20
  • 34
-1

have you tried

<table>
<thead>
    <tr>
        <td>
            This is my header which appears on every page
        </td>
    </tr>
</thead>
<tr>
    <td>
        My Content
    </td>
</tr>
</table>

I'm using smarty, with this you have more possibilities to manually break the table (e.g. if you're using borders). If needed, i'll post this, too...

manuxi
  • 331
  • 6
  • 11