Symfony2 - PdfBundle not working

可紊 提交于 2019-12-05 22:05:56

Ok I got it.

For some reason, the example that comes in the bundle documentation didn't work for me. Nevertheless, there is this class in de bundle: http://github.com/psliwa/PdfBundle/blob/master/Controller/ExampleController.php, where I could find an example that did work for me. This is the code that I finally used:

/**
 * @Route ("/generateInvoice", name="_generate_invoice")
 */
public function generateInvoiceAction($name = 'Pedro')
{
    $facade = $this->get('ps_pdf.facade');
    $response = new Response();
    $this->render('AcmeStoreBundle:Shop:generateInvoiceAction.pdf.twig', array("name" => $name), $response);

    $xml = $response->getContent();

    $content = $facade->render($xml);

    return new Response($content, 200, array('content-type' => 'application/pdf'));
}   

Next challenge: store that PDF into disk.

It's because you've missed the "_format" option in the URL.

$this->render() shouldn't be used with the @Template annotation. The @Template will serve the correct template's format depending of the _format parameter.

...
use Ps\PdfBundle\Annotation\Pdf;
...

/**
 * @Pdf()
 * @Route ("/pdf.{_format}", name="_pdf")
 * @Template()
 */
public function generateInvoicePDFAction($name = 'Pedro')
{
    return array('name' => $name);
}

Should work fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!