imageToBlob() on multi-page PDFs?

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
wsleg
Posts: 2
Joined: 2015-10-13T12:40:18-07:00
Authentication code: 1151

imageToBlob() on multi-page PDFs?

Post by wsleg » 2015-10-13T12:49:47-07:00

Hi,

I'm using PerlMagick to try convert multi-page PDFs into separate PNG files. I'm using the imageToBlob() method but am only getting the last page of the PDF file in the blob. Having Googled a bit I've seen reference about casting the result to an array (@) but I still seem to get a single element representing the last page of the PDF.

Strangely, when I use the command line utility on the same file it works file: convert IMG_0034.PDF IMG_0034_%d.PNG which results in IMG_0034_1.PNG and IMG_0034_2.PNG.

Here's the relevant bit of my code:

Code: Select all

          ...
          $value = $object->get; # PDF file from AWS S3
          $image=Image::Magick->new(magick=>$docType);  # docType is determined from the S3 file, typically is 'pdf'
          $image->Set(density=>300);
          $image->BlobToImage($value);

          $width  = $image->Get('width');
          $height = $image->Get('height');

          # resize the image into roughly 1200 pixels wide
          $factor = int ($width / 1200);
          if($factor > 1)
          {
            $width  = int ($width / $factor);
            $height = int ($height / $factor);
          }

          $image->Set( Gravity => 'Center' );
          $image->Resize( geometry => $width . 'x' . $height );
          $image->Extent( geometry => $width . 'x' . $height );

          # convert to grayscale 
          $image->Quantize(colorspace=>'gray');

          # set output format to be PNG
          $image->Set(magick=>'png');
          $newValue = $image->ImageToBlob();
          ...
Any ideas?

Thanks,
Lyndon

wsleg
Posts: 2
Joined: 2015-10-13T12:40:18-07:00
Authentication code: 1151

Re: imageToBlob() on multi-page PDFs?

Post by wsleg » 2015-10-13T13:43:11-07:00

Don't worry, I'm an idiot. My earlier tests with casting to an array gave an array length of 1. But of course that means 2 in perl.

So to modify my above code example, the last line should be:

@newValue = $image->ImageToBlob();
print "Number of pages: " . $#newValue + 1;

I'm then able to iterate over the array and get each individual image just fine.

Post Reply