Symfony knp snappy bundle和wkhtmltopdf ContextErrorException
我已经使用composer安装了knp snappy bundle,并按照教程的说明在 我已安装wkhtmltopdf-0.9.9-installer.exe,并更改了config.yml文件中的默认路径Symfony knp snappy bundle和wkhtmltopdf ContextErrorException,symfony,wkhtmltopdf,Symfony,Wkhtmltopdf,我已经使用composer安装了knp snappy bundle,并按照教程的说明在 我已安装wkhtmltopdf-0.9.9-installer.exe,并更改了config.yml文件中的默认路径 # knp_snappy Configuration knp_snappy: pdf: enabled: true binary: C:\Program Files (x86)\wkhtmltopdf options:
# 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:
错误2:
有人能告诉我怎么解决这个问题吗?谢谢
json.php文件中的json_encode函数(奇怪的是空的)
config.yml文件
树枝锉
{#app/Resources/views/layout.html.twig}
{%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) }}
{PrifBundle/invite/new.html.twig}
{%extends'PrifBundle::layout.html.twig%}
{%block body%}
{{form_start(form,{'attr':{'novalidate':'novalidate'}}}}
{#公式化规则的规则。}
{{form_errors(form)}}
此问题与wkhtml2pdf或snappy捆绑包无关。试试下面的方法
- 清除缓存
- 从响应或列表中删除
'charset'=>'UTF-8'
- 确保您的视图和数据是UTF-8编码的
问题在于
json\u encode()
方法在代码(您没有发布)或缓存中的某个位置的使用。可能是一个长期问题,但添加一些与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
您好,感谢您的回复,我已经按照您的建议,清理了现金,从响应中删除了'charset'=>'UTF-8',在我的layout.html.twig中有,但我仍然有json_encode的错误消息。在上一篇文章中,我使用json_编码方法添加了更多代码。。。感谢您的时间我的ide是netbeans我发现我必须在netbeans.conf中添加这一行:netbeans\u default\u options=“-J-Dfile.encoding=UTF-8”,但错误消息仍然是…事实上,在收到这个UTF-8错误消息之前我不知道json\u encode,我甚至没有尝试使用它。我只想在symfony上将我的html页面转换成pdf和excel文件。所以我试着使用这个knp snappy包并按照教程进行操作,但可能我错过了一些东西。。。
/**
* (PHP 5 >= 5.2.0, PECL json >= 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) {}
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);
}
# 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) }}
# 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