TCPDF is now one of the world's most active Open Source projects, used daily by millions of users and included in thousands of CMS and Web applications to generate pdf files with php.
To use tcpdf in symfony 2, we will add a bundle which makes the implementation easier for us. In symfony 2, is a good practice use bundles instead using require once in a controller to include our library. The creator of this bundle (not the PHPExcel library) is WhiteOctober and the source code can be viewed in the official repository in github.
To install our bundle we will add in the composer.json file in the require zone
If you use composer directly in the console then execute :
when the required component has been downloaded simply add the bundle to your kernel (AppKernel.php file located in /app/AppKernel.php) so :
Creating a PDF from HTML
TCPDF allows you to create a pdf from html markup (awesome isn't ?). To return a PDF in a response, use the following code :
The previous example returns a PDF response directly in the browser, now if we want to use this function in a symfony controller we can simply call the function in any line. Yes, you don't need a special symfony response when you use the Output as a online PDF, this is handled by the TCPDF library. Then in our controller we can use :
TCPDF makes the things really easy for the developer. You can read the official TCPDF documentation here and all the examples here.
3 Comments