Conversion PDF to PNG or JPEG is very very slow using ImageMagick

你。 提交于 2019-11-30 10:49:27
Kurt Pfeifle

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!