Learn how to create pdf files with php in symfony 2 & 3 with tcpdf

How to create a pdf file with php in symfony 2 & 3 with tcpdf

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

"whiteoctober/tcpdf-bundle": "dev-master",

If you use composer directly in the console then execute :

$ composer require whiteoctober/tcpdf-bundle

when the required component has been downloaded simply add the bundle to your kernel (AppKernel.php file located in /app/AppKernel.php) so :

$bundles = array(
        // ...
        new WhiteOctober\TCPDFBundle\WhiteOctoberTCPDFBundle(),// register bundle
    );

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 :

public function returnPDFResponseFromHTML($html){
        //set_time_limit(30); uncomment this line according to your needs
        // If you are not in a controller, retrieve of some way the service container and then retrieve it
        //$pdf = $this->container->get("white_october.tcpdf")->create('vertical', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        //if you are in a controlller use :
        $pdf = $this->get("white_october.tcpdf")->create('vertical', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        $pdf->SetAuthor('Our Code World');
        $pdf->SetTitle(('Our Code World Title'));
        $pdf->SetSubject('Our Code World Subject');
        $pdf->setFontSubsetting(true);
        $pdf->SetFont('helvetica', '', 11, '', true);
        //$pdf->SetMargins(20,20,40, true);
        $pdf->AddPage();
        
        $filename = 'ourcodeworld_pdf_demo';
        
        $pdf->writeHTMLCell($w = 0, $h = 0, $x = '', $y = '', $html, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = '', $autopadding = true);
        $pdf->Output($filename.".pdf",'I'); // This will output the PDF as a response directly
}

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 :

public function indexAction(){
    // You can send the html as you want
   //$html = '<h1>Plain HTML</h1>';

    // but in this case we will render a symfony view !
    // We are in a controller and we can use renderView function which retrieves the html from a view
    // then we send that html to the user.
    $html = $this->renderView(
         'Templates/template.html.twig',
         array(
          'someDataToView' => 'Something'
         )
    );
  
    $this->returnPDFResponseFromHTML($html);
}

TCPDF makes the things really easy for the developer. You can read the official TCPDF documentation here and all the examples here.


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors