Symfony2 - PdfBundle 不工作 [英] Symfony2 - PdfBundle not working

查看:46
本文介绍了Symfony2 - PdfBundle 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Symfony2 和 PdfBundle 生成动态 PDF 文件,我确实无法生成文件.

Using Symfony2 and PdfBundle to generate dynamically PDF files, I don't get to generate the files indeed.

按照文档说明,我已经设置了所有捆绑包:

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:

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

... new 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,
    ));
}

还有这个树枝文件:

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

嗯.不知何故,我刚刚在我的页面中得到的只是生成的普通 html,就好像它是普通的 Response 渲染一样.

Well. Somehow, what I just get in my page is just the normal html generated as if it was a normal Response rendering.

Pdf() 注释应该提供创建 PDF 文件而不是呈现普通 HTML 的特殊"行为.

The Pdf() annotation is supposed to give the "special" behavior of creating the PDF file instead of rendering normal HTML.

因此,有了上面的代码,当我请求路由 http://www.mysite.com/*...*/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:

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

(所以是一个空白的 HTML 页面,上面只有 Hello Pedro! 字样.

(so a blank HTML page with just the words Hello Pedro! on it.

有什么线索吗?我做错了什么吗?除了 *.pdf.twig 版本之外,是否必须有替代 *.html.twig 版本?我不这么认为... :(

Any clue? Am I doing anything wrong? Is it mandatory to have the alternative *.html.twig apart from the *.pdf.twig version? I don't think so... :(

推荐答案

好的,我知道了.

出于某种原因,捆绑文档中的示例对我不起作用.尽管如此,de bundle 中有这个类:http://github.com/psliwa/PdfBundle/blob/master/Controller/ExampleController.php,我可以在其中找到对我有用的示例.这是我最终使用的代码:

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'));
}   

下一个挑战:将该 PDF 存储到磁盘中.

Next challenge: store that PDF into disk.

这篇关于Symfony2 - PdfBundle 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆