使用 MPDF 将HTML转为PDF,然后将该PDF转为PNG图片的时候,中文报错... ...

WBOY
Libérer: 2016-09-28 08:54:07
original
1450 Les gens l'ont consulté

第一步:

使用 MPDF(版本6.1) 将 HTML 页面转为PDF文件,可以转成功。代码如下:

<code>$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();</code>
Copier après la connexion
Copier après la connexion

第二步:

使用 imagick 实现 PDF 转 图片 的时候,
如果 PDF 文件中 没有中文,则转换成功
如果 PDF 文件中 有中文,则直接报错:,报错代码:

<code>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</code>
Copier après la connexion
Copier après la connexion

备注:另外,自己找了一个公开API的PDF说明文件(基本全是中文), 使用 imagick把该PDF转成png,转换成功功功功...

PDF 转 图片 代码:

<code>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);</code>
Copier après la connexion
Copier après la connexion

求解决:

<code>使用 MPDF 将HTML转为PDF,然后将该PDF转为PNG图片时有中文怎么才能不报错??
</code>
Copier après la connexion
Copier après la connexion

回复内容:

第一步:

使用 MPDF(版本6.1) 将 HTML 页面转为PDF文件,可以转成功。代码如下:

<code>$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();</code>
Copier après la connexion
Copier après la connexion

第二步:

使用 imagick 实现 PDF 转 图片 的时候,
如果 PDF 文件中 没有中文,则转换成功
如果 PDF 文件中 有中文,则直接报错:,报错代码:

<code>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</code>
Copier après la connexion
Copier après la connexion

备注:另外,自己找了一个公开API的PDF说明文件(基本全是中文), 使用 imagick把该PDF转成png,转换成功功功功...

PDF 转 图片 代码:

<code>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);</code>
Copier après la connexion
Copier après la connexion

求解决:

<code>使用 MPDF 将HTML转为PDF,然后将该PDF转为PNG图片时有中文怎么才能不报错??
</code>
Copier après la connexion
Copier après la connexion
Étiquettes associées:
php
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!