Encountering errors while trying to convert JPG files to PDF on Ubuntu can be frustrating. One common issue is the "convert: not authorized pictures.pdf'" error, which arises when using the
convert` command from ImageMagick. This article provides a detailed explanation of the cause and offers several solutions to resolve it.
The error message "convert: not authorized `pictures.pdf' @ error/constitute.c/WriteImage/1028" indicates a permission issue within ImageMagick's security policy. A security update in ImageMagick, specifically version 8:6.8.9.9-7ubuntu5.13, introduced restrictions on certain operations, including PDF conversion, to mitigate potential vulnerabilities. This update modified the default policy to restrict the reading and writing of PDF files, hence the "not authorized" error.
Here are several approaches to resolve the "convert: not authorized" error:
Warning: While this is a quick fix reported by several users, modifying security settings can expose your system to risks. Proceed with caution.
Locate the policy.xml
file: The file is typically located in /etc/ImageMagick-6/policy.xml
.
Edit the file: Open the file with root privileges using a text editor like nano
or vim
.
sudo nano /etc/ImageMagick-6/policy.xml
Change PDF rights: Find the line that restricts PDF rights. It usually looks like this:
<policy domain="coder" rights="none" pattern="PDF" />
Change rights="none"
to rights="read|write"
:
<policy domain="coder" rights="read|write" pattern="PDF" />
Alternatively, you can comment out or remove the entire line.
Note: Some users have stated " Change destination file format policy respectively .Usually they will be at the end .Thanks"
Save the file: Save the changes and exit the text editor.
This allows ImageMagick to read and write PDF files, resolving the error.
Automated Fix (using sed
)
A one-liner using sed
can automate the process:
sudo sed -i 's#<policy domain="coder" rights="none" pattern="PDF" />#<policy domain="coder" rights="read|write" pattern="PDF" />#' /etc/ImageMagick-6/policy.xml
img2pdf
(Recommended Solution)A safer and more recommended method is to use the img2pdf
tool, which is specifically designed for converting images to PDF without relying on potentially insecure ImageMagick configurations.
Install img2pdf
: If it's not already installed, install it using apt
:
sudo apt install img2pdf
Convert JPG to PDF: Use the following command:
img2pdf --output out.pdf in.jpg
Replace in.jpg
with the name of your JPG file and out.pdf
with the desired name for the output PDF file. You can also convert multiple JPG files at once:
img2pdf image1.jpg image2.jpg -o output.pdf
img2pdf
is considered safer because it doesn't require disabling security limitations within ImageMagick, respecting the intended security measures.
pdftoppm
and ImageMagickAnother alternative involves using pdftoppm
(from the poppler-utils
package) to convert the PDF to a series of JPEG images, then use Imagemagick or another tool to convert those back to a PDF. This sounds counter-intuitive for converting JPG to PDF, but if you are having issues converting PDF to JPG, this is a valid solution.
Install poppler-utils:
sudo apt-get install poppler-utils
Convert PDF to JPEG:
pdftoppm -jpeg input.pdf output
This will create output-01.jpg
, output-02.jpg
, etc., for each page of the PDF.
If security implications are a concern, ensure Ghostscript is up to date. The Ghostscript Release Notes mention fixes to vulnerabilities. Updating ghostscript may resolve the underlying issue preventing ImageMagick from functioning correctly.
policy.xml
file might suffice. Remember to revert after conversion if security is paramount.img2pdf
is the preferred method.pdftoppm
Always prioritize security when making system changes. Consider the implications before disabling security features. It's often best to utilize alternative tools designed to work within existing security constraints.