Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

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

tcpdf

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

#1 Alhazred

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

pegc.JPG

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

k3ce.JPG

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

class MYPDF extends TCPDF {

    //Page header
    public function Header() {
        // logo
        $this->Image(PDF_HEADER_LOGO,15,10,35,0,'','','T',false,72,'',false,false,1,false,false,false);
        
        // 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
        $this->line(10,35,200,35,$style);
    }

    // Page footer
    public function Footer() {
       // margini del footer
        $this->SetY(-15);
        // 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 = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor(PDF_AUTHOR);
$pdf->SetTitle('Shop: Fattura');
$pdf->SetSubject('Fattura');
$pdf->SetKeywords('');

$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

$pdf->AddPage();

$pdf->Ln(10);

// 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">
                <tr>
                    <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>
                </tr>';

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>
                </tr>';
}

$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

Alhazred

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 133 posts

Posted 01 September 2013 - 12:57 AM

I've solved removing

$pdf->Ln(10);

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



#3 JasonKnight

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">
		<thead>
			<tr>
				<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>
			</tr>
		</thead><tbody>';

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

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

.products {
	border-collapse:collapse;
	border:0;
}

.products th,
.products td {
	padding:5px;
	border:solid #000;
}

.products thead th {
	width:80px;
	text-align:right;
	border-width:0 0 1px;
}

.products thead th.desc {
	width:300px;
	text-align:center;
}

.products thead th.quantity {
	width:40px;
}

.products tbody th {
	font-weight:normal;
	text-align:left;
	border-width:0 1px;
}

.products tbody td {
	text-align:right;
	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