69

I have a program, in which I need to convert a PDF to an image using Image Magick. I do that using the subprocess package:

cmd = 'magick convert -density 300 '+pdfFile+'['+str(rangeTuple[0])+'-'+str(rangeTuple[1])+'] -depth 8 '+'temp.tiff' #WINDOWS
if(os.path.isfile('temp.tiff')):
    os.remove('temp.tiff')
subprocess.call(cmd,shell=True)
im = Image.open('temp.tiff')

The error I got is:

convert-im6.q16: not authorized `temp2.pdf' @ error/constitute.c/ReadImage/412.
convert-im6.q16: no images defined `temp.tiff' @ error/convert.c/ConvertImageCommand/3258.
Traceback (most recent call last):
  File "UKExtraction2.py", line 855, in <module>
    doItAllUpper("A0","UK5.csv","temp",59,70,"box",2,1000,firstPageCoordsUK,boxCoordUK,voterBoxCoordUK,internalBoxNumberCoordUK,externalBoxNumberCoordUK,addListInfoUK)
  File "UKExtraction2.py", line 776, in doItAllUpper
    doItAll(tempPDFName,outputCSV,2,pdfs,formatType,n_blocks,writeBlockSize,firstPageCoords,boxCoord,voterBoxCoord,internalBoxNumberCoord,externalBoxNumberCoord,addListInfo,pdfName)
  File "UKExtraction2.py", line 617, in doItAll
    mainProcess(pdfName,(0,noOfPages-1),formatType,n_blocks,outputCSV,writeBlockSize,firstPageCoords,boxCoord,voterBoxCoord,internalBoxNumberCoord,externalBoxNumberCoord,addListInfo,bigPDFName,basePages)
  File "UKExtraction2.py", line 542, in mainProcess
    im = Image.open('temp.tiff')
  File "/home/rohit/.local/lib/python3.6/site-packages/PIL/Image.py", line 2609, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'temp.tiff'

The most important of which is:

convert-im6.q16: not authorized `temp2.pdf' @ error/constitute.c/ReadImage/412.

I think this is because ImageMagick isn't authorized to access the PDF. What should be done now? I'm on a Linux server. Any help is appreciated.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Mooncrater
  • 4,146
  • 4
  • 33
  • 62
  • 2
    Please review [Security Policy](https://imagemagick.org/script/security-policy.php) in the documentation. You'll need to enable support by editing a `policy.xml` file. – emcconville Oct 17 '18 at 19:57
  • 3
    Possible duplicate of [PHP ImagickException: not authorized](https://stackoverflow.com/questions/37599727/php-imagickexception-not-authorized) – strpeter Oct 19 '18 at 12:41
  • 1
    It looks like a temporary patch to safeguard against this security vulnerability: https://bugs.gentoo.org/664236 so it should "go away" and "just work" again at some point. In the mean time you have the choice of either being unable to use imagemagick for some formats, or having a vulnerability (which you get by following the suggestions below). – TheChymera Dec 12 '18 at 02:51
  • One more thing, if possible, people should look into using GhostScript directly, instead of ImageMagick, as it an do the same task of PDF conversion much much faster than IM. – Mooncrater Mar 27 '19 at 05:19
  • See https://stackoverflow.com/questions/52861946/imagemagick-not-authorized-to-convert-pdf-to-an-image/52863413#52863413 – fmw42 Nov 07 '19 at 06:39

4 Answers4

100

emcconville is correct. More specifically edit the Imagemagick policy.xml file to uncomment this line:

  <!-- <policy domain="module" rights="none" pattern="{PS,PDF,XPS}" /> -->


And change it from rights="none" to rights="read|write"

  <policy domain="module" rights="read|write" pattern="{PS,PDF,XPS}" />


This was a recent addition to the policy.xml file, I believe, due to a security flaw found in the Ghostscript delegate. I think that flaw has now been fixed in the current version of Ghostscript, which is 9.25.

NOTE: On some systems the policy line will have domain="coder" rather than domain="module"

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • 3
    On Mac Homebrew I had to remove PDF from `` – OneCricketeer Oct 26 '18 at 07:37
  • Why did you have to do that? Did it not work to just uncomment it and change the rights to "read|write"? – fmw42 Oct 30 '18 at 01:09
  • I didn't have a commented line like you did. I searched the whole file for PDF, and it was in the line I mentioned – OneCricketeer Oct 30 '18 at 03:43
  • This should have worked. ``. Note I changed domain="coder" to domain="module". Perhaps Homebrew miscoded it with the wrong term? – fmw42 Oct 30 '18 at 03:53
  • Maybe. But removing it from `none` rights also works too :) not sure the security implications of that, but I just wanted to document what I had to do to get my script working – OneCricketeer Oct 30 '18 at 03:56
  • Can you try my suggestion above? Note that I edited it as I see yours had domain="coder" and mine has domain="module". If you cannot do that, then that is OK. I understand what you did. I am not sure of the implications of your approach either. – fmw42 Oct 30 '18 at 03:58
  • Where is this file? – rudolfbyker Nov 06 '18 at 18:18
  • `@rudolfbyker` What file? Are you talking about the policy.xml file? If so, see https://imagemagick.org/script/resources.php – fmw42 Nov 06 '18 at 19:09
  • 26
    On Ubuntu 18.04 on `/etc/ImageMagick-6/policy.xml` near the end I had to change the rights from none to `domain="coder" rights="read|write" pattern="PDF"`. – Pablo Bianchi Dec 10 '18 at 21:37
  • Some versions of ImageMagick use coder and others module in the policy.xml file. I am not sure why it was changed. But this is the same solution as I posted above except in my command the policy.xml file used module. – fmw42 Dec 11 '18 at 00:23
  • make sure to update ghostscript https://www.kb.cert.org/vuls/id/332928/ – ykay says Reinstate Monica Mar 05 '19 at 09:36
  • the policy options get evaluated from top to bottom, so you have to add the "ps,pdf,xps"-right under the existing restrictions or comment out those restrictions. – InLaw Nov 22 '19 at 14:57
45

Quick and easy solution:

sudo mv /etc/ImageMagick-6/policy.xml /etc/ImageMagick-6/policy.xml.off

When done, you can restore the original with

sudo mv /etc/ImageMagick-6/policy.xml.off /etc/ImageMagick-6/policy.xml
mivk
  • 13,452
  • 5
  • 76
  • 69
Jarda Pavlíček
  • 1,636
  • 17
  • 16
  • 4
    This works great. Thank you. However, I suggest replacing `rm` with `mv`, and will try to edit your answer. – mivk Sep 17 '19 at 14:52
  • This mv out and back in solution is great! I use this undo-restore now around all my numerous convert commands in scripts involving pdf files. Reliable again after despair. Thanks! – Rob Rutten Nov 07 '20 at 20:18
19

I am using Dockerfile to update an image, and suddenly I got the policy.xml file in my way. although the version of Ubuntu (xenial) was the same and ImageMagick as well.

I ended up removing the single line causing my problem.

RUN sed -i 's/^.*policy.*coder.*none.*PDF.*//' /etc/ImageMagick-6/policy.xml

hope this helps someone

Mathieu J.
  • 1,932
  • 19
  • 29
-2

Use the below command to delete the policy file to fix it, If required you can also take backup of this policy file.

rm /etc/<ImageMagick_PATH>/policy.xml

for me it was ImageMagick6 and the command was :

sudo rm /etc/ImageMagick-6/policy.xml
Sujeet Kumar
  • 1,280
  • 1
  • 17
  • 25