I need to view one of the pages in a PDF document to crop a portion of it and generate a new PNG-formatted document.
GD does not support PDF format, Imagick supports opening PDFs, and turns one page into PNG, but how do you crop up part of this page?
Reference code from the network
The code is as follows
function Pdf2png ($pdf, $path, $page =-1) {if (!extension_loaded (' Imagick ')) {return false; } if (!file_exists ($pdf)) {return false; } $im = new Imagick (); $im->setresolution (600,600); $im->setcompressionquality (100); if ($page ==-1) $im->readimage ($pdf); else $im->readimage ($pdf. " [". $page."] "); foreach ($im as $Key + $Var) {$height = $Var->getimageheight ();//Get the height of the original image $width = $Var->getimagewid th ();//Get the width of the original//echo "H". $height. " W: ". $width; $im _cut = new Imagick (); This sets up an instance of the new diagram $im _cut->setresolution (600,600); Resolution $im _cut->setcompressionquality (100);//Compression ratio//$im _cut->setimageformat (' png '); Format the new diagram here is an error//$im _cut->setimagetype (0);//$im _cut-> Next do not know how to do//how to set the height length of the new diagram//How to copy part of the $var to New figure,//How to save the new diagram $Var->setimageformat (' png '); $Var->setimagetype (0); $filename = $path. " /". MD5 ($Key. Time ()). PNG '; if ($Var->writeimage ($filename) = = True) {$Return [] = $filename; }} return $Return; }
Reply to discussion (solution)
$myurl = '/pdf/mypdf.pdf '; $image = new Imagick (Realpath ($myurl). ' [0] '); [0] Indicate the number of the wanted page$image->setresolution, $image->setimageformat ("png"); $image ->writeimage (Realpath ('./mypdf.png '));
It's been settled or thank you.