10

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.

Gravy
  • 12,264
  • 26
  • 124
  • 193
  • 3
    Looks like the issue is in the MBString compatiblity shim supplied by Patchwork. I.e. your server does not have MBString enabled and something in the shim doesn't quite work as expected. It would help to know what's calling the `mb_strlen` function. Can you get a stack trace? – BrianS Mar 19 '14 at 01:45
  • 1
    Problem solved. Thanks BrianS. Problem solved by re-installing mbstring. `sudo yum --disablerepo="*" --enablerepo="remi*" install php-mbstring* sudo httpd -k restart` – Gravy Mar 19 '14 at 14:36
  • 1
    ^^ You should make this a solution by answering your own question. Can you also explain how you run this process. – Shane Dec 12 '14 at 14:51
  • @Gravy I had the same problem, and updating php-mbstring here resolved it. You should post an answer to your question with what you did to resolve it! – msturdy Dec 17 '14 at 21:10

5 Answers5

9

Problem solved. Thanks BrianS.

This was solved by re-installing mbstring.

sudo yum --disablerepo="*" --enablerepo="remi*"
install php-mbstring*
sudo httpd -k restart
Niraj Shah
  • 15,087
  • 3
  • 41
  • 60
Gravy
  • 12,264
  • 26
  • 124
  • 193
4

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:

Community
  • 1
  • 1
TachyonVortex
  • 8,242
  • 3
  • 48
  • 63
  • So is there a way to change the '8bit' encoding or get around this problem? Having the samen problem, but on shared hosting, so sadly i can not simply reinstall to solve the issue! Hope you can point me in the right direction. – rofavadeka Jan 28 '15 at 15:11
3

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
philipxy
  • 14,867
  • 6
  • 39
  • 83
0

If you are using WHM then you can use EasyApache to rebuild. Once you get the modules options after selecting your version of PHP select the "Exhaustive Options List" button. Then ctrl+f "mbstring" and it should come up. Mark the check box and rebuild. It should work.

Goddard
  • 2,863
  • 31
  • 37
0

If you are on wamp or some custom stack, remember that the php in your may be using a custom php_something.ini for the apache use, due to which even if the GUI is showing that mbstring is on(uncommented) still its quiet possible that that same line is commented out(disabled/off) inside the actual php.ini file (True Story).

Solution:- Just navigate to

wamp dir > bin > php > phpx.x.x >

In this directory you shall find various .ini files named slightly differently, Mine was using php_uwamp.ini for the stack but for the CLI, php was using the adjacent file named php.ini.

This had me pulling hairs for quiet long, thought it might help someone.

Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88