Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scale an HTML table to fit onto a PDF page in TCPDF

Tags:

php

tcpdf

Is it possible to render a HTML table into PDF using TCPDF and scale it to fit the page? I have many columns in my table. Would it be possible to zoom out or scale it down so it fits?

coloured table cut off by edge of PDF page

like image 367
Raioneru Avatar asked Nov 29 '12 08:11

Raioneru


2 Answers

TCPDF allows you to write HTML. This way, your table structure does not matter.

Example:

$tbl = <<<EOD
<table border="1">
<tr>
<th rowspan="3">Left column</th>
<th colspan="5">Heading Column Span 5</th>
<th colspan="9">Heading Column Span 9</th>
</tr>
<tr>
<th rowspan="2">Rowspan 2<br />This is some text that fills the table cell.</th>
<th colspan="2">span 2</th>
<th colspan="2">span 2</th>
<th rowspan="2">2 rows</th>
<th colspan="8">Colspan 8</th>
</tr>
<tr>
<th>1a</th>
<th>2a</th>
<th>1b</th>
<th>2b</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
</tr>
</table>
EOD;

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

See Full Full Code | See PDF Output

like image 128
Baba Avatar answered Oct 22 '22 10:10

Baba


Send the page dimensions to avoid that. Set your array variable with width and height like this.

$pageDimension = array('500,300'); //width,height

Substitute this array value where you use this pre-defined TCPDF variable (PDF_PAGE_FORMAT)

Replace PDF_PAGE_FORMAT with $pageDimension

like image 38
Shankar Narayana Damodaran Avatar answered Oct 22 '22 11:10

Shankar Narayana Damodaran