Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

TCPDF: long tables rendered over the header on pages after the 1st


This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 Alhazred


    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 133 posts

Posted 31 August 2013 - 03:37 PM

I'm trying to create an automated routine to create invoices. If there are a lot of items, the table is long and I could need a 2nd page.
I want to have the header in all the pages and the problem is explained in the following screenshots

This is the 1st page, the table begins correctly under the header


The table is long so it continues on the 2nd page, but it starts over the header as you can see here


This is the code which I'm using at the moment

class MYPDF extends TCPDF {

    //Page header
    public function Header() {
        // logo
        // Set font
        $this->SetFont('helvetica', '', 9);
        // Cell backgroung color
        $this->SetFillColor(255, 255, 255);
        // Cell content
        $this->MultiCell(0, 1, PDF_HEADER_TITLE, 0, 'R', 1, 0, '', 3, true);
        // line style
        $style = array('width' => 0.5, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'phase' => 0, 'color' => array(0,205,50));
        // line under the header

    // Page footer
    public function Footer() {
       // margini del footer
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');


$pdf->SetTitle('Shop: Fattura');


$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);



// Set font
$pdf->SetFont('helvetica', '', 9);

/***             TABLE             ***/

$pdf->setCellMargins(0, 0, 0, 0);

$head_style = 'style="border-bottom: 1px solid black;"';
$td1c_style = 'style="border-left:1px solid black;border-right:1px solid black;"';
$td_style     = 'style="border-right:1px solid black;"';
$tabella = '<table border="0" cellpadding="5px">
                    <th width="300" align="center" '.$head_style.'>Descrizione</th>
                    <th width="40" align="right" '.$head_style.'>Qt.</th>
                    <th width="80" align="right" '.$head_style.'>Prezzo IVA escl.</th>
                    <th width="80" align="right" '.$head_style.'>Prezzo IVA incl.</th>

for ($i=0; $i<40; $i++)
    $tabella .=    '<tr>
                    <td '.$td1c_style.'>Denominazione prodotto</td>
                    <td '.$td_style.' align="right">1</td>
                    <td '.$td_style.' align="right">€ '.$p_no_iva.'</td>
                    <td '.$td_style.' align="right">€ '.$p_iva.'</td>

$tabella .= '</table>';

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

$pdf->Output('invoice_example.pdf', 'I');

What should I do to make the table continue under the header on the 2nd and next pages?


#2 Alhazred


    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 133 posts

Posted 01 September 2013 - 12:57 AM

I've solved removing


and setting a proper PDF_MARGIN_TOP value a bit higher than the header.

#3 JasonKnight


    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 312 posts

Posted 03 September 2013 - 06:12 AM

Some advice -- I don't know if TCPDF supports it, but you might want to add THEAD around your initial TH, and properly get SCOPE in there. THEAD should copy the headers to all pages, not just the first one resulting in a easier to follow table.

Oh, and '5px' is an invalid value for cellpadding. px is a CSS metric, not a HTML one.

Likewise, how's it's CSS support? You might want to cut down on the markup by getting rid of all that presentational nonsense to make it easier and a smaller file.

That would gut the markup down to a simple:

$tabella = '
	<table class="products">
				<th scope="col" class="desc">Descrizione</th>
				<th scope="col" class="quantity">Qt.</th>
				<th scope="col">Prezzo IVA escl.</th>
				<th scope="col">Prezzo IVA incl.</th>

for ($i=0; $i<40; $i++) $tablella .= '
				<th scope="row">Denominazione prodotto</th>
				<td>€ '.$p_no_iva.'</td>
				<td>€ '.$p_iva.'</td>

$tabella .= '
With none of that inlined style nonsense bloating out the code. Then you could use this for CSS:

.products {

.products th,
.products td {
	border:solid #000;

.products thead th {
	border-width:0 0 1px;

.products thead th.desc {

.products thead th.quantity {

.products tbody th {
	border-width:0 1px;

.products tbody td {
	border-width:0 1px 0 0;
Resulting in a fraction the markup you are sending to it... That would only get worse the more lines you send!

Edited by JasonKnight, 03 September 2013 - 06:12 AM.

The only thing about Dreamweaver that can be considered professional grade tools are the people promoting it's use.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download