How to convert PDF to PNG images and back in Linux

Andrei Maksimov

Andrei Maksimov

0
(0)

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.

poppler-utils

poppler-utils is a precompiled set of command-line utilities (based on Poppler) for manipulating PDF files and converting them to other formats.

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).

Installing poppler-utils

To install poppler-utils, we need to execute the following command:

sudo apt install poppler-utils -y
1. How to convert PDF to image and back in Linux - apt install poppler-utils

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
2. How to convert PDF to image and back in Linux - pdftoppm convert complete file

In this command:

  • How-to-convert-PDF-to-images-and-back-in-Linux.pdf is a PDF file name
  • How-to-convert-PDF-to-images-and-back-in-Linux is a common prefix for all images names
  • -png is 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
3. How to convert PDF to image and back in Linux - pdftoppm convert single page

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
4. How to convert PDF to image and back in Linux - pdftoppm convert range of pages

Adjusting image quality (DPI)

By default, 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
5. How to convert PDF to image and back in Linux - pdftoppm adjusting image resolution (DPI)

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

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.

Installing ImageMagick

To install the ImageMagick tools, use the following:

sudo apt install imagemagick
1. The ultimate guide to image manipulation with ImageMagick - apt-get 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
6. How to convert PDF to image and back in Linux - convert images to pdf file

Summary

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!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Subscribe to our updates

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Want to be an author of another post?

We’re looking for skilled technical authors for our blog!

Leave a comment

If you’d like to ask a question about the code or piece of configuration, feel free to use https://codeshare.io/ or a similar tool as Facebook comments are breaking code formatting.