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 from
8bit//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.
Problem solved. Thanks BrianS.
This was solved by re-installing mbstring.
sudo yum --disablerepo="*" --enablerepo="remi*"
install php-mbstring*
sudo httpd -k restart
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.
@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:
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
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.
If you are on wamp or some custom stack, remember that the php
in your wamp may be using a custom php_something.ini
for the apache use, due to which even if the wamp 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.