Symfony:使用样式表和PdfBundle

时间:2013-10-02 13:20:41

标签: php symfony pdf-generation

PdfBundle的实现中,向Pdf()注释添加样式表既不会引发错误也不会被使用。呈现的页面是默认的8.5 x 11,而不是预期的5 x 8.使用随机字符替换样式表文件名不会引发错误。是否需要其他配置才能利用样式表?

控制器:

   /**
     * @Pdf(stylesheet="ManaClientBundle:Test:pdfstyle.xml.twig",
     * @Route("/card")
     */
    public function cardAction() {
        $em = $this->getDoctrine()->getManager();
        $household = $em->getRepository('ManaClientBundle:Household')->find(8607);
        $facade = $this->get('ps_pdf.facade');
        $response = new Response();
        $this->render('ManaClientBundle:Test:card.pdf.twig', array(
            'household' => $household,
            'date' => date_create(),
        ), $response);
        $xml = $response->getContent();
        $content = $facade->render($xml);
        return new Response($content, 200, array('content-type' => 'application/pdf'));
    }

模板(在... / Resources / views / Test /中)

<pdf>
    <page id="card">
    ...
    </page>
</pdf>

样式表在... / Resources / views / Test / pdfstyle.xml.twig

<stylesheet>
    <page id="card" page-size="8in:5in"  margin=".5in" font-size="12">
    </page>
</stylesheet>

1 个答案:

答案 0 :(得分:0)

来自该作者:

如果直接使用$ facade对象,则不需要Pdf注释。如果要以隐式方式使用pdf渲染,则应使用Pdf注释。在你的代码中你应该传递样式表xml作为$ facade-&gt; render(...)方法的第二个参数。

控制器现在显示:

   /**
     * @Route("/card")
     */
    public function cardAction() {
        $em = $this->getDoctrine()->getManager();
        $household = $em->getRepository('ManaClientBundle:Household')->find(8607);

        $stylesheetXml = $this->renderView('ManaClientBundle:Test:pdfstyle.xml.twig', array());

        $facade = $this->get('ps_pdf.facade');
        $response = new Response();

        $this->render('ManaClientBundle:Test:card.pdf.twig', array(
            'household' => $household,
            'date' => date_create(),
        ), $response);
        $xml = $response->getContent();
        $content = $facade->render($xml, $stylesheetXml);
        return new Response($content, 200, array('content-type' => 'application/pdf'));
    }