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 ??