Sometimes we need quickly convert a PDF file into images or save a set of images as a PDF file. To do this, we can use a couple of command-line tools such as poppler-utils (converts PDF to image files) and ImageMagick (converts images to PDF document). This article describes how to do it in a quick and easy way.
Table of contents
One of those tools is pdftomppm that converts PDF documents to images in formats like PNG or JPG. This tool will help you to save a complete PDF file, single or range of pages from your PDF document to an image(s).
To install poppler-utils, we need to execute the following command:
sudo apt install poppler-utils -y
Now, you can use the pdftoppm command to transform PDF file to single or multiple images.
Convert complete PDF file to images
Let’s convert the
How-to-convert-PDF-to-images-and-back-in-Linux.pdf file to PNG images:
pdftoppm How-to-convert-PDF-to-images-and-back-in-Linux.pdf How-to-convert-PDF-to-images-and-back-in-Linux -png
In this command:
How-to-convert-PDF-to-images-and-back-in-Linux.pdfis a PDF file name
How-to-convert-PDF-to-images-and-back-in-Linuxis a common prefix for all images names
-pngis a target image format
As a result of execution you’ll get a nice PNG image of every single PDF page.
Convert single PDF page to image
You can convert any page from a PDF document to the image by specifying the first (
-f) and the last (
-l) arguments for the same command.
For example, to convert only the first page of the document, use the following command:
pdftoppm -f 1 -l 1 How-to-convert-PDF-to-images-and-back-in-Linux.pdf How-to-convert-PDF-to-images-and-back-in-Linux -png
Convert a range of PDF pages
In the same way you can convert a range of PDF pages to images:
pdftoppm -f 2 -l 3 How-to-convert-PDF-to-images-and-back-in-Linux.pdf How-to-convert-PDF-to-images-and-back-in-Linux -png
Adjusting image quality (DPI)
pdftoppm converts PDF pages to images using 150 DPI.
To modify the resolution, use the
-rx argument to specify the X resolution and the
-ry number to determine the Y resolution, for example:
pdftoppm How-to-convert-PDF-to-images-and-back-in-Linux.pdf How-to-convert-PDF-to-images-and-back-in-Linux -png -rx 200 -ry 200
As soon as we’ve covered PDF to image transformation, let’s take a look how we can save images as PDF file using ImageMagick.
ImageMagick is a widely used set of command-line tools for converting images from one format to another. One of such tools is “convert.”
Convert allows you not only to scale, blur, crop, despeckle, fade, draw on, flip, join, and re-sample images, but you can use it to convert one or more images to a PDF file. Check out The ultimate guide to image manipulation with the ImageMagick article to get more information about it.
To install the ImageMagick tools, use the following:
sudo apt install imagemagick
Convert images to PDF file
Use the command given below to convert all the images into pdf file:
convert *.png My-Awesome-PDF-file.pdf
Note: if you’re facing convert-im6.q16: attempt to perform an operation not allowed by the security policy `PDF’ @ error/constitute.c/IsCoderAuthorized/408. error, you can fix it by executing the following command:
sed -i '/disable ghostscript format types/,+6d' /etc/ImageMagick-6/policy.xml
In this article we’ve described how to convert PDF files to PNG images and PNG images to PDF file using Linux command-line tools such as pdftomppm and ImageMagick.
Know the better way? Let us know about it in the comments section below!
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?