convert jpg to pdf

How to Convert JPG to PDF in Linux

JPG and PDF are two commonly used file types in organizations. Many times you may need to convert JPG images into PDF documents. In fact, you may even need to provide this feature in your application or website for your users. In this article, we will look at how to convert JPG to PDF in Linux, using ImageMagick library. It provides a command line interface (CLI) to work with image files. Since it is a CLI tool, you can easily add it to your application/website.

How to Convert JPG to PDF in Linux

Here are the steps to convert JPG to PDF in Linux. We will use ImageMagick utility for this purpose. It allows you to convert JPG as well as other image file types to PDF documents.

1. Install ImageMagick

Open terminal and run the following command to install ImageMagick.

$ sudo apt install ImageMagick

2. Convert JPG to PDF

Once it is installed, you can simply use convert command to convert images to PDF, as shown below. Here is the syntax of convert command

$ sudo convert /path/to/image /path/to/pdf

In the above command you need to specify file paths to both image and pdf files. Here is an example.

$ sudo convert /home/ubuntu/flower.jpg to /home/files/flowers.pdf

Please note, if you do not specify full file paths, convert command will look for it in your present working directory.

Sometimes you may get a policy error message saying “convert: attempt to perform an operation not allowed by the security policy…”

In such cases, open file /etc/ImageMagick-6/policy.xml in a text editor

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

and add the following line to it

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

before the following tag at the end of the file.


Here is what it should look like.

3. Convert Multiple images

The convert command above converts only one image. If you want to convert multiple images, use mogrify command, -format option. The following command will convert all JPG files in /home/ubuntu directory to PDF files.

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

If you want to convert and save the PDF files in another folder /home/data then use -path option also.

$ mogrify -format pdf -path /home/data /home/ubuntu/*.jpg

Imagemagick provides many commands and options to help you work with images. You can even embed these commands inside shell scripts or use them as cronjobs if you want.

4. Automate JPG to PDF conversion

If you want to automate JPG to PDF conversion, then you can add the above command in a shell script as shown below. Create a blank shell script.

$ sudo vi

Add the following lines to it.


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

Save and close the file. Make it executable with the following command.

$ sudo chmod +x

Execute it with the following command.

$ ./

You may even create a cronjob to run the above script daily at 10.a.m. Just open crontab with the following command.

$ crontab -e

Add the following line to it.

0 10 * * * sudo ./jpg_to_pdf >/dev/null 2>&1

Save and close the file. This will run your above shell script daily at 10.a.m.

In this article, we have learnt how to convert JPG to PDF using ImageMagick. You can also use it to convert PNG and other image types to PDF. We also learnt how to convert multiple files at once, and also how to automate JPG to PDF conversion using shell scripts & cronjobs.

Please note, you can also call convert and mogrify commands from within your application. For example, if you are using Python for backend processing, then you can call the above commands using or subprocess.popen commands.

Also read:

How to Install & Configure ElasticSearch in Ubuntu
How to Use Strace Command in Linux
Top SFTP Command Examples
How to Clear Cache Memory in Linux
How to Compare Two Directories in Linux

Leave a Reply

Your email address will not be published.