If you’re looking to convert image to PDF on Command Line on Linux, you’ve come to the right place. Here’s a detailed guide on the tools you can use and how to do it.
Often, you want to convert a set of images to a portable document format (PDF) document. For instance, you want users of your website to download content as PDF rather than images. So, you will first convert these images to a PDF document and present them as a link to online users.
There are three approaches to performing this conversion task on Linux:
- Online tools: There are several online tools that can convert images to PDF documents. Amongst these tools are smallPDF and ilovePDF. However, this approach may expose your data to a third-party server. In addition, these online tools may embed their watermark on the PDF document.
- GUI-based tools: Compared to the online tool, the better approach is to perform this conversion on your local machine. For this, there are several tools provided by Linux.
Some of them are GUI based, and some are based on the command line. Amongst the popular GUI-based tools are LibreOffice and gScan2PDF.
- Command line tools: A command line tool can be used to perform the conversion by specifying text commands on the console and also specifying various options or flags. The advantage of a command line interface (CLI) is that one can automate the whole process.
You can also use shell programming to perform many repetitive tasks programmatically.
How to Convert Image to PDF on Command Line on Linux
In this article, we will discuss various command line tools that can be used to convert images to PDF documents.
It is one of the most widely used cross-platform conversion tools. Owing to its multithreading nature, it can perform conversion very quickly. It can perform bulk operations and, therefore, generally run in the backend of various servers and content management tools.
To install ImageMagick, run the following command on your Linux machine:
$ sudo apt update $ sudo apt install -y imagemagick
The basic usage of ImageMagick is as follows:
$convert image document.pdf
Converting a single image to a PDF document
Now move to the directory where your pictures are located by using the cd command:
$ cd pictures
And convert image1.jpg to doc1.PDF using the following command:
$ convert image1.jpg doc1.pdf
You may encounter the error “operation not allowed” when converting the document. To resolve this error, you can edit the policy.xml file located in the ImageMagick directory.
Open the file in your favorite text editor, such as nano, using the following command:
$ sudo nano /etc/ImageMagick-6/policy.xml
Search for the following line in an XML document:
<policy domain="coder" rights="none" pattern="PDF" />
Change the rights attribute from “none” to “read|write” as follows:
<policy domain="coder" rights="read|write" pattern="PDF" />
Save the file and rerun the command to convert the image.
$convert image1.jpg doc1.pdf
Alternatively, you can run the following command to edit the document directly:
sudo sed -i's/^.*policy.*coder.*none.*PDF.\*//' /etc/ImageMagick-6/policy.xml
Converting multiple images to document
You can also convert multiple documents into PDF documents. You can provide the name of the images separated by spaces. Alternatively, you can provide the wild card expression (e.g. *.jpg) to select all the images to be converted
$ convert *.jpg doc.pdf
ImageMagick is a handy tool for converting images to PDF. However, there is a drawback to using ImageMagick. The image quality is reduced when you convert them to PDF, as you can’t specify the image’s resolution while converting.
So, the second tool we will discuss is Img2PDF, which converts the images to PDF without losing the quality of the images. In this tool, you can specify the size of the image while converting the image to PDF. The advantage of this tool is lossless, fast conversion along with reduced size.
In addition, the tool doesn’t load the raw pixel into memory. Hence, it can handle large file sizes.
There are two approaches to installing the img2PDF (i.e. using apt or using pip). To install the tool using apt, type the following on the command line:
$ sudo apt install img2pdf
To check that the tool is correctly installed, verify by typing:
Another approach to installing the img2PDF is to use the pip utility. Type the following command to install:
$ pip install img2pdf
Converting a single image to a PDF
Move to the folder where images are located. Now, type the following command to convert a single image to PDF:
$ img2pdf pic1.jpg -o doc.pdf
–o flag is used to specify the name of the output file.
Converting multiples images to PDF
If you want to convert multiple images, you can specify them by spaces. Alternatively, you can set them using wild card expressions.
$ img2pdf*.jpg -o doc.pdg
Specifying the size of the image while converting
While converting the image to PDF, you can also specify the size of the image of the pagesize. For this purpose, you can use the ‘
–imgsize’ or the ‘
For example, to specify the size of the image as 50cm by 75cm, use the following command:
$ img2pdf img1.jpg --imgsize 50cmx75cm -o doc.pdf
This article discusses various command line utilities that you can use to convert images to PDFs. Besides the tool discussed, there are online utilities and GUI-based tools for converting images to documents. However, command line tools can better help automate a range of tasks and are generally preferred by proficient users.
If you have any questions on converting images to PDF in Linux OS, don’t forget to leave a comment below.
If this guide helped you, please share it.