TCPDF is an open-source PHP library that is used to generate PDF documents. After downloading TCPDF library, you can invoke all the basic functions of this class to generate your PDF. TCPDF Supports UTF-8, Unicode, RTL languages, XHTML, Javascript, digital signatures, barcodes and much more.
In this blog, I am going to explain you step by step how you can use the TCPDF PHP library to create a PDF document.
Step 1 - Import a TCPDF library in your project:
Step 2 - Create the object of TCPDF:
$pdf=new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
Parameters explanation:
- PDF_PAGE_ORIENTATION:- It is used for page orientation like Portrait Landscape. Possible values are (case insensitive):
- P or Portrait (default)
- L or Landscape
- PDF_UNIT :- Possible values are:
- pt: point
- mm: millimeter (default)
- cm: centimeter
- in: inch
- PDF_PAGE_FORMAT :- The format is used for page size. Possible values are:
- 4A0,2A0,A0,A1,A2,A3,A4 (default),A5,A6,A7,A8,A9,A10
- Unicode :- TRUE means that the input text is unicode (default = true)
- Encoding : - Charset encoding; default is UTF-8
- Encoding : - If TRUE, it reduces the memory usage by caching temporary data on filesystem (slower).
If you want to add custom Header and Footer.
class MYPDF extends TCPDF {
public function Header() {
$image_file = 'st_logo.png';
$this->SetFont(Arial, 'B', 9);
$this->SetTextColor(167, 147, 68);
$this->Image($image_file, 11, 3, 50, 15);
}
public function Footer() {
$this->SetY(-10);
$this->SetFont('helvetica', 'B', 8);
$this->Cell(0, 10, 'https://www.surekhatech.com/', 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
Note: now you can create your MYPDF class object.
$pdf=new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
Step 3 - Call AddPage() method:
Adds a new page to the document. If a page is already present, the Footer() method is called first to output the footer (if enabled). Then the page is added, the current position set to the top-left corner according to the left and top margins (or top-right if in RTL mode), and Header() is called to display the header (if enabled).
Parameters of AddPage() method
- $orientation :- Page orientation. Possible values are (case insensitive):
- P or PORTRAIT (default)
- L or LANDSCAPE
- $format:- The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat()
- $tocpage:- If true set the tocpage state to true (the added page will be used to display Table Of Content)
Step 4 - WriteHTML() method :
WriteHTML() Allows to preserve some HTML formatting (limited support).
$pdf->writeHTML($content);
Supported tags are: a, b, blockquote, br, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, p, hr, i, img, li, ol, p, pre, small, span, strong, sub, sup, table, tcpdf, td, th, thead, tr, tt, u, ul.
NOTE: All the HTML attributes must be enclosed in double-quotes.
Parameters of writeHTML() method
- @param float :W, H, X, Y, :- cell width, Height, upper-left corner X coordinate, upper-left corner Y coordinate.
- @param string :html :-text to display.
- @param mixed: border:-Indicates if borders must be drawn around the cell
- The value can be either a number: 0 - no border (default), 1 - frame
- or a string: L - left, T - top, R - right, B - bottom
- @param boolean :ln :- If true add a new line after text (default = true)
- @param int :fill :- Indicates if the background must be painted (1:true) or transparent (0:false).
- @param boolean: reseth:- if true reset the last cell height (default false).
- @param boolean: cell:- if true add the default c_margin space to each Write (default false).
- @param string: align:- Allows to center or align the text. Possible values are:
- L: left align
- C: center
- R: right align
- “”: empty string: left for LTR or right for RTL
Step 5 - Output() method:
It also provides the Output method to output the generated pdf as required.
$pdf->Output('surekhatech.pdf', ‘S’);
Parameters of Output() method
- @param string: name:- The name of the file when saved
- @param string: dest:- Destination where to send the document. It can take one of the following values:
- I:- send the file inline to the browser (default).
- D:- send to the browser and force a file download.
- F:- It allows the generated pdf to be saved as a local file.
- S:- return the document as a string.
- FI:- equivalent to F + I option.
- FD:- equivalent to F + D option.
NOTE: I have generated a sample Pdf using TCPDF and phpqrcode library. I have attached screenshot for the same.