3

I have a working PDF to PNG conversion script using PHP and ImageMagick but I am having a problem with the speed of the conversion.

I know it works because with a very small PDF the time taken to convert is not that great, but with a 250kb file (still not that large really) it takes in excess of 20 minutes to convert.

Here's the PHP:

//***** GET PATH TO IMAGEMAGICK *****
$path_to_imagemagick = trim(`which convert`);

//***** PATH TO PDF TO CONVERT *****
$path_to_pdf = getcwd() . "/pdf/myfile.pdf[0]";

//***** PATH TO OUTPUT TO *****
$output_path = getcwd() . "/pdfimage/test_converted.png";

@exec($path_to_imagemagick . " -density 72 -quality 60 -resize 150x " . $path_to_pdf . " " . $output_path);

Are there any settings I can change to make this quicker?

If it helps, the image does not need to be a PNG. If JPEG is going to be quicker I'm happy to go with that.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
Tom
  • 12,776
  • 48
  • 145
  • 240
  • What is the speed of the conversion on the commandline? Does it take 20 minutes for all 250kb PDF files, or is it just one specific PDF? How many pages does this PDF contain? – Kurt Pfeifle Sep 24 '12 at 21:30
  • Having looked into it, it only seems to be certain PDFs which it struggles with. Some larger ones complete in seconds, some smaller ones time-out. Is there a certain PDF setting that ImageMagick would struggle with? – Tom Sep 24 '12 at 21:48
  • There are many PDFs which *most* or even *all* PDF viewers, renderers or interpreters may struggle with... One of the notorious features which may be slow to interprete are page areas using *transparenies* and/or *drop shadows*. -- Also, as I explain in my answer, it is not ImageMagick that handles PDF->Image conversion -- it's Ghostscript. And newer versions of Ghostscript tend to be **much** better handling more modern PDFs (like those created by Adobe CS6, or Acrobat X, or InDesign 5 or whatever...) -- Which version of Ghostscript are you using? (output of: `gs -v`) – Kurt Pfeifle Sep 24 '12 at 22:06
  • If you can provide (a link to) the sample 250kb PDF which renders slow, I can analyze in detail. – Kurt Pfeifle Sep 24 '12 at 22:07

1 Answers1

8

ImageMagick cannot convert PDF to raster images by itself at all.

ImageMagick uses a delegate for this job: that delegate is Ghostscript. If you hadn't installed Ghostscript on the same system as ImageMagick, the PDF conversion by convert wouldn't work.

To gain speed, don't use ImageMagick for PDF -> raster image conversion. Instead, rather use Ghostscript directly (also possible via PHP).

Command line for JPEG output:

gs                                 \
  -o ./pdfimage/test_converted.jpg \
  -sDEVICE=jpeg                    \
  -dJPEGQ=60                       \
  -r72                             \
  -dLastPage=1                     \
   pdf/myfile.pdf

Command line for PNG output:

gs                                 \
  -o ./pdfimage/test_converted.png \
  -sDEVICE=pngalpha                \
  -dLastPage=1                     \
  -r72                             \
   pdf/myfile.pdf 

Both of these commands will give you unscaled output.

To scale the output down, you may use something like

gs                                 \
  -o ./pdfimage/test_converted.png \
  -sDEVICE=pngalpha                \
  -dLastPage=1                     \
  -r72                             \
  -dDEVICEWIDTHPOINTS=150          \
  -dDEVICEHEIGHTPOINTS=150         \
  -dPDFFitPage                     \
   pdf/myfile.pdf 

Also please note: You used a -quality 60 setting for your PNG outputting command. But -quality for JPEG and -quality for PNG output do have a completely different meaning with ImageMagick (and you may not be aware of it). See also this answer for some details about this.

Community
  • 1
  • 1
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345