minimagick / minimagick

mini replacement for RMagick
https://rubydoc.info/gems/mini_magick
MIT License
2.83k stars 347 forks source link

Unable to create png from pdf with ghostscript 9.26, ImageMagick 6.9.10-14 #474

Closed lvonk closed 5 years ago

lvonk commented 5 years ago

This is not really a minimagick issue I think, but I just wanted to share this in case other people start banging their heads over this. So feel free to close immediately. This is related to https://github.com/minimagick/minimagick/issues/472 but it is a different use case.

This issue applies to:

We create thumbnails from a PDF and this started failing since ghostscript released the security update 9.26.

In our code we create png from pdf like this:

image = MiniMagick::Image.read(raw_data '.pdf')
image.format "PNG"

This results in a command like convert document.pdf[0] document.png. This worked previously, but since the 9.26 security update of ghostscript it started erroring with a convert: no images defined ....

This also happens when directly calling convert. As workaround we now first create a single page pdf from the raw_data using the combine_pdf gem. and pass that into MiniMagick.

new_pdf = CombinePDF.new
new_pdf << CombinePDF.parse(raw_data).pages[0]
image = MiniMagick::Image.read(new_pdf.to_pdf, '.pdf')
image.format "PNG", nil

Passing in nil as argument to image.format will prevent that the [0] postfix is added to the convert command and the png is created as expected.

fukayatsu commented 5 years ago

Same issue for me, and I found this.

Bug #1806517 “Ghostscript segmentation fault onb PDF using -dFir...” : Bugs : GS-GPL

Subscribing Ubuntu Security Team as this is a regression caused by the recent security updates.

jjf21 commented 5 years ago

Same issue for me:

Some '.pdf' files doesn't want to open with MiniMagick

pdf = MiniMagick::Image.open(pdf_file)

If I look at pdf.data I get the error:

MiniMagick::Error: `convert /var/folders/p9/8s4hyff50hl_821gd7r6dmmh0000gn/T/mini_magick20190417-66256-klm1ju.pdf[0] json:` failed with error:
convert: FailedToExecuteCommand `'gs' -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r72x72' -dFirstPage=1 -dLastPage=1 '-sOutputFile=/var/folders/p9/8s4hyff50hl_821gd7r6dmmh0000gn/T/magick-69397xb6yWSS0wmFO%d' '-f/var/folders/p9/8s4hyff50hl_821gd7r6dmmh0000gn/T/magick-69397jldpq8J7xwvV' '-f/var/folders/p9/8s4hyff50hl_821gd7r6dmmh0000gn/T/magick-69397MJA2hQISIWNj'' (-1) @ error/delegate.c/ExternalDelegateCommand/478.
convert: no images defined `json:' @ error/convert.c/ConvertImageCommand/3300.

But this happen only on some pdfs (not all).

thao33 commented 5 years ago

this makes my day!! Thanks lvonk

image.format "PNG", nil

ajeje commented 5 years ago

Hi all, same issue for me with GhostScript 9.26.

I've resolved by editing the policy file for ImageMagick. In /etc/ImageMagick-6/policy.xml i've comment out this row:

And now all is working

jsmedmar commented 5 years ago

what row did you commented out?

janko commented 5 years ago

Thanks for sharing. I will close the issue as it's not MiniMagick-related, but feel free to keep discussing.

bess commented 4 years ago

The line that was commented out isn't appearing in the comment above. It is: policy domain="coder" rights="none" pattern="PDF" (but with xml tags around it)