php-tcpdfHow do I create a table using PHP and TCPDF?
Creating a table using PHP and TCPDF is a relatively easy task. The following example code block will create a basic table with three columns and three rows.
<?php
$pdf = new TCPDF();
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 11);
$pdf->Cell(40, 10, 'Column 1', 1, 0, 'C', 0, '', 0);
$pdf->Cell(40, 10, 'Column 2', 1, 0, 'C', 0, '', 0);
$pdf->Cell(40, 10, 'Column 3', 1, 1, 'C', 0, '', 0);
$pdf->Cell(40, 10, 'Row 1', 1, 0, 'C', 0, '', 0);
$pdf->Cell(40, 10, 'Row 1', 1, 0, 'C', 0, '', 0);
$pdf->Cell(40, 10, 'Row 1', 1, 1, 'C', 0, '', 0);
$pdf->Cell(40, 10, 'Row 2', 1, 0, 'C', 0, '', 0);
$pdf->Cell(40, 10, 'Row 2', 1, 0, 'C', 0, '', 0);
$pdf->Cell(40, 10, 'Row 2', 1, 1, 'C', 0, '', 0);
$pdf->Cell(40, 10, 'Row 3', 1, 0, 'C', 0, '', 0);
$pdf->Cell(40, 10, 'Row 3', 1, 0, 'C', 0, '', 0);
$pdf->Cell(40, 10, 'Row 3', 1, 1, 'C', 0, '', 0);
$pdf->Output('table.pdf', 'I');
?>
This code will generate a PDF file with a table that looks like this:
Column 1 | Column 2 | Column 3 |
---|---|---|
Row 1 | Row 1 | Row 1 |
Row 2 | Row 2 | Row 2 |
Row 3 | Row 3 | Row 3 |
The code uses the TCPDF library to create a PDF document, and the Cell() method to create the table. The parameters of this method are as follows:
$w
: width of the cell$h
: height of the cell$txt
: text to be printed inside the cell$border
: specifies if borders should be drawn around the cell$ln
: specifies where the current position should go after the call$align
: specifies the alignment of the text inside the cell$fill
: specifies if the cell background should be painted$link
: link to be associated with the cell$stretch
: specifies if the cell should be stretched to fit the text
For more information on how to use TCPDF to create tables, please refer to the TCPDF documentation.
More of Php Tcpdf
- How do I install TCPDF using Yum on a PHP server?
- How do I add a watermark to PDFs using PHP and TCPDF?
- How can I generate a PDF from XML data using PHP and TCPDF?
- How do I use the setfont function in PHP TCPDF?
- How can I use PHP and TCPDF to read a PDF?
- How do I install TCPDF with Composer in a PHP project?
- How can I use TCPDF's writeHTML() function in PHP?
- How do I use the setxy function in PHP TCPDF?
- How can I generate a PDF from data stored in a MySQL database using TCPDF and PHP?
- How can I generate a QR code using PHP and TCPDF?
See more codes...