When MPDF is used to convert HTML to PDF and convert the PDF to PNG image, an error is reported in Chinese ......

Source: Internet
Author: User
Step 1: Use MPDF (version 6.1) to convert HTML pages into PDF files. The code is as follows: {code ...} step 2: When you use imagick to convert a PDF file to an image, if the PDF file contains no Chinese characters, the conversion is successful. if the PDF file contains Chinese characters, then... step 1:

Use MPDF (version 6.1) to convert HTML pages into PDF files. The code is as follows:

$ Html = "for blind beginners, it does not require any additional modifications. "; // $ Html =" These are the most used acronyms throughout this manual. "; include '. /mpdf. php '; $ mpdf = new mPDF (' + aCJK '); $ mpdf-> autoScriptToLang = true; $ mpdf-> autoLangToFont = true; $ mpdf-> WriteHTML ($ html); $ mpdf-> Output ();
Step 2:

When you use imagick to convert a PDF file to an image,
IfIf no Chinese characters exist, the conversion is successful.
IfIf you have Chinese characters, an error is reported:, Error 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
Note: In addition, I found a PDF description file for public API (basically all in Chinese). I used imagick to convert the PDF file to png, and the conversion was successful...

PDF conversion 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);
Solution:
When MPDF is used to convert HTML into PDF, and then convert the PDF into PNG image, how can I avoid the error in Chinese ??

Reply content: Step 1:

Use MPDF (version 6.1) to convert HTML pages into PDF files. The code is as follows:

$ Html = "for blind beginners, it does not require any additional modifications. "; // $ Html =" These are the most used acronyms throughout this manual. "; include '. /mpdf. php '; $ mpdf = new mPDF (' + aCJK '); $ mpdf-> autoScriptToLang = true; $ mpdf-> autoLangToFont = true; $ mpdf-> WriteHTML ($ html); $ mpdf-> Output ();
Step 2:

When you use imagick to convert a PDF file to an image,
IfIf no Chinese characters exist, the conversion is successful.
IfIf you have Chinese characters, an error is reported:, Error 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
Note: In addition, I found a PDF description file for public API (basically all in Chinese). I used imagick to convert the PDF file to png, and the conversion was successful...

PDF conversion 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);
Solution:
When MPDF is used to convert HTML into PDF, and then convert the PDF into PNG image, how can I avoid the error in Chinese ??

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.