62

I have multiple *.jpeg pictures I like to merge into a single *.pdf file like so:

convert Blatt1.jpg Blatt2.jpg Blatt3.jpg Blatt4.jpg out.pdf

I am prompted with the following exception:

convert: attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/408

What causes this problem and how can I fix it? I did some research but couldn't find anything about this particular problem.

n00b.exe
  • 723
  • 1
  • 5
  • 4

2 Answers2

98

ImageMagick has some security policies disabling some rights for security reasons. See why at the bottom of this answer.
You will have to edit a config file to re-enble the action you need.

Open /etc/ImageMagick-6/policy.xml with your favorite text editor, find the line <policy domain="coder" rights="none" pattern="PDF" /> and replace "none" by "read|write"

# Open the file 
sudo nano /etc/ImageMagick-6/policy.xml

find and edit the line

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

to :

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

About ImageMagick security policy
The restricted policy is made to prevent unknown vulnerabilities coming from third party software as Ghostscript used here for PDF files. Be sure to update Ghostscript.

cmak.fr
  • 8,696
  • Perfect! Exactly what Iw as looking for! Thanks a lot! – n00b.exe Mar 20 '19 at 15:11
  • 3
    For anyone who winds up here, I recommend looking at the question this has been marked as a duplicate of. You really don't want to relax ImageMagick's security policy when there are alternatives that are both safer and more lossless than asking ImageMagick to decode JPEGs and then re-encode them as part of making a PDF. – ssokolow Feb 16 '20 at 03:34
  • Disabling security limitations is just a bad idea, whether it works or not. ImageMagick thinks there is a security issue. Unless you know exactly what you are doing, the right thing to do here is use other software which is held to be secure by its own authors. – Sqerstet Jun 08 '20 at 13:52
  • 3
    @Sqerstet :have you red the imagemagick documentation about security policy.xml ? No... ImageMagick thinks there is a security issue. Nope, they do not. The restricted policy is made to prevent unknown vulnerabilities coming from third party software as Ghostscript used here for PDF files. If Ghostscript is up to date, then no (known) issue. Have a nice day and please, read, read, read – cmak.fr Jun 08 '20 at 15:24
  • @Sqerstet : You confirm what I thought. Your previous comment is not based on information but on a belief. This site is a site of help and information, no place for convictions. If you don't have any information, don't write your beliefs. Thank you for the community. – cmak.fr Jun 08 '20 at 19:03
  • 1
    Since you chose to only read the first sentence, I have deleted it because it changes nothing. You are recommending users to disable settings which were set "for security reasons" (your words), without even explaining the implications. Sorry, but that is irresponsible. – Sqerstet Jun 09 '20 at 05:01
  • 1
    If I know I'm only going to process my own files, generated by my own scripts, I feel it is safe to disable the blockings.

    Is there a way to have multiple policies?

    – Lenne Jul 26 '20 at 19:38
  • 2
    Additional info: be sure, you have installed GhostScript >= 9.26. After that you can play around with policy.xml https://www.suse.com/support/kb/doc/?id=000019384 – Hermann Schwarz Mar 17 '21 at 07:36
  • Scripted: sudo sed -i 's#"coder" rights="none" pattern="PDF"#"coder" rights="read|write" pattern="PDF"#' /etc/ImageMagick-6/policy.xml – A T Sep 04 '22 at 16:59
  • The fix (upgrading to a newer ghostscript) was already addressed in bionic-updates (ghostscript-9.26~dfsg+0-0ubuntu0.18.04.17). This means in any newer LTS release (>= focal), you're not affected by this bug, but the policy is still there. – Olaf Dietsche Dec 31 '22 at 18:28
6

Because of a known bug with security implications, the conversion to pdf is disabled in ImageMagick convert.

I suggest that you work around the problem,

  • either
    • Import the pictures into LibreOfffice and save the document.
    • Export as pdf from LibreOffice.
  • or
    • install img2pdf from the repository 'universe'

      sudo apt install img2pdf
      

      and run the following command line to create a pdf file with several pictures corresponding to the question or something similar for other cases,

      img2pdf --out out.pdf Blatt1.jpg Blatt2.jpg Blatt3.jpg Blatt4.jpg
      
    • There are details for fine tuning in the manual man img2pdf


Edit: There is a solution now, ghostscript is new enough in my computers, and I think in most up to date Linux operating systems:

Additional info: be sure, you have installed GhostScript >= 9.26. After that you can play around with policy.xml https://suse.com/support/kb/doc/?id=000019384:

So we can do the edits in the accepted answer and start using ghostscript again to convert to pdf with ImageMagick convert.

sudodus
  • 46,324
  • 5
  • 88
  • 152
  • 3
    Downvoted for suggesting an interactive replacement for a batch command which could easily have been excerpted from a larger script. – ssokolow Feb 15 '20 at 09:36
  • 2
    Upvoted for not recommending people to break default security settings. Another option here: https://askubuntu.com/a/1210721/304983 – Sqerstet Jun 08 '20 at 15:49
  • 2
    @Sqerstet these default security settings are utterly irrelevant for most users. That's not to say they're useless: for those few users who run ImageMagick in some web service, it is a severe security issue, so the developers were certainly right to disable this just to enforce fixing the issue. But for anybody else who just uses ImageMagick with PDFs they have complete control over, it is much more sensible to re-enable it than to switch to a completely different tool, certainly one as badly suited for the task as LibreOffice. (Though, the only proper solution is to fix GhostScript...) – leftaroundabout May 18 '21 at 10:34
  • 1
    @leftaroundabout You are almost certainly right but that's not the point. Recommending that people break default security settings is a terrible idea. As you say, the solution is to fix the problem. Or use alternatives, because apparently ImageMagick cannot properly maintain its software. The alternative I recommended was not LibreOffice. – Sqerstet May 19 '21 at 09:50