将所有pdf页面转换为图像


convert all pdf pages into image

在php中,使用Imagick,我可以一次将一个pdf页面转换为jpg图像。但我需要将pdf的所有页面转换成单独文件夹中的jpg文件。

低于我的代码

<?php 
  for($i=0;$i<=20;$i++){
  $pdf_file   = 'book.pdf';
  $save_to    = 'pages/tw'.$i.'.jpg';
  $img = new imagick();
  $img->setResolution(200,200);
  $img->readImage("{$pdf_file}[$i]");
  $img->scaleImage(800,0);
  $img->setImageFormat('jpg'); 
  $img = $img->flattenImages();
  $img->writeImages($save_to, false);
  $img->destroy();     
 }
 ?> 

以上代码最多可生成10页的结果。然后它在执行时间为30秒时终止。我无法管理php.ini,因为我在另一家公司使用托管。

 $mypdf = escapeshellarg( "mysafepdf.pdf" );
  $newjpg = escapeshellarg( "output.jpg" );
  $result = 0;
  exec("convert -density 600 {$mypdf} {$newjpg} -colorspace RGB -resample 300", null, $result);

$result将为0,如果转换工作

-密度=dpi

我希望这对你有帮助!

PS.:这只是一张图片,但你可以根据你的$i进行调整。