How to convert pdf to image on Linux command line

PDF files are one of the most popular files that can be found all over the Internet. However, it is always possible to get the most out of them to handle them. And today you will learn how to convert PDF to image using the terminal.

The Linux terminal is a marvel where you can perform many tasks to be more productive at work. You can even extract each page of a PDF into an image that can be manipulated later.

To achieve this, you need certain tools available on Linux that, with a terminal interface, you can set up the necessary options to extract the pages.

To perform this tutorial, we can use two different tools, but that fulfill the objective to the fullest. They are pdftoppm and convert These tools are easily installable on Linux.

The pdftoppm command to convert PDF to Image

The pdftoppm command converts pages of PDF documents to image formats such as JPG, PNG, or others. Although it is not present by default in Linux distributions, it is easily installable.

This command is part of poppler which is a PDF rendering library based on the xpdf-3.0 code base. This library opens up many possibilities for editing PDF files.

Therefore, it is necessary to download and install poppler on your system to enjoy pdftoppm.

First open a terminal and update your system, then run one of these commands

#For Debian, Ubuntu, Linux Mint and the like
sudo apt install poppler-utils 
#On RHEL, CentOS or Fedora based systems
sudo dnf install poppler-utils 
#On SUSE and OpenSuse
sudo zypper install poppler-tools 
#On Arch Linux, Manjaro and derivatives
sudo pacman -S poppler

Now, with the command installed, you can use it.

Using pdftoppm command to convert PDF to images using the terminal

The basic syntax of the command is as follows

pdftoppm -[image_format] [pdffile] [image_name]

It supports export to PNG or JPG, so you can choose. You can also use an absolute path to define the input PDF file, as well as an absolute path for the output.

An example of how to use it would be to run

pdftoppm -png sample.pdf sample_image

This will generate an image file for each page of the PDF file (sample-image-01, sample-image-02, … ). Therefore, the most logical option is to set a range of pages to convert.

To achieve this, follow this syntax

pdftoppm -[image_format] -f [first-page-toconvert] -l [last-page-to-convert] [pdffile] [image_name]

For example:

pdftoppm -png -f 1 -l 5 sample.pdf image

In this case, an image file of pages 1 to 5 of the document named sample.pdf will be created.

By default, pdftopm uses 150 Dpi, which is sufficient for most cases. However, it is also possible to modify this value to increase the quality of the resulting image.

This can be achieved with the rx and ry options, which set the amount of horizontal and vertical pixels.

For example:

pdftoppm -png -rx 300 -ry 300 sample.pdf sample_image

So, the image quality will be higher, but the weight of each file will be higher as well.

The convert command on Linux

The convert command is part of imagemagick. ImageMagick is a free and open-source software suite for displaying, converting, and editing raster image and vector image files. It can read and write over 200 image file formats.

So to edit and work with images, it is necessary to use this tool on Linux. Well, it has a command that allows you to make a PDF file to be exported into different image files.

First, you will have to install it

#Debian, Ubuntu and derivatives
sudo apt install imagemagick

#On RHEL, CentOS or Fedora based systems
sudo dnf install ImageMagick ImageMagick-devel ImageMagick-perl

Once it is installed, you can use it.

Using the convert command to convert PDF to images using the terminal

The syntax of this command is quite simple to use.

convert [pdf-file] [output].[format]

For example,

convert sample.pdf output.jpg

In this case, each page of the sample.pdf file will be exported in several files starting with output.jpg.

You can also specify a specific page

convert sample.pdf[44] output.png

In this case, page 44 will not be converted but page 45 since the count starts at 0.

Finally, you can change the quality of the exported image with the options -density and quality.

convert -density [value] [pdf-file] -quality [value] [output].[format]

Where -density changes the Dpi of the image. The most commonly used values are between 150 and 300. And --quality sets the compression level of the image, where 100 means that there is no compression and therefore the quality is higher.

Conclusion

Manipulating PDF files can be a headache, but thanks to external tools you can even convert them to images.

I hope you liked this post and can share it with all your friends.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top