When you use MPDF to convert HTML to PDF, and then convert the PDF to a PNG image, the Chinese error ...

Source: Internet
Author: User
Tags stack trace

The first step:

Using MPDF (version 6.1) to convert an HTML page to a PDF file, you can go to it successfully. The code is as follows:

$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();

Step Two:

When using Imagick to achieve PDF-to-image,
If there is no Chinese in the PDF file, the conversion succeeds
If the PDF file has Chinese, then direct error:, 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 public API PDF documentation (basically all in Chinese), using Imagick to convert the PDF to PNG, the success of the conversion ...

PDF to Image 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:

使用 MPDF 将HTML转为PDF,然后将该PDF转为PNG图片时有中文怎么才能不报错??

Reply content:

The first step:

Using MPDF (version 6.1) to convert an HTML page to a PDF file, you can go to it successfully. The code is as follows:

$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();

Step Two:

When using Imagick to achieve PDF-to-image,
If there is no Chinese in the PDF file, the conversion succeeds
If the PDF file has Chinese, then direct error:, 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 public API PDF documentation (basically all in Chinese), using Imagick to convert the PDF to PNG, the success of the conversion ...

PDF to Image 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:

使用 MPDF 将HTML转为PDF,然后将该PDF转为PNG图片时有中文怎么才能不报错??
  • Related Article

    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.