php mpdf html 转pdf,使用 MPDF 将HTML转为PDF,然后将该PDF转为PNG图片的时候,中文报错... ...汗血宝马...
第一步:
使用 MPDF(版本6.1) 將 HTML 頁面轉為PDF文件,可以轉成功。代碼如下:
$html = "對盲人初學者來說,它無需任何額外的修改。";
// $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 實現 PDF 轉 圖片 的時候,
如果 PDF 文件中 沒有中文,則轉換成功
如果 PDF 文件中 有中文,則直接報錯:,報錯代碼:
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
備注:另外,自己找了一個公開API的PDF說明文件(基本全是中文), 使用 imagick把該PDF轉成png,轉換成功功功功…
PDF 轉 圖片 代碼:
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轉為PDF,然后將該PDF轉為PNG圖片時有中文怎么才能不報錯??
原來這么簡單:
$mpdf=new mPDF('utf-8'); ? //這里改成UTF-8 即可
總結
以上是生活随笔為你收集整理的php mpdf html 转pdf,使用 MPDF 将HTML转为PDF,然后将该PDF转为PNG图片的时候,中文报错... ...汗血宝马...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php随机获取文件夹,php从文件夹随机
- 下一篇: 杰奇php配置模块,custom.php