【问题标题】:knp snappy bundle and wkhtmltopdf ContextErrorExceptionknp snappy bundle 和 wkhtmltopdf ContextErrorException
【发布时间】:2013-12-15 19:52:27
【问题描述】:

我已经用 composer 安装了 knp snappy bundle,并按照https://github.com/KnpLabs/KnpSnappyBundle 上的使用和配置教程进行操作

我已经安装了 wkhtmltopdf-0.9.9-installer.exe 并更改了 config.yml 文件中的默认路径

# knp_snappy Configuration
knp_snappy:
    pdf:
        enabled:    true
        binary:     C:\Program Files (x86)\wkhtmltopdf
        options:    []

这里是函数

/**
 * @Route("/print", name="invite_print")
 * @Method("GET")
 */
public function printAction() {
    $entity = new Invite();
    $form = $this->createCreateForm($entity);

    $html = $this->renderView('PrifProtocoleBundle:Invite:new.html.twig', array(
        'entity' => $entity,
        'form' => $form->createView()
    ));

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

但不幸的是,我有这些错误消息:

错误 1:

ContextErrorException: Warning: json_encode(): Invalid UTF-8 sequence in argument in  C:\wamp\www\protocole\app\cache\dev\classes.php line 4592

错误 2:

ContextErrorException: Warning: json_encode(): Invalid UTF-8 sequence in argument in C:\wamp\www\protocole\vendor\monolog\monolog\src\Monolog\Formatter\NormalizerFormatter.php line 132

谁能告诉我如何解决这个问题?谢谢

json.php文件中的json_encode函数(奇怪为空)

/**
 * (PHP 5 &gt;= 5.2.0, PECL json &gt;= 1.2.0)<br/>
 * Returns the JSON representation of a value
 * @link http://php.net/manual/en/function.json-encode.php
 * @param mixed $value <p>
 * The <i>value</i> being encoded. Can be any type except
 * a resource.
 * </p>
 * <p>
 * This function only works with UTF-8 encoded data.
 * </p>
 * @param int $options [optional] <p>
 * Bitmask consisting of <b>JSON_HEX_QUOT</b>,
 * <b>JSON_HEX_TAG</b>,
 * <b>JSON_HEX_AMP</b>,
 * <b>JSON_HEX_APOS</b>,
 * <b>JSON_NUMERIC_CHECK</b>,
 * <b>JSON_PRETTY_PRINT</b>,
 * <b>JSON_UNESCAPED_SLASHES</b>,
 * <b>JSON_FORCE_OBJECT</b>,
 * <b>JSON_UNESCAPED_UNICODE</b>. The behaviour of these
 * constants is described on
 * the JSON constants page.
 * </p>
 * @param int $depth [optional]
 * @return string a JSON encoded string on success or <b>FALSE</b> on failure.
 */
 function json_encode ($value, $options = 0, $depth = 512) {}

错误 2 的 NormalizerFormatter.php 文件

protected function toJson($data, $ignoreErrors = false)
{
    // suppress json_encode errors since it's twitchy with some inputs
    if ($ignoreErrors) {
        if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
            return @json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        }

        return @json_encode($data);
    }

    if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
        return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
    }

    return json_encode($data);
}

config.yml 文件

# Doctrine Configuration
doctrine:
dbal:
    driver:   %database_driver%
    host:     %database_host%
    port:     %database_port%
    dbname:   %database_name%
    user:     %database_user%
    password: %database_password%
    charset:  UTF8
    # if using pdo_sqlite as your database driver, add the path in parameters.yml
    # e.g. database_path: %kernel.root_dir%/data/data.db3
    # path:     %database_path%

orm:
    auto_generate_proxy_classes: %kernel.debug%
    auto_mapping: true
    naming_strategy: doctrine.orm.naming_strategy.underscore

树枝文件

{# app/Resources/views/layout.html.twig #}
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="utf-8" >
    <link href="{{ asset('css/bootstrap.min.css') }}" rel="stylesheet"  media="screen">
    {% block body %}

{% endblock %}

====================

{# PrifBundle/invite/new.html.twig #}
{% extends 'PrifBundle::layout.html.twig' %}

{% block body %}

<div class="well">

<div class="form-horizontal" role="form"> 


{{ form_start(form, {'attr': {'novalidate': 'novalidate'}}) }}         
{# Les erreurs générales du formulaire. #}
{{ form_errors(form) }}

【问题讨论】:

    标签: symfony wkhtmltopdf


    【解决方案1】:

    这个问题与 wkhtml2pdf 和 snappy bundle 无关。试试下面的

    • 清除缓存

    • 从响应中删除 'charset' =&gt; 'UTF-8'

    • 确保您的视图和数据采用 UTF-8 编码

    问题是在您的代码(您未发布)或缓存中的某处使用了json_encode() 方法。

    【讨论】:

    • 嗨,感谢您的回复,我已听从您的建议,清除了现金,从响应中删除了 'charset' => 'UTF-8' 并且在我的 layout.html.twig 中有 但我仍然有 json_encode 的错误消息。我在上一篇文章中使用 json_encode 方法添加了更多代码...感谢您的时间
    • 我的 ide 是 netbeans 我发现我必须在 netbeans.conf 中添加这一行:netbeans_default_options="-J-Dfile.encoding=UTF-8",但错误消息仍然是.. .
    • 实际上,直到我收到此 utf-8 错误消息,我才知道 json_encode,我什至没有尝试使用它。我想要的只是将我的 html 页面转换为 symfony 上的 pdf 和 excel 文件。所以我尝试使用这个 knp snappy bundle 并按照教程进行操作,但也许我错过了一些东西......
    【解决方案2】:

    可能是一个长镜头,但添加一些 javascript 相关参数帮助我解决了一个 javascript 问题。您可能会看到 wkhtmltopdf 有哪些其他可用的参数可能会有所帮助?

    # knp_snappy pdf generation
    knp_snappy:
        pdf:
            enabled:    true
            binary:     %wkhtmltopdf_location%/wkhtmltopdf
            options:
                load-error-handling: ignore
                no-stop-slow-scripts: ~
                enable-javascript: ~
                use-xserver: ~
                page-size: A4
                dpi: 300
                javascript-delay: 3200
    

    【讨论】:

      猜你喜欢
      • 2015-01-31
      • 2016-09-04
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      • 2014-03-28
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多