Home > Backend Development > PHP Tutorial > When using MPDF to convert HTML to PDF, and then convert the PDF to a PNG image, an error message appears in Chinese...

When using MPDF to convert HTML to PDF, and then convert the PDF to a PNG image, an error message appears in Chinese...

WBOY
Release: 2016-09-28 08:54:07
Original
1516 people have browsed it

Step one:

Use MPDF (version 6.1) to convert HTML pages to PDF files, and the conversion can be successful. The code is as follows:

<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>
Copy after login
Copy after login

Step 2:

When using imagick to convert PDF to image,
if there is no Chinese in the PDF file , the conversion is successful
If there is Chinese in the PDF file , an error will be reported directly: , error code:

<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>
Copy after login
Copy after login

Note: In addition, I found a PDF description file of the public API (basically all in Chinese), used imagick to convert the PDF to png, and the conversion was successful...

PDF to image code:

<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>
Copy after login
Copy after login

Seeking solution:

<code>使用 MPDF 将HTML转为PDF,然后将该PDF转为PNG图片时有中文怎么才能不报错??
</code>
Copy after login
Copy after login

Reply content:

Step one:

Use MPDF (version 6.1) to convert HTML pages to PDF files, and the conversion can be successful. The code is as follows:

<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>
Copy after login
Copy after login

Step 2:

When using imagick to convert PDF to image,
if there is no Chinese in the PDF file , the conversion is successful
If there is Chinese in the PDF file , an error will be reported directly: , error code:

<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>
Copy after login
Copy after login

Note: In addition, I found a PDF description file of the public API (basically all in Chinese), used imagick to convert the PDF to png, and the conversion was successful...

PDF to image code:

<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>
Copy after login
Copy after login

Please solve:

<code>使用 MPDF 将HTML转为PDF,然后将该PDF转为PNG图片时有中文怎么才能不报错??
</code>
Copy after login
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template