TCPDF is a PHP library which is used to generate PDF. FPDF is an open source library. You can use it for any kind of usage and modify it to suit your needs. It started in 2002. TCPDF is widely used to generate PDF on the fly from HTML in PHP due to its simplicity. One of the most awesome features of TCPDF is that you can embed almost any kind of Font (TrueType format), however not every developer knows how to do it easily. In this article, we want to show you how easily is to add a new custom font to TCPDF.In this tutorial, we will show you how to create a PDF helper using TCPDF library. Our focus will be how to create the helper class, if you want to find out more about TCPDF library as well as its usage. Please visit its official site at http://www.tcpdf.org.
Table Of Content
- Download TCPDF from Sourceforge.
- Unpack downloaded folder, and copy it to directory 'app/vendors/tcpdf'
2.PDF Helper class
Create 'pdf.php' and place it to directory 'app/views/helpers/pdf.php'. Below is the code for the class:
- Import the TCPDF class from vendor folder.
- Extend PdfHelper from AppHelper class.
From the code above, we can tell PdfHelper class is actually just a wrapper class for TCPDF. Whenever you want to use TCPDF's functions, you simply do:
Let us test out this helper class.
- Create a layout file 'app/views/layouts/pdf.ctp':
- Create a PdfsController class, and include PDF Helper. We will also create a index function for generating pdf:
- Now let us create a view file 'app/views/pdfs/index.ctp', this file will utilize our PDF Helper class to generate a PDF:
- Now go to your browser and type in address to access /pdfs/index. You should be prompted to download a PDF file.
Hopefully this simple tutorial helped you with your development.
If you like our post, please follow us on Twitter and help spread the word. We need your support to continue.
If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.
You might also want to check out course Up and Running with CakePHP from lynda.com. Rhs veg planner 2020.
Started in 2002, TCPDF is now one of the world'smost active Open Source projects, used daily by millions of usersand included in thousands of CMS and Web applications. Checkthe examples..
A new version of this library is under development athttps://github.com/tecnickcom/tc-lib-pdfand as a consequence the old version will not receive anyadditional development or support.
Please consider supporting this project by making adonation via PayPal
- no external libraries are required for the basicfunctions;
- all standard page formats, custom page formats, custom marginsand units of measure;
- UTF-8 Unicode and Right-To-Left languages;
- TrueTypeUnicode, OpenTypeUnicode, TrueType, OpenType, Type1 andCID-0 fonts;
- font subsetting;
- images, graphic (geometric figures) and transformationmethods;
- supports JPEG, PNG and SVG images natively, all imagessupported by GD (GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM)and all images supported via ImagMagick (http:www.imagemagick.org/www/formats.html)
- 1D and 2D barcodes: CODE 39, ANSI MH10.8M-1983, USD-3, 3 of 9,CODE 93, USS-93, Standard 2 of 5, Interleaved 2 of 5, CODE 128A/B/C, 2 and 5 Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A,UPC-E, MSI, POSTNET, PLANET, RMS4CC (Royal Mail 4-state CustomerCode), CBC (Customer Bar Code), KIX (Klant index - Customer index),Intelligent Mail Barcode, Onecode, USPS-B-3200, CODABAR, CODE 11,PHARMACODE, PHARMACODE TWO-TRACKS, Datamatrix ECC200, QR-Code,PDF417;
- ICC Color Profiles, Grayscale, RGB, CMYK, Spot Colors andTransparencies;
- automatic page header and footer management;
- document encryption up to 256 bit and digital signaturecertifications;
- transactions to UNDO commands;
- PDF annotations, including links, text and fileattachments;
- text rendering modes (fill, stroke and clipping);
- multiple columns mode;
- no-write page regions;
- bookmarks and table of content;
- text hyphenation;
- text stretching and spacing (tracking/kerning);
- automatic page break, line break and text alignments includingjustification;
- automatic page numbering and page groups;
- move and delete pages;
- page compression (requires php-zlib extension);
- XOBject templates;
- PDF/A-1b (ISO 19005-1:2005) support.