Albee
Albee

Reputation: 155

knp snappy bundle and wkhtmltopdf ContextErrorException

i've installed knp snappy bundle with composer and followed the tutorial for usage and configuration at https://github.com/KnpLabs/KnpSnappyBundle

i've installed wkhtmltopdf-0.9.9-installer.exe and changed the default path in the the config.yml file

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

here is the function

/**
 * @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',
            )
    );
}

but unfortunateley i have these errors messages:

error 1:

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

error 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

can someone tell me what to do to resolve this problem? Thanks

The json_encode function (strangely empty) in the json.php file

/**
 * (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) {}

The NormalizerFormatter.php file for error 2

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

The config.yml file

# 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

The twig files

{# 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) }}

Upvotes: 0

Views: 4786

Answers (2)

timhc22
timhc22

Reputation: 7451

Possibly a long-shot but adding some javascript related parameters helped me with a javascript issue. You could maybe see what other parameters are available for wkhtmltopdf that might help?

# 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

Upvotes: 0

DarkLeafyGreen
DarkLeafyGreen

Reputation: 70406

This problem is not related to wkhtml2pdf nor to snappy bundle. Try the following

  • clear your cache

  • remove 'charset' => 'UTF-8' from response OR

  • ensure your view and data is UTF-8 encoded

The problem is the usage of the json_encode() method somewhere in your code (which you not posted) or in the cache.

Upvotes: 1

Related Questions