KnpSnappyBundle不会资产图片

时间:2015-03-20 17:59:08

标签: symfony wkhtmltopdf

我在controler中使用:

 $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"'
            )
        );

正常文档示例。 用我的绝对img路径生成HTML

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

但是在pdf中没有显示图像。 如果我保存生成的html并在控制台图像上使用wkhtmltopdf这个文件可以很好地保存在pdf中。 Snappy萌芽的问题就在于此。 Windows 8.1 x64。 怎么了?

1 个答案:

答案 0 :(得分:0)

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

<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().'/..'
        ));
相关问题