php mpdf html 转pdf,使用 MPDF 将HTML转为PDF,然后将该PDF转为PNG图片的时候,中文报错... ...汗血宝马...
第一步:
使用 MPDF(版本6.1) 將 HTML 頁面轉(zhuǎn)為PDF文件,可以轉(zhuǎn)成功。代碼如下:
$html = "對(duì)盲人初學(xué)者來說,它無需任何額外的修改。";
// $html = "These are the most used acronyms throughout this manual.";
include './mpdf/mpdf.php';
$mpdf=new mPDF('+aCJK');
$mpdf->autoScriptToLang = true;
$mpdf->autoLangToFont = true;
$mpdf->WriteHTML($html);
$mpdf->Output();
第二步:
使用 imagick 實(shí)現(xiàn) PDF 轉(zhuǎn) 圖片 的時(shí)候,
如果 PDF 文件中 沒有中文,則轉(zhuǎn)換成功
如果 PDF 文件中 有中文,則直接報(bào)錯(cuò):,報(bào)錯(cuò)代碼:
Fatal error: Uncaught exception 'ImagickException' with message 'Failed to read the file' in /home/www/test/index-back.php:26 Stack trace: #0 /home/www/test/index-back.php(26): Imagick->readimage('/home/www/test/...') #1 /home/www/test/index-back.php(48): pdf2png('/home/www/test/...', '/home/www/test/...') #2 {main} thrown in /home/www/test/index-back.php on line 26
備注:另外,自己找了一個(gè)公開API的PDF說明文件(基本全是中文), 使用 imagick把該P(yáng)DF轉(zhuǎn)成png,轉(zhuǎn)換成功功功功…
PDF 轉(zhuǎn) 圖片 代碼:
function pdf2png($PDF, $Path){
if(!extension_loaded('imagick')){
return false;
}
if(!file_exists($PDF)){
return false;
}
$IM = new imagick();
$IM->setResolution(120, 120);
$IM->setCompressionQuality(100);
$IM->readImage($PDF);
foreach($IM as $Key => $Var){
$Var->setImageFormat('png');
$Filename = $Path.'/'.md5($Key.time()).'.png';
if($Var->writeImage($Filename)==true){
$Return[]= $Filename;
}
}
return $Return;
}
$pdf = __DIR__.'/1.pdf';
$path = __DIR__.'/images';
$data = pdf2png($pdf, $path );
var_dump($data);
求解決:
使用 MPDF 將HTML轉(zhuǎn)為PDF,然后將該P(yáng)DF轉(zhuǎn)為PNG圖片時(shí)有中文怎么才能不報(bào)錯(cuò)??
原來這么簡(jiǎn)單:
$mpdf=new mPDF('utf-8'); ? //這里改成UTF-8 即可
總結(jié)
以上是生活随笔為你收集整理的php mpdf html 转pdf,使用 MPDF 将HTML转为PDF,然后将该PDF转为PNG图片的时候,中文报错... ...汗血宝马...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php随机获取文件夹,php从文件夹随机
- 下一篇: 杰奇php配置模块,custom.php