php-tcpdfHow can I output a PDF file using PHP and TCPDF?
Using the TCPDF library, you can output a PDF file using PHP. Here is an example code block to demonstrate this:
<?php
// Include the TCPDF library
require_once('tcpdf_include.php');
// Create a new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// Set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Author Name');
$pdf->SetTitle('PDF Document Title');
// Add a page
$pdf->AddPage();
// Set some content to print
$html = <<<EOD
<h1>PDF Document</h1>
<p>This is a sample PDF document.</p>
EOD;
// Print text using writeHTMLCell()
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
// Output the PDF document
$pdf->Output('example_001.pdf', 'I');
?>
This code will output a PDF file named example_001.pdf
.
The code consists of the following parts:
require_once('tcpdf_include.php');
: This will include the TCPDF library.$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
: This will create a new PDF document.$pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('Author Name'); $pdf->SetTitle('PDF Document Title');
: This will set the document information.$pdf->AddPage();
: This will add a page to the PDF document.$html = <<<EOD ... EOD;
: This will set some content to print.$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
: This will print the content using writeHTMLCell().$pdf->Output('example_001.pdf', 'I');
: This will output the PDF document.
Helpful links
More of Php Tcpdf
- How do I install TCPDF using Yum on a PHP server?
- How do I use the PHP TCPDF WriteHTMLCell function?
- How do I add a watermark to PDFs using PHP and TCPDF?
- How do I use the setfont function in PHP TCPDF?
- How can I generate a QR code using PHP and TCPDF?
- 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 do I set the page orientation when using PHP TCPDF?
- How do I set the margins using PHP TCPDF?
See more codes...