php-tcpdfHow can I use TCPDF's writeHTML() function in PHP?
The writeHTML()
function in TCPDF is used to render HTML code into a PDF document. It takes two parameters: the HTML code and a boolean indicating whether to use the default font or not. Here is an example of how to use it:
<?php
// Include the TCPDF library
require_once('tcpdf/tcpdf.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('My App');
$pdf->SetAuthor('Me');
$pdf->SetTitle('My PDF Document');
// Add a page
$pdf->AddPage();
// Render HTML code
$html = '<h1>Hello World!</h1>';
$pdf->writeHTML($html, true);
// Output the PDF
$pdf->Output('my_document.pdf', 'I');
This code will generate a PDF document with a single page containing the text Hello World!
.
Code explanation
require_once('tcpdf/tcpdf.php');
: This includes the TCPDF library in the script.$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
: This creates a new PDF document.$pdf->SetCreator('My App');
: This sets the document creator.$pdf->SetAuthor('Me');
: This sets the document author.$pdf->SetTitle('My PDF Document');
: This sets the document title.$pdf->AddPage();
: This adds a page to the document.$html = '<h1>Hello World!</h1>';
: This creates a string containing the HTML code to be rendered.$pdf->writeHTML($html, true);
: This renders the HTML code into the PDF document.$pdf->Output('my_document.pdf', 'I');
: This outputs the PDF document.
For more information, see the TCPDF Documentation.
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 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...