How to convert PDF to PNG images and back in Linux

Related Content

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 documents). This article describes how to convert PDF to PNG and back in a quick and easy way.

What is PDF format?

PDF, or Portable Document Format, is a file format that was developed by Adobe in the 1990s. PDF files are designed to be platform-independent, which means they can be opened on any type of device. PDFs are also typically smaller in size than other types of files, making them ideal for email attachments or for posting online. In addition, PDFs can be password-protected and contain many features that make them suitable for business use, such as the ability to add watermarks, comments, and form fields. While PDF format has some drawbacks (such as the inability to edit the file without special software), overall it is a versatile and convenient file format.

What is PNG format?

PNG (Portable Network Graphics) is a lossless compression format for digital images. PNG was created to improve upon and replace GIF (Graphics Interchange Format) as an image-file format not requiring a patent license. PNG supports three main types of images: grayscale images, true-color images, and index-color images. PNG also supports transparency in index-color and true-color images. Nearly all web browsers now support PNG. Some operating systems do not come with built-in support for PNG files but most can display them if the proper graphics driver is installed. Many software programs can open and edit PNG files including Microsoft Paint, Adobe Photoshop, and GIMP.

poppler-utils

Poppler-utils is a PDF file rendering library and a set of command-line utilities based on Poppler that enables you to convert PDF to PNG and other file formats including HTML, text, and image files. It is available as a free and open-source project under the GPL v3 license. The project was started by Marco “KcoX” Cimmino in 2006 and is currently maintained by a team of developers. Poppler-utils is used by a variety of applications, including the popular Adobe Reader and Okular PDF viewer.

One of those tools is PDFToPPM, a free open-source program that enables you to convert PDF documents into images in the Portable Pixmap Format (PPM). This can be useful for viewing PDFs on devices that do not have a PDF viewer or printing PDFs when a printer does not have native PDF support. PDFToPPM can also be used to convert PDFs into other image formats, such as JPEG, PNG, and TIFF. PDFToPPM is released under the GNU General Public License and is available in package repositories.

Installing poppler-utils

To install the poppler-utils package in Ubuntu, 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 files to single or multiple images.

Convert entire PDF document to images

To use PDFToPPM, simply specify the input and output files, and the desired image format. Optionally, you can also specify the resolution of the output images.

Let’s convert PDF to PNG using this tool:

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 document 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 a single PDF page to an image

You can use the PDFToPPM tool to convert PDF to PNG for single or any number of pages. Just specify 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

As a result, you’ll get a single PDF page rendered as a PNG file.

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

As a result, you’ll get a set of PNG files generated from specified PDF 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 at how we can save images as PDF documents using ImageMagick.

ImageMagick

ImageMagick is a software suite for creating, editing and converting images. It can be used to create everything from simple thumbnails to complex photomontages. ImageMagick supports a wide range of image formats, including popular formats such as PNG, JPEG, and GIF. In addition, ImageMagick can be used to perform a variety of image processing tasks such as resizing, cropping, and color correction. ImageMagick is free and open-source software that is available for Windows, macOS, Linux, and other operating systems. The CLI tool responsible for file transformation from one format to another is called “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 files to a PDF document. Check out The ultimate guide to image manipulation with the ImageMagick article to get more information about it. Overall, the convert CLI tool is the perfect PNG converter to PDF and various other image formats.

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 a set of images to a PDF document

Use the command given below to convert all the images into PDF files:

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

How to convert PDF to PNG online for free?

One popular option is to use an online free PDF converter such as pdfsimply.com, pdf2png.com, or cloudconvert.com. These websites allow you to upload your PDF and download the converted PNG file. Another option is to use a browser extension such as Adobe Acrobat Reader DC. This extension will allow you to open PDFs in your browser and save them as PNGs. Just upload PND and download PNG. Finally, you can also use a desktop program such as GIMP or Adobe Photoshop to open your PDF and export it as a PNG file. Whichever method you choose, converting PDF to PNG is relatively simple and can be done for free.

Summary

This article described how to convert PDF files to PNG images and PNG images to PDF files using Linux command-line tools such as pdftomppm and ImageMagick.

Know the better way? Let us know about it in the comments section below!

LIKE THIS ARTICLE?
Facebook
Twitter
LinkedIn
Pinterest
WANT TO BE AN AUTHOR OF ANOTHER POST?

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

Table of Contents