How to install TCPDF to symfony 2.7

5.5k Views Asked by At

I want generata PDF file by symfony 2, so I use

composer require tecnick.com/tcpdf

It created new folder name tecnick.com/tcpdf in my Vendors folder So how I call tcpdf class?

2

There are 2 best solutions below

1
On

There is documentation for this available : TCPDF

0
On

This bundle uses the last version of TCPDF from the official website. The installation as Symfony bundle is different (from the regular installation) because you're using it with the Symfony framework. In this case you won't be using require_once etc. because Symfony does that for you.

For general information, consider reading the official bundle documentation

First add this to the require section of your projects composer.json

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

Then execute composer install.

When is finished, register the bundle in the app/AppKernel.php:

<?php

 public function registerBundles()
 {
     $bundles = array(
      // ...
         new WhiteOctober\TCPDFBundle\WhiteOctoberTCPDFBundle(),
      );
 }

Now you can use TCPDF on your controllers, for example:

class MyController extends Controller {
    ...

    public function mypdfAction(Request $request){

        $pdf = $this->container->get("white_october.tcpdf")->create(
            'LANDSCAPE',
            PDF_UNIT,
            PDF_PAGE_FORMAT,
            true,
            'UTF-8',
            false
        );
        $pdf->SetAuthor('qweqwe');
        $pdf->SetTitle('Prueba TCPDF');
        $pdf->SetSubject('Your client');
        $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
        $pdf->setFontSubsetting(true);

        $pdf->SetFont('helvetica', '', 11, '', true);
        $pdf->AddPage();

        $html = '<h1>Working on Symfony</h1>';

        $pdf->writeHTMLCell(
            $w = 0,
            $h = 0,
            $x = '',
            $y = '',
            $html,
            $border = 0,
            $ln = 1,
            $fill = 0,
            $reseth = true,
            $align = '',
            $autopadding = true
        );

        $pdf->Output("example.pdf", 'I');
    }
}