php 如何设置 mpdf HTML 包含无效的 UTF-8 字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28736061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:02:02  来源:igfitidea点击:

how to set mpdf HTML contains invalid UTF-8 character(s)

phppdf

提问by debasish

how to set mpdf HTML contains invalid UTF-8 character(s) when you create pdf on your appliactions

在应用程序上创建 pdf 时,如何设置 mpdf HTML 包含无效的 UTF-8 字符

回答by MoxFulder

Try this

尝试这个

$html = mb_convert_encoding($html, 'UTF-8', 'UTF-8');

before calling: "$mpdf->WriteHTML($html);"

在调用之前:“$mpdf->WriteHTML($html);”

It's seems senseless, but it works for me.

这似乎毫无意义,但它对我有用。

回答by Runaurufu

With mpdf it is no point in conversions and encoding as these most likely will lose your chars and you would get only "?" or other unrecognisable chars (but will yield output)

使用 mpdf,转换和编码毫无意义,因为这些很可能会丢失您的字符,而您只会得到“?” 或其他无法识别的字符(但会产生输出)

Try using these before any input being send to mpdf:

在将任何输入发送到 mpdf 之前尝试使用这些:

$mpdf->allow_charset_conversion=true;
$mpdf->charset_in='UTF-8';

回答by Erik

This works for me:

这对我有用:

$mpdf->WriteHTML(utf8_encode($html));

回答by debasish

Use utf8_encode() function. Eg : $html = '

使用 utf8_encode() 函数。例如: $html = '

Name of Originator

发起人姓名

Originator Address

发起人地址

Originator Phone Number

发起人电话号码

Originator Email

发件人电子邮件

Borrower

借款人

Property Address

物业地址

Date of GFE

GFE 日期

 ?

 ?

'; $html = utf8_encode($html1);

'; $html = utf8_encode($html1);

回答by user3408779

The below two lines will do the trick

下面的两行代码可以解决问题

$mpdf->allow_charset_conversion = true;
$mpdf->charset_in = 'iso-8859-4';

Add the above two lines after creating the object , this will look like

创建对象后添加以上两行,这看起来像

$mpdf=new mPDF();
$mpdf->allow_charset_conversion = true;
$mpdf->charset_in = 'iso-8859-4';

回答by Sudhir

After hours of hair-pulling, this worked wonders for me :)

经过几个小时的拉头发,这对我来说很神奇:)

(In my case ??????? was showing as ??????)

(在我的情况下 ??????? 显示为 ??????)

$mpdf->SetAutoFont();

$mpdf->SetAutoFont();

$mpdf = new mPDF('utf-8','', 0, '', 15, 15, 16, 16, 9, 9, 'L');
$mpdf->SetAutoFont();

//~ Nothing of below worked :(
//~ $mpdf->useLang = true;
//~ $mpdf->autoScriptToLang = true;
//~ $mpdf->autoLangToFont = true;
//~ $pdf_html = mb_convert_encoding($html, 'UTF-8', 'UTF-8');

$mpdf->WriteHTML($html);
//~ $mpdf->DeletePages(2);
$filename   = date('ymdhis').".pdf";
$mpdf->Output($filename,'D');

My PDF included a mix of English and Hindi words like

我的 PDF 包含英语和印地语单词的混合,例如

"3 units of ??????? Priced @ $10.00/unit".

“3 件 ??????? 售价 @ 10.00 美元/件”。

回答by Aleksey Kuznetsov

I'm 99% sure that problem is because you did some manipulations with the text using unicode-unsafe functions. Use mb_strpos()/mb_substr()instead of strpos()/substr(), and add "/u" modifier to all your conversions with prog_replace().

我 99% 肯定这个问题是因为您使用 unicode-unsafe 函数对文本进行了一些操作。使用mb_strpos()/mb_substr()而不是strpos()/substr(),并使用 将“ /u”修饰符添加到所有转换中prog_replace()

Of course, the problem can be fixed by allowing "charset conversion" as already suggested before. But much better to know that your text already correct and meets the compliance of UTF-8.

当然,这个问题可以通过允许之前已经建议的“字符集转换”来解决。但最好知道您的文本已经正确并符合 UTF-8 的要求。

回答by jab11

I got this error when I send NULLto ->multicell(). Sending ""fixed it.

当我发送NULL->multicell(). 发送""修复它。

old question but maybe somebody comes here from google like I did

老问题,但也许有人像我一样从谷歌来到这里