【问题标题】:KnpSnappyBundle dont asset imagesKnp Snappy Bundle 没有资产图像
【发布时间】:2015-03-20 17:59:08
【问题描述】:

我在控制器中使用:

 $html = $this->renderView('RaportBundle:pdf:magazinesbarcode.html.twig', array(
            'magazines'  => $magazines
        ));

        return new Response(
            $this->get('knp_snappy.pdf')->getOutputFromHtml($html,
                array('images'=> true,'enable-external-links' => true)
            ),
            200,
            array(
                'images' =>true,
                'Content-Type'          => 'application/pdf',
                'Content-Disposition'   => 'attachment; filename="file.pdf"'
            )
        );

普通文档示例。 HTML 生成良好,使用我的绝对 img 路径

<img src="{{ asset('uploads/barcode/2.jpg', absolute=true) }}" /> 

但在 pdf 中没有显示图像。 如果我保存生成的 html 并使用 wkhtmltopdf 从控制台图像中的这个文件保存在 pdf 中。 所以问题出在 Snappy budle 中。 视窗 8.1 x64。 怎么了?

【问题讨论】:

    标签: symfony wkhtmltopdf


    【解决方案1】:

    您应该使用图像的绝对本地路径:

    <img src="{{ rootDir ~ '/web' ~ asset('uploads/barcode/2.jpg') }}">
    

    rootDir 传递给模板的地方:

    $html = $this->renderView('RaportBundle:pdf:magazinesbarcode.html.twig', array(
                'magazines'  => $magazines,
                'rootDir' => $this->get('kernel')->getRootDir().'/..'
            ));
    

    【讨论】:

    • 从 Symfony 2.5 开始,您也可以对绝对路径执行此操作:&lt;img src="{{ asset('uploads/barcode/2.jpg', absolute=true) }}" /&gt;
    • 所以我使用绝对路径,阅读我的问题。 Symfony 2.6.3.Absolute 路径生成良好但不起作用。 @dmnptr 解决方案,也没有用。
    • 我的解决方案很适合我,使用 Symfony 2.3。检查文件权限,以确保不是原因。
    猜你喜欢
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 2014-03-28
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多