Error when converting PDF to image on Github Actions
David Wessman
Posted on October 15, 2020
Error:
Documents::UploadJobTest#test_#perform_creates_an_activity_for_document:
MiniMagick::Error: `convert /tmp/shrine20201009-22018-5itpj9.pdf[0] -auto-orient /tmp/image_processing20201009-22018-1uk39lv.png` failed with error:
convert-im6.q16: not authorized `/tmp/shrine20201009-22018-5itpj9.pdf' @ error/constitute.c/ReadImage/412.
convert-im6.q16: no images defined `/tmp/image_processing20201009-22018-1uk39lv.png' @ error/convert.c/ConvertImageCommand/3258.
app/uploaders/document_uploader.rb:13:in `block in <class:DocumentUploader>'
test/jobs/documents/documents_upload_job_test.rb:8:in `block in <class:UploadJobTest>'
In my Ruby on Rails application, this error is raised when using MiniMagick to convert a PDF into an image.
MiniMagick uses ImageMagick which uses Ghostscript for anything related to postscript files - for example PDFs.
Due to a security vulnerability in Ghostscript < 9.24, ImageMagick changed the default policy to not allow conversions using Ghostscript.
Even if Ghostscript has fixed the vulnerability, the ImageMagick policy has not been changed.
In my application I use MiniMagick via a gem called Shrine where a PDF is processed to get a thumbnail. To read more about this, please visit the Shrine documentation.
On Github Actions the default linux image is Ubuntu 18.04 (20.04 in preview).
Ubuntu 18.04 should have an updated version of Ghostscript, but still has a policy for ImageMagick that disallows conversion from PDF to an image.
Solution
My solution was inspired by this StackOverflow answer.
It can be added to a Github Action like this:
# ---
jobs:
job01:
runs-on: ubuntu-latest # ubuntu-18.04
steps:
- name: Change ImageMagick policy to allow pdf->png conversion.
run: |
sudo sed -i 's/^.*policy.*coder.*none.*PDF.*//' /etc/ImageMagick-6/policy.xml
- <more steps>
Mac OS
If you experience this error on Mac OS it can be helpful to reinstall ImageMagick and Ghostscript:
brew uninstall ghostscript imagemagick
brew install ghostscript imagemagick
gs --version # make sure it is > 9.24 (9.53.3 on my machine currently.)
magick --version # make sure the row `Delegates` includes `gslib`
Posted on October 15, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.