Linux Applications

How to Convert Image to PDF on Linux Command Line?

Converting an image file to a PDF can be a nerving task for Linux beginners. Image conversion to PDF requires a command-line utility that maintains image resolution or offers minimum loss of quality. This is why, in this article, we will introduce command-line utilities to convert single/multiple images to a PDF.

ImageMagick

ImageMagick is an open-source, cross-platform image processing suite. It is a ready-to-run binary distribution that a user can utilize to modify, copy, or distribute in all free/proprietary applications.

Another main advantage of this utility is that it incorporates several threads to improvise computational performance to read, write, or process images of various sizes.

The capability of this utility isn’t limited to image conversion only. It can also edit and create images into various image file formats. Unlike other image processing suites, it performs bulk or repetitive imaging operations and runs at the backend of various web servers, content management systems, and social media networks.

Getting Started

To begin with, update the repository to download the latest available version.

[email protected]:~$ sudo apt-get update

[email protected]:~$ sudo apt-get install imagemagick -y

Convert PNG, JPEG Image Format to PDF

First, cd into the image’s directory and list all the images.

[email protected]:~$ cd /tmp/images && ls

Image01.png Image02.png Image03.png

The above output shows all images in the PNG format. Note that we can apply the same image conversion operations on the jpeg file format.

Convert Option

The convert command is the ImageMagick powerhouse, as it’s used for image conversion in all possible formats along with other tasks like image resizing, blurring, drawing, cropping, resampling, and many others.

Let’s list all the possible methods the utility provides with the “convert” command for image conversion to PDF:

  • Convert one image file to PDF

To convert an individual PNG file to PDF, run the following command:

ubuntu@ubuntu:/tmp/Images$ sudo convert Image01.png Image01.pdf

convert-im6.q16: not authorized `Image1.pdf' @ error/constitute.c/WriteImage/1037.

The above command may generate an error, as shown above, due to the changes in the ImageMagick global policy file (policy.xml). These changes were recommended as a mitigating step towards an ImageTragick vulnerability for versions below 7.0.1-2.

Go to the /etc/ImageMagick-6/ directory and open the policy.xml file in your favorite editor.

[email protected]:~$ cd /etc/ImageMagick-6/

ubuntu@ubuntu:/etc/ImageMagick-6$ sudo vim policy.xml

Change the rights in the following policy from “none” to “read|write”:

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

To

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

Run the “convert” command again and list the directory to find a new pdf file.

  • Convert Multiple Images to PDF

The “convert” command also allows converting multiple or all images within the folder to a pdf file. Use the following command to convert multiple images to pdf:

[email protected]:/tmp/Images$ sudo convert Image1.png Image2.png Image.pdf

Moreover, ImageMagick extends the Linux filename globbing concept. This feature refers to all the image files in the folder for file conversion.

[email protected]:/tmp/Images$ sudo *.png Images.pdf

The above command generates a PDF file containing all the PNG format images of the current folder.

However, ImageMagick does not offer lossless conversion for large-size images with minimum time and memory usage. The drawback leads us to another command-line utility that doesn’t require the user to re-loosen ImageMagick security regulations.

img2pdf

img2pdf is an image conversion utility for raster images to PDF. It’s best suited for the requirements of:

  • lossless image conversion: images in PDF with exact color details for each pixel
  • fast: does not require CPU to re-encode pixel data
  • small: the only file size difference between input and output is due to the PDF container overhead

Another advantage of img2pdf is that it does not load raw pixel details to the memory. This quality feature enables the utility to easily handle large-size input.

Getting Started

Update ubuntu repository to install img2pdf.

[email protected]:~$ sudo apt-get install img2pdf

Image Conversion

The method to convert images to pdf for this utility is somehow similar to ImageMagick. Go to the stored images directory cd /tmp/Images to:

  • Convert a single image to PDF
[email protected]:/tmp/Images$ sudo img2pdf image01.png -o output_img1.pdf
  • Convert multiple or all files to PDF
[email protected]:/tmp/Images$ sudo img2pdf image01.png image02.png -o output_img.pdf

Or

[email protected]:/tmp/Images$ sudo img2pdf *.png -o output_imgs.pdf

List all the files in the folder to notice newly generated PDF files.

Img2Pdf vs. ImageMagick

Create a large-size test image to compare the lossless-fast conversion of both utilities.

[email protected]:/tmp/Images$ sudo convert logo: : -resize 9000x image.png

Convert the above PNG image to PDF while using both utilities. Use the Linux time command to note the elapsed time taken by the utilities.

ubuntu@ubuntu:/tmp/Images$ sudo time img2pdf image.png -o img2pdf.pdf

4.15user 0.35system 0:04.99elapsed

ubuntu@ubuntu:/tmp/Images$ sudo time convert image.png ImageMagick.pdf

6.32user 0.33system 0:09.22elapsed

Now open the images to compare both PDFs with the original.

Another way for lossless output in ImageMagick is to use Zip compression, but this method increases the output size.

Conclusion

The article summarizes Linux command-line utilities for image conversion from any image file format to PDF. We introduced ImageMagick and img2pdf as well as discussed their pros and cons to provide a better overview for the readers to choose preferred utility.

About the author

Admin

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.