PDF is one of the most popular forms to convey information nowadays, and we often need to manipulate those PDF files. Grabbing some portion of a PDF file as an image for various purposes is quite a common phenomenon. On Linux distros, one can convert PDF to image with ease. We can even specify the image file type here. A whole PDF can be converted into several images depending upon the page number of that PDF. Also, any particular page can be converted as well. Depending upon your necessity, you can choose anyone between them. You have the flexibility to choose.
Converting PDF to Image in Linux System
As I said earlier, you can convert a PDF to an image in several formats. It solely depends on what type of output you want to get. Do not worry! In this post, I am going to let you learn how to convert PDF to image in a Linux system.
To do so, you have to have some mandatory things in your Linux system. You must have bash or any shell. Any app to export your output file. And have to know some commands to do the work. Let us see them in detail.
1. Using the “pdftoppm” Command
The first method we are going to see is using the “pdftoppm” command. To access this command, you have to have the “poppler” tools on your computer.
- For Debian or Ubuntu, the command to install poppler utils is as follows:
sudo apt install poppler-utils
- For Arch Linux, you have to write this command on the terminal:
sudo pacman -S poppler
- And, for RedHat or CentOS, the command goes like this:
sudo dnf install poppler-utils
Once you have poppler installed, for any Linux distro, that can be Ubuntu or Debian-based or Arch Linux, the rest of the syntax is the same for each of them. We can convert a PDF file in various ways. Each of them is going to be discussed below.
a. Converting a Whole PDF to Images
In this procedure, the whole PDF file is going to be converted into images. You can choose the image file type as well. First, you have to use the “pdftoppm” command. Then specify the image format. After that, write the PDF filename and the output name, respectively.
The ideal syntax to do so is as follows:
pdftoppm <image type> <pdf filename> <desired name for the image>
For instance, we have a PDF named “Sample.pdf”. So, how do we convert that whole PDF into images? Just write in the terminal:
pdftoppm -jpeg Sample.pdf images_extracted_from_sample_pdf
The image type can be anything like png or any type of extension. Note an important point here. You must write .jpeg and not .jpg. Writing the short form will cause errors here.
Each page from the PDF file is going to be converted into a jpeg-type photo. The first page will be named “images_extracted_from_sample_pdf-1.jpeg“, and the second one will be named “images_extracted_from_sample_pdf-2.jpeg“, and so on.
b. Converting a Bunch of Pages from the PDF to Images
Let us say that we do not need the whole PDF file to be converted. Only certain pages are to be converted into images. How to do so?
The syntax is quite the name. The only difference that appears here is you have to specify the range of page numbers you want to convert.
pdftoppm <image type> -f sn -l ln <pdf filename> <desired name for the image>
Here the sn depicts the starting page number, and ln depicts the last page number.
We want to convert the pages from 9 to 12 of the same PDF file we took in the previous section. The syntax is going to be:
pdftoppm -jpeg -f 9 -l 12 Sample.pdf images_extracted_from_sample_pdf
The output name will be like this: images_extracted_from_sample_pdf-9.jpeg, and it will count up to 12.
c. Converting a Single Page From a PDF to Image
Actually, it is a derivative of the last procedure we have seen. The syntax to convert a single page is the same. This time, the starting and finishing page number is going to have the same numerical value.
pdftoppm <image type> -f x -l x <pdf filename> <desired name for the image>
Here x represents the page number we want to convert into PDF. If we want to convert the fifth page to be converted, we have to write:
pdftoppm -jpeg -f 5 -l 5 Sample.pdf images_extracted_from_sample_pdf
Only one image file will be created here.
d. Customized Quality for the Converted Image File
Each image we see has a DPI (Dots Per Inch) value related to that image. Usually, when the DPI value increases, the picture quality also increases but takes a large space to exist and vice versa. Sometimes we need to control the DPI value of an image. How to control that? Look at the syntax closely.
pdftoppm <image type> -rx 300 -ry 300 <pdf filename> <desired name for the image>
The pdftoppm command assumes the DPI value to be 150 by default. To change that, we need to set the resolution of both of the axes individually. Let us say we want to set the X resolution to be 300 and the Y resolution to be 350 also. We have to use the -rx and -ry command for that.
For the file we have used previously, the syntax is going to be:
pdftoppm -jpeg -rx 300 -ry 300 Sample.pdf images_extracted_from_sample_pdf
2. Using the “convert” Command
You can also use the “convert” command to convert PDF to image in the Linux system. To have the convert command enabled on your computer, you have to install ImageMagick first. Depending upon your operating system, the syntax of installing Imagemagick varies.
- If you have any Ubuntu or Debian based operating system on your computer, then open the shell and write:
sudo apt install imagemagick
- If the running operating system is RedHat-based or CentOS-based, then first, you have to install the php-devel, gcc, and php-pear as a prerequisite for Imagemagick. To do so, write:
yum install php-pear php-devel gcc
Now your computer is ready to install ImageMagic. Write the following line in the shell:
yum install ImageMagick-devel ImageMagick-perl
The next point is, you have to install the PHP extension of ImageMagick.
pecl install imagick
And then, the final step.
echo “extension=imagick.so” > /etc/php.d/imagick.ini
On the latest versions of CentOS or Red Hat, ImageMagick is not available anymore and has been substituted with GraphicsMagick. To install that, just write down:
dnf info GraphicsMagick
After that, complete this.
dnf install GraphicsMagick GraphicsMagick-devel GraphicsMagick-perl
- On a computer running upon an Arch Linux, open the terminal and write
sudo pacman -S imagemagick
Once you have installed ImageMagick, you are ready to use the convert command.
a. Converting a Whole File into Images
Just like the pdftoppm command, on any Linux distro, the syntax is the same for using the convert command. The common syntax is as follows:
convert <pdf filename> <desired name for the image>.<image type>
For example, let us say that we have a PDF named Sample.pdf, and we want to convert it into png type images. The syntax for doing so is as follows:
convert Sample.pdf images_extracted_from_sample_pdf.png
The naming format for the extracted images is the same as there was in the pdftoppm command.
b. Converting a Single Page from a Document into Image
To convert a single page to an image, the following syntax is to be executed from the terminal:
convert <pdf filename><page number> <desired name for the image>.<image type>
To convert the 10th page of Sample.pdf, we have to write:
convert Sample.pdf images_extracted_from_sample_pdf.png
An important note: the numbering of pages is done upon a zero-based numbering system. So the first page of the PDF is numbered as 0, and the rest will be counted from it.
c. Customizing the Quality of Converted Images
The DPI (Dot Per Inch) and compression can both be set using the convert command. We shall see both of them altogether.
convert -density <DPI value> <pdf filename> -quality <Compression value> <desired name for the image>.<image type>
To convert Sample.pdf into png type images with no compression and 300 DPI, the command line syntax is going to be:
convert -density 300 Sample.pdf -quality 100 images_extracted_from_sample_pdf.png
Here, 100 means no compression should be done. You can set the value to any number under 100 to get compression of that corresponding level.
3. Using GIMP (GNU Image Manipulation Program) to Convert PDF to Images
GIMP is an amazing software to manipulate images in any distro. To install GIMP, follow the simple steps stated below.
- For Debian or Ubuntu-based OS, open the terminal and write:
sudo apt install snapd
After completion of this, install GIMP.
sudo apt install gimp
- For CentOS or RHEL 8, the syntax is :
sudo dnf install gimp
- And, for Arch Linux, the procedure is not that straightforward. First, you have to install the snap repo and then install GIMP from there. Write the following instructions respectively.
git clone https://aur.archlinux/snapd.git cd snapd makepkg -si
Now you have to make a symbolic link to the
sudo systemctl enable --now snapd.socket
ln -s /var/lib/snapd/snap /snap
And here goes the final step…
sudo snap install gimp
At this point, GIMP has been installed on your computer, and you are ready to work with it. Open GIMP form application manager. Go to the “File” option and hit “Open”. Now from this window, search for your desired document that you want to convert. Select that and go ahead.
You can apply different changes to the file from the edit menu. Manipulating files using GIMP is a completely different topic that is not in the scope of this post. After manipulation, again go to the “File” option and hit “Export as”. You can choose the extension type according to your necessity. Hit “Export” and you are done.
So we have come to an end. Here we have seen how to convert PDF documents into images for a Linux system. We have covered several techniques to do that, and each of them is efficient and fruitful. The most popular method is using pdftoppm because of its flexibility. You can go for any of them depending upon your work.
Leave a comment about the post—any suggestions or problems you have faced during your conversion work. Also, do not hesitate to let us know if I have missed anything that should have been covered here. Good day!