简体   繁体   English

Symfony2 - PdfBundle无法正常工作

[英]Symfony2 - PdfBundle not working

Using Symfony2 and PdfBundle to generate dynamically PDF files, I don't get to generate the files indeed. 使用Symfony2和PdfBundle生成动态PDF文件,我确实无法生成文件。

Following documentation instructions, I have set up all the bundle thing: 按照文档说明,我已经设置了所有捆绑的东西:

autoload.php: autoload.php:

'Ps' => __DIR__.'/../vendor/bundles',
'PHPPdf' => __DIR__.'/../vendor/PHPPdf/lib',
'Imagine' => array(__DIR__.'/../vendor/PHPPdf/lib', __DIR__.'/../vendor/PHPPdf/lib/vendor/Imagine/lib'),
'Zend' => __DIR__.'/../vendor/PHPPdf/lib/vendor/Zend/library',
'ZendPdf' => __DIR__.'/../vendor/PHPPdf/lib/vendor/ZendPdf/library',

AppKernel.php: AppKernel.php:

... new Ps\\PdfBundle\\PsPdfBundle(), ... ...新的Ps \\ PdfBundle \\ PsPdfBundle(),...

I guess all the setting up is correctly configured, as I am not getting any "library not found" nor anything on that way... 我想所有的设置都配置正确,因为我没有得到任何“库未找到”,也没有任何东西......

So, after all that, I am doing this in the controller: 所以,毕竟,我在控制器中这样做:

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

/**
 * @Pdf()
 * @Route ("/pdf", name="_pdf")
 * @Template()
 */
public function generateInvoicePDFAction($name = 'Pedro')
{
    return $this->render('AcmeStoreBundle:Shop:generateInvoice.pdf.twig', array(
        'name' => $name,
    ));
}

And having this twig file: 有这个twig文件:

<pdf>
    <dynamic-page>
        Hello {{ name }}!
    </dynamic-page>
</pdf>

Well. 好。 Somehow, what I just get in my page is just the normal html generated as if it was a normal Response rendering. 不知何故,我在页面中得到的只是正常的html生成,好像它是一个正常的Response呈现。

The Pdf() annotation is supposed to give the "special" behavior of creating the PDF file instead of rendering normal HTML. Pdf()注释应该给出创建PDF文件的“特殊”行为,而不是呈现普通的HTML。

So, having the above code, when I request the route http://www.mysite.com/*...*/pdf , all what I get is the following HTML rendered: 所以,有了上面的代码,当我请求路由http://www.mysite.com/*...*/pdf ,我得到的是以下HTML渲染:

<pdf>
    <dynamic-page>
        Hello Pedro!
    </dynamic-page>
</pdf>

(so a blank HTML page with just the words Hello Pedro! on it. (所以一个空白的HTML页面上只有Hello Pedro!字样。

Any clue? 任何线索? Am I doing anything wrong? 我做错了吗? Is it mandatory to have the alternative *.html.twig apart from the *.pdf.twig version? 除了* .pdf.twig版本之外,是否必须使用替代* .html.twig? I don't think so... :( 我不这么认为...... :(

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. 然而,在de bundle中有这个类: http//github.com/psliwa/PdfBundle/blob/master/Controller/ExampleController.php ,在那里我可以找到一个对我有用的例子。 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. 下一个挑战:将PDF存储到磁盘中。

It's because you've missed the "_format" option in the URL. 这是因为你错过了URL中的“_format”选项。

$this->render() shouldn't be used with the @Template annotation. $this->render()不应与@Template注释一起使用。 The @Template will serve the correct template's format depending of the _format parameter. @Template将根据_format参数提供正确的模板格式。

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

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

Should work fine. 应该工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM