Mastering PDF Modification with Minimagick and ImageMagick in Ruby on Rails: A Docker Container Guide

·

1 min read

I am currently developing an image hosting service with a direct upload feature using Ruby on Rails. Uploading and modifying images using Minimagick has been a smooth process, as long as you understand how Minimagick processes active storage in Ruby on Rails.

However, modifying PDF files using Minimagick was a challenge. This is because ImageMagick does not allow the modification of PDF files by default. To change this, you need to modify the /etc/ImageMagick-6/policy.xml file.

Within the policy.xml file, locate the following line:

<policy domain="coder" rights="none" pattern="PDF" />

and change it to:

<policy domain="coder" rights="read|write" pattern="PDF" />

By doing this, ImageMagick is able to modify PDF files.

As I am developing the application within a Docker container, modifying the policy.xml file alone is not enough. Once the policy.xml file has been modified, copy the file to the host machine folder using the command:

docker cp assets-dev:/etc/ImageMagick-6/policy.xml

Additionally, ensure that the ghostscript library, which is used by Minimagick to handle PDF files, is installed. As I am using Ubuntu in my Docker container, I installed ghostscript by adding the following line to my Dockerfile:

sudo apt-get install -y ghostscript

By following these steps, ImageMagick can process PDF files without any extra effort required.