Bad characters when creating pdf file using Knp Snappy

I am using Symfony2. When a pdf file is created using this code:

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

I get this content (French characters are displayed with bad characters): enter image description here

+5
source share
2 answers

try adding the encoding property

 'encoding' => 'utf-8', 

heres a complete copy of my working code, note that I am passing an array of parameters as the second argument to 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"' ) ); 
+12
source

If you use the generateFromHtml method, you should use it as follows: third parameter:

 $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', ) ); 
+1
source

All Articles