2012-02-10 11:23 阅读 68


I'm using a litte script to convert PDF to JPG. That works but the quality is very poor.

The script:

$im = new imagick( 'document.pdf[ 0]' ); 
$im->setResolution(300, 300);

One more thing, I want to keep the original size of the PDF but the conversion crops the size of the JPG.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • 已采纳
    douxi4414 douxi4414 2012-09-13 13:43

    It can be done using setResolution, but you need to do it before loading an image. Try something like this:

    // instantiate Imagick 
    $im = new Imagick();
    点赞 评论 复制链接分享
  • doushi6932 doushi6932 2012-02-10 11:53

    Ensure that the PDF is created with the correct colour profiles, I once had my JPG being very washed out after resizing due to source file was created with wrong colour profile. See also: ImageMagick PDF to JPEG conversion results in green square where image should be

    点赞 评论 复制链接分享
  • douxun1934 douxun1934 2012-02-10 15:37

    The quality of the image produced from the PDF can be changed by setting the density (which is the DPI) before reading in the PDF - this gets past to ghostscript (gs) underneath which rasterizes the PDF. To get a good result, supersample at double the density you require, and use resample to get back to the desired DPI. Remember to change the colorspace to RGB if you want an RGB JPEG.

    A typical command line version for convert might be:

    convert -density 600 document.pdf[0] -colorspace RGB -resample 300 output.jpg

    If you need to crop it, a -shave command following the resample is usually sensible, if the image is centred within the page.

    As for the PHP IMagick extension, well, I never personally use it - so am unsure of how you specify file reading hints to it, but I would hope it is possible.

    点赞 评论 复制链接分享
  • dougou8552 dougou8552 2014-12-09 14:30
    $im = new imagick();
    //this must be called before reading the image, otherwise has no effect
    //read the pdf
    点赞 评论 复制链接分享
  • doujiao2443 doujiao2443 2016-01-20 13:33

    Click here for more details. Try this:


        <form action="ConvertPdfToImg.php" enctype="multipart/form-data" method="post" name="f1">
          <input id="templateDoc" name="templateDoc" type="file" />
          <input type="submit" />


    $pdfAbsolutePath = __DIR__."/test.pdf";
    if (move_uploaded_file($_FILES['templateDoc']["tmp_name"], $pdfAbsolutePath)) {
          $im             = new imagick($pdfAbsolutePath);
          $noOfPagesInPDF = $im->getNumberImages(); 
          if ($noOfPagesInPDF) { 
              for ($i = 0; $i < $noOfPagesInPDF; $i++) { 
                  $url = $pdfAbsolutePath.'['.$i.']'; 
                  $image = new Imagick($url);
              echo "All pages of PDF is converted to images";
          echo "PDF doesn't have any pages";
    点赞 评论 复制链接分享