2015-05-18 57 views
5

我正在使用Symfony2。当使用此代码生成的PDF文件:使用Knp Snappy生成PDF文件时出现错误字符

public function printAction($id) 
    { 
     // initialiser $demande 
     $html = $this->renderView('PFETimeBundle:Demande:print.html.twig', 
      array('demande'=> $demande) 
     ); 

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

我得到这个内容(法文字符出现在坏人的角色): enter image description here

回答

12

尝试添加encoding财产

'encoding' => 'utf-8', 

继承人我的工作代码的完整副本,请注意,我将一个选项数组作为第二个参数传递给getOutPutFromHtml()

 return new Response(
      $this->get('knp_snappy.pdf')->getOutputFromHtml($html, array(
       'orientation' => 'landscape', 
       'enable-javascript' => true, 
       'javascript-delay' => 1000, 
       'no-stop-slow-scripts' => true, 
       'no-background' => false, 
       'lowquality' => false, 
       'encoding' => 'utf-8', 
       'images' => true, 
       'cookie' => array(), 
       'dpi' => 300, 
       'image-dpi' => 300, 
       'enable-external-links' => true, 
       'enable-internal-links' => true 
      )), 
      200, 
      array(
       'Content-Type'   => 'application/pdf', 
       'Content-Disposition' => 'attachment; filename="report.pdf"' 
      ) 
     ); 
+0

非常感谢你。它为我工作。 – mehdi

+0

np,很高兴帮助! –

+0

谢谢你。你已经为我节省了大量的时间去了解如何传递选项以及通过哪些选项来设置'encoding' –

1

如果您使用的是generateFromHtml方法,你必须使用它像这样,在第三个参数:

$this->container->get('knp_snappy.pdf')->generateFromHtml(
    $this->container->get('templating')->render(
     'YourBundle:Template:pdfTemplate.html.twig', 
     array(
      'var' => $var, 
     ) 
    ), 
    '/path/to/file.pdf', 
    array(
     'encoding' => 'utf-8', 
    ) 
); 
相关问题