DEV Community

Cover image for Error when converting PDF to image on Github Actions
David Wessman
David Wessman

Posted on • Edited on • Originally published at wessman.co

1 1

Error when converting PDF to image on Github Actions

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>'
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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`
Enter fullscreen mode Exit fullscreen mode

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay