Fix ImageMagicK Policy Error

Fix ImageMagicK Policy Error

June 27,2019 by MAhmadZ

Fix ImageMagicK Policy Error

Converting documents from JPG to PDF or vice versa, compressing and scaling images is a relatively common task for me. After installing the required ImageMagick package in Ubuntu, I tried running the convert command but to my surprise, got the following error message:

$ convert source.jpg destination.pdf
convert-im6.q16: attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/408.

After little searching I found the resolution as mention below.

Steps to Follow

Open the Terminal on type the command:

$ sudo vim /etc/ImageMagick-6/policy.xml

You will get the output based on the devices attached to you system:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policymap [
  <!ELEMENT policymap (policy)+>
  <!ATTLIST policymap xmlns CDATA #FIXED ''>
  <!ELEMENT policy EMPTY>
  <!ATTLIST policy xmlns CDATA #FIXED '' domain NMTOKEN #REQUIRED
  Configure ImageMagick policies.

  Domains include system, delegate, coder, filter, path, or resource.

  Rights include none, read, write, execute and all.  Use | to combine them,
  for example: "read | write" to permit read from, or write to, a path.
  Rules are processed in order.  Here we want to restrict ImageMagick to only
  read or write a small subset of proven web-safe image types:

    <policy domain="delegate" rights="none" pattern="*" />
    <policy domain="filter" rights="none" pattern="*" />
    <policy domain="coder" rights="none" pattern="*" />
    <policy domain="coder" rights="read|write" pattern="{GIF,JPEG,PNG,WEBP}" />
  <!-- <policy domain="system" name="shred" value="2"/> -->
  <policy domain="coder" rights="none" pattern="PS" />
  <policy domain="coder" rights="none" pattern="EPS" />
  <policy domain="coder" rights="read | write" pattern="PDF" />
  <policy domain="coder" rights="none" pattern="XPS" />

The only change is the highlighted line in which none was replaced with read | write and the file was saved. Now running the convert command again worked as expected 👍🏼