2015-05-18 58 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', 
    ) 
); 
相關問題