简体   繁体   English

ImageMagick 无权将 PDF 转换为图像

[英]ImageMagick not authorized to convert PDF to an image

I have a program, in which I need to convert a PDF to an image using Image Magick.我有一个程序,我需要在其中使用 Image Magick 将 PDF 转换为图像。 I do that using the subprocess package:我使用subprocess包来做到这一点:

        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.我认为这是因为 ImageMagick 无权访问 PDF。 What should be done now?现在应该怎么做? I'm on a Linux server.我在 Linux 服务器上。 Any help is appreciated.任何帮助表示赞赏。

emcconville is correct.埃姆康维尔是正确的。 More specifically edit the Imagemagick policy.xml file to uncomment this line:更具体地说,编辑 Imagemagick policy.xml 文件以取消注释此行:

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


And change it from rights="none" to rights="read|write"并将其从 rights="none" 更改为 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.我相信,这是由于 Ghostscript 委托中发现的安全漏洞而最近添加到 policy.xml 文件中的。 I think that flaw has now been fixed in the current version of Ghostscript, which is 9.25.我认为这个缺陷现在已经在 Ghostscript 的当前版本 9.25 中得到修复。

NOTE: On some systems the policy line will have domain="coder" rather than domain="module"注意:在某些系统上,策略行将包含 domain="coder" 而不是 domain="module"

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

I am using Dockerfile to update an image, and suddenly I got the policy.xml file in my way.我正在使用 Dockerfile 更新图像,突然间我遇到了 policy.xml 文件。 although the version of Ubuntu (xenial) was the same and ImageMagick as well.虽然 Ubuntu (xenial) 的版本和 ImageMagick 是一样的。

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希望这有助于某人

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 :对我来说是 ImageMagick6,命令是:

sudo rm /etc/ImageMagick-6/policy.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM