【问题标题】:Bad characters when generating pdf file with Knp Snappy使用 Knp Snappy 生成 pdf 文件时出现错误字符
【发布时间】:2015-07-29 23:00:20
【问题描述】:

我正在使用 Symfony2。当使用此代码生成 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"'
                )
            );
    }

我得到了这个内容(法语字符出现在坏字符中):

【问题讨论】:

    标签: php symfony pdf pdf-generation wkhtmltopdf


    【解决方案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',
        )
    );
    

    【讨论】:

      【解决方案2】:

      尝试添加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"'
                  )
              );
      

      【讨论】:

      • 谢谢。您节省了我很多时间来查看代码以弄清楚如何传递选项以及传递哪个选项来设置encoding
      猜你喜欢
      • 2014-03-28
      • 2015-05-15
      • 1970-01-01
      • 2019-06-30
      • 2013-11-22
      • 2014-09-29
      • 1970-01-01
      • 2016-09-04
      • 1970-01-01
      相关资源
      最近更新 更多