Bad characters when generating pdf file with Knp Snappy
Asked Answered
S

2

7

I am using Symfony2. When the pdf file is generated 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 appear in bad characters) : enter image description here

Sentimentality answered 18/5, 2015 at 12:27 Comment(0)
M
17

try to add the encoding property

'encoding' => 'utf-8',

heres a full copy of my working code, pls note that i pass an options array as 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"'
            )
        );
Mazman answered 18/5, 2015 at 12:45 Comment(1)
Thanks for this. You've saved me a lot of time of going through the code to figure out how to pass options and which option to pass to set the encodingSnowden
C
1

If you are using the generateFromHtml method, you have to use it like this, at 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',
    )
);
Caressa answered 15/6, 2017 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.