简体   繁体   English

Symfony:将样式表与PdfBundle一起使用

[英]Symfony: Use stylesheet with PdfBundle

In an implementation of PdfBundle , adding a stylesheet to the Pdf() annotation neither throws an error or is used. PdfBundle的实现中,将样式表添加到Pdf()批注既不会引发错误,也不会被使用。 The page rendered is a default 8.5 x 11, not the expected 5 x 8. Replacing the stylesheet file name with random characters does not elicit an error. 呈现的页面默认为8.5 x 11,而不是预期的5 x8。用随机字符替换样式表文件名不会引发错误。 Is other configuration required to take advantage of a stylesheet? 要使用样式表是否需要其他配置?

Controller: 控制器:

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

Template (in .../Resources/views/Test/) 模板(在... /资源/视图/测试/中)

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

Stylesheet in .../Resources/views/Test/pdfstyle.xml.twig ... /资源/视图/测试/pdfstyle.xml.twig中的样式表

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

From the author of the bundle: 来自捆绑包的作者:

If you use $facade object directly, Pdf annotation is unnecessary. 如果直接使用$ facade对象,则不需要Pdf批注。 You should use Pdf annotation when you want to use pdf rendering in implicit way. 如果要隐式使用pdf渲染,则应使用Pdf批注。 In you code you should pass stylesheet xml as second argument of $facade->render(...) method. 在代码中,应将样式表xml作为$ facade-> render(...)方法的第二个参数传递。

Controller now reads: 控制器现在读取:

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

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

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