Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug iconv_strlen error - PHP 5.5

I am trying to export a document to PDF using Laravel & DomPDF. This works on my mac, but not on staging or live server. Error as follows:

I have no idea what this means, and cannot find any solutions.

iconv_strlen(): Wrong charset, conversion from8bit//IGNORE' to UCS-4LE' is not allowed

open: /srv/www/html/vendor/patchwork/utf8/class/Patchwork/PHP/Shim/Mbstring.php

        return true;
    }

    static function mb_strlen($s, $encoding = INF)
    {
        INF === $encoding && $encoding = self::$internal_encoding;
        return iconv_strlen($s, $encoding . '//IGNORE');
    }

I have tried adding the following to .htaccess

AddDefaultCharset UTF-8

I have tried adding the following to the top of the view which I am trying to generate the pdf for:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

If you need any further information to assist me in debugging, please ask in comments.

like image 694
Gravy Avatar asked Mar 18 '14 22:03

Gravy


Video Answer


3 Answers

Problem solved. Thanks BrianS.

This was solved by re-installing mbstring.

sudo yum --disablerepo="*" --enablerepo="remi*"
install php-mbstring*
sudo httpd -k restart
like image 182
Gravy Avatar answered Sep 21 '22 14:09

Gravy


BrianS's solution does indeed solve the issue, but I thought it'd be interesting to explain what caused the original problem.

In the latest release of dompdf, the Cpdf class contains about 30 calls to mb_strlen() with the $encoding parameter set to '8bit', which is a valid encoding for mb_strlen().

Laravel's composer.json requires patchwork/utf8. It provides the mb_strlen() shim which calls iconv_strlen().

PHP usually uses either glibc or libiconv for its iconv module. For both libraries, the list of supported encodings can be displayed using iconv --list.

Neither of those libraries support an encoding called '8bit', which is why iconv_strlen() throws that error:

Wrong charset, conversion from '8bit//IGNORE' to 'UCS-4LE' is not allowed

Installing the mbstring PHP module causes mb_strlen() to be executed natively, so the shim isn't used and the error doesn't occur.

Update

@rofavadeka One solution is to create a fork of the dompdf repo, and replace every use of '8bit' encoding with a different 8-bit encoding which is supported by mb_strlen(), glibc and libiconv.

I've written a script to determine which encodings are suitable. Here's the output of the script for glibc and libiconv. The suitable encodings are:

  • cp850
  • cp866
  • iso-8859-1
  • iso-8859-2
  • iso-8859-4
  • iso-8859-5
  • iso-8859-9
  • iso-8859-10
  • iso-8859-13
  • iso-8859-14
  • iso-8859-15
  • iso-8859-16
  • koi8-r
  • koi8-u
like image 29
TachyonVortex Avatar answered Sep 19 '22 14:09

TachyonVortex


I was getting that error in Hash:make() during seeding my DB for testing.

Enabling php_mbstring in php-cli.ini caused it.

In Windows the solution is: remove the semicolon before

 extension=php_mbstring.dll
like image 32
Vani Murarka Avatar answered Sep 22 '22 14:09

Vani Murarka