Angel
Angel

Reputation: 357

How to create PDF on Symfony 2.4 using PHPpdf Bundle

I have installed correctly using composer.phar and added as well in AppKernel.php. Now I'm trying to do an easy pdf to see how it works, but when I make it Google Chrome shows me this error:

There has been an error loading pdf

For the installation of bundle I followed this steps (in spanish but easy to understand): http://symfony.es/bundles/psliwa/pdfbundle/instalacion-en-symfony-2-1

My routing file shows like:

pdf_hello:
    pattern: /{_locale}/hello/{name}.{_format}
    defaults: { _controller: AcmeClientBundle:Default:pdf, _format: html}
    requirements:
        _format: html|pdf

My controller:

public function pdfAction($name){
    $format = $this->get('request')->get('_format');

    $content = $this->render(
        sprintf('AcmeClientBundle:Default:helloAction.%s.twig', $format),
        array('name' => $name)
    );

    $contentType = 'pdf' == $format ? 'application/pdf' : 'text/html';

    $response = new Response($content, 200, array('content-type' => $contentType));

    return $response;
}

And my views:

{# hello.html.twig #}
Hello <b>{{ name }}</b>!

{# hello.pdf.twig #}
<pdf>
    <dynamic-page>
        Hello <b>{{ name }}</b>!
    </dynamic-page>
</pdf>

Any ideas?

Thanks!

Upvotes: 0

Views: 805

Answers (1)

amcastror
amcastror

Reputation: 568

I have it working. There are two things I didn't see in your code:

use PHPPdf\Core\FacadeBuilder;
use Ps\PdfBundle\Annotation\Pdf;

And

/**
* @Pdf()
*/
public function pdfAction($name){
...
}

Hope this helps.

Upvotes: 2

Related Questions