1. Introduction

Keeping track of multiple images would take time and effort, especially in different directories. So, combining them into a single file makes things more organized and easily accessible. For this purpose, we can use the Portable Document Format (PDF), one of the most commonly used file viewing tools.

In this tutorial, we’ll discuss different methods of converting multiple images to PDF files. Although various tools are available for this purpose, we’ll focus on two most commonly used tools: imagemagick and img2pdf. Lastly, we’ll also look into their strengths and weaknesses.

2. Using ImageMagick

ImageMagick is a third-party tool that needs to be installed first. Its installation depends on the Linux distribution that we’re using:

$ sudo apt install imagemagick            # For Ubuntu
$ sudo yum install ImageMagick            # For CentOS 
$ sudo dnf install ImageMagick            # For Fedora

We can use the above commands to install the imagemagick for our distribution. It is worth noting that the commands are all case-sensitive.

2.1. Prerequisites

It is necessary to make some changes in the policy.xml file of the imagemagick. Otherwise, this conversion process will fail. We need sudo privileges to complete this action:

$ sudo nano /etc/ImageMagick-6/policy.xml

Here, we use the nano text editor to read the content of policy.xml. Alternatively, other well-known editors like vim or emacs can also be used. Next, we need to scroll down to find the line:

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

Here, we need to provide read and write permissions for PDF:

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

This only works if we’ve got the write permissions for the policy.xml file as well. Otherwise, we can provide it using the chmod command.

2.2. Converting Multiple Image Files to a Single PDF

We’ll use the ls command first to display the content of the directory before converting them to PDF:

$ ls 
image1.jpg image2.jpg image3.jpg

Currently, we’ve three images that can be converted to PDF using its convert option. Say we want to name the output file images.pdf:

$ convert image1.jpg image2.jpg image3.jpg images.pdf

This command converts the three selected image files into a single PDF file, images.pdf, which can be verified using the ls command again:

$ ls 
image1.jpg image2.jpg image3.jpg images.pdf

Similarly, we can also use it to convert other image formats like .png, .tiff, or .bmp:

$ convert image1.jpg image2.png image3.bmp image4.gif image5.tiff images.pdf

If we’re in a different directory, then we need to provide the complete directory path for the conversion:

$ convert /home/Downloads image1.jpg image2.jpg image3.jpg image1.pdf

We need to make sure that the provided path is correct. Otherwise, the process will fail.

If the number of images desired to be converted into PDF is too large, typing a long list of file names can get tedious. So, in this situation, we can provide the extension names only to convert all the images into PDF:

$ convert /home/Downloads/selected_images *.jpg *.png images.pdf

This is not always recommended, as sometimes we only want to convert specific images. In such cases, moving the desired images to a new directory and converting them is advisable.

2.3. Converting Single Image File to PDF

We can also convert each image into its respective PDF file using the mogrify option of the imagemagick if a single file is not required:

$mogrify -format pdf *.jpg
$mogrify -format pdf /home/Downloads/*.jpg

Let’s execute the ls command again for a better understanding:

$ ls
image1.jpg image2.jpg image3.jpg image1.pdf image2.pdf image3.pdf

The output confirms the successful creation of the PDF files for each image.

2.4. Managing File Size

Converting multiple images to a  single PDF file can consume significant space. To control the output file size, we can use the -quality option of the imagemagick tool. We can use the du command to check the size of the output file created earlier:

$ du -h images.pdf
20M	images.pdf

Now, let’s reduce the file size:

$ convert -quality 50 *.jpg newfile.pdf

This reduces the quality of each image to 50 percent and then converts all the images into PDF with the filename newfile.pdf. So, we need to balance the size and quality, which should be acceptable. Let’s compare the size of both PDF files to see if it reduces the size or not:

$ du -h images.pdf newfile.pdf
20M	images.pdf
12M	newfile.pdf

The newly created PDF file, newfile.pdf, takes 12M, which is comparatively less than the original.

3. Using img2pdf

We can use img2pdf  to convert images to PDF files, but we need to install it first:

$ sudo apt install img2pdf          # For Ubuntu

For other distributions, we can install it using:

$ pip3 install img2pdf

We also need to install the Python to install img2pdf using:

$ sudo yum install python3
$ sudo yum install python3-pip

After installing img2pdf, we can convert images to PDF using:

$ img2pdf image1.jpg image2.png image3.bmp -o images.pdf

This command converts the three images to a single PDF file with the name images.pdf. Similarly, we can also convert images to PDF by giving the extension name:

$ img2pdf *.jpg *.png -o images.pdf

In both commands, the -o option provides the output name, images.pdf.

4. Comparison

We’ve discussed two different tools to convert images to PDFs. So the question arises, which one is better among these two? To answer this, let’s compare the time that both would take to convert images to PDF:

$ time convert *.jpg images.pdf

real	0m9.936s
user	0m5.653s
sys	0m4.303s

$ time img2pdf *.jpg -o file.pdf

real	0m1.822s
user	0m0.782s
sys	0m1.033s

The output clearly shows that the img2pdf takes the lead and consumes less time for file conversion. But things get interesting when we compare the PDF file size:

$ du -h output.pdf newoutput.pdf 
20M	images.pdf
28M	files.pdf

We can see the noticeable difference in file sizes created by these two tools, which are 20M for imagemagick and 28M for img2pdf. The imagemagick generally reduces the size of the images while converting, and that’s why it takes more time and consumes less space. In contrast, the img2pdf is specially designed to convert images to PDF while retaining the original quality of the images. This is why it consumes less time while converting but takes more space.

5. Conclusion

In this article, we’ve discussed imagemagick and img2pdf to convert multiple images to PDF files. Each of these tools has its pros and cons. Therefore, choosing the right tool depends on our requirements.

The output file created by imagemagick is smaller than img2pdf without much compromise to the original image quality. On the other hand, img2pdf  is also a highly effective tool that is specifically designed for converting images into PDFs. It is more efficient at doing its job and does that much faster than imagemagick.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.