How to convert pdf to image on Linux command line

I have many PDF files, and I need to convert them to a png file format, add a border to those images, and convert back all those images to pdf format. How can I convert pdf to image format on Linux and vice versa using the CLI?

Portable Document Format (PDF) files are typically used everywhere; however, manipulating them via the Linux command line is hard. On the other hand, managing images is easy on Linux. Let us see how to convert pdf to png image format, manipulate it and convert back the png file to pdf again directly from your Linux terminal.

How to convert pdf to image on Linux command line

Tools needed to transmute pdfs to png/jpeg formats:

  1. Linux command line especially bash or any modern shell
  2. pdftoppm command – Portable Document Format (PDF) to Portable Pixmap (PPM) converter (image format).
  3. convert command – Convert between image formats and resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more. We can use the same tool to convert images to pdf again.
  4. GIMP app – We can use the GIMP app to import PDF file and then export as PNG/JPG image format.

Converting pdf to image on Linux

The syntax is as follows:
pdftoppm {input.pdf} {output.file} -png
For example, convert pdf called Bulldog.pdf to Bulldog.pdf.png as follows:
$ pdftoppm Bulldog.pdf Bulldog.pdf -png
$ ls -l Bulldog.pdf*

How to convert pdf to image format on Linux
Please note that each page from the PDF file is converted to Bulldog.pdf-1.png, Bulldog.pdf-2.png, and so on. Then we can manipulate a single image manually using Imagemagick or GIMP. For example, add a border to the first image only. For instance:
$ gimp Bulldog.pdf-1.png
$ gimp Bulldog.pdf-?.png
$ ksnip -e *.pdf-1.png

Adding a border using Imagemagick to your image

Again the syntax is pretty straightforward to add a border using the Linux CLI:
$ convert -border {SIZE} -bordercolor {COLOR_NAME} {input.file} {output.file}
$ convert -border 20 -bordercolor Bulldog.pdf-1.png out-Bulldog.pdf-1.png
$ ls -l *Bulldog.pdf-1.png
$ xdg-open out-Bulldog.pdf-1.png


Click to enlarge

Transforming image to pdf format

Now that we have edited our image, it is time to build back pdf file. Let us create a new directory as follows using the mkdir command:
$ mkdir out

Transforming image to pdf format is simple:
$ convert manipulated-input-1.png input-2.png input-N.png out/output.pdf
$ convert out-Bulldog.pdf-1.png Bulldog.pdf-2.png Bulldog.pdf-3.png out/final-Bulldog.pdf
$ xdg-open out/final-Bulldog.pdf

Putting it all together

For a large number of files, you may want to write a shell script. Here is a handy bash for loop on my Ubuntu Linux desktop that I used:

# Step 1 - PDF to IMAGE / PNG format #
for p in *.pdf
   pdftoppm "$p" "$p" -png

Then I used gimp to edit the first png image:
$ gimp *.pdf-1.png
Overwrite and saved all images in the same folder. Quit from GIMP and get back to the Linux terminal. Then finally convert back all images to pdf in the output folder:

# Step 2 - IMAGE / PNG to PDF format #
mkdir output
for i in *.pdf
  convert $b-?.png "output/$b" 

We can compress files using the zip command and send it to the concerned party via email or cloud storage:
$ zip -r -e /tmp/project-invoices.pdf /tmp/output

  • -r : Recurse into directories
  • -e : Password protect our zip file
  • /tmp/project-invoices.pdf : Zip file name
  • /tmp/output : Path/folder name to zip and compress everything for ease of use

A note about GIMP

Start the GIMP app on Linux.

Open the PDF file by visiting to File > Open > Select PDF File > OK:
Import from PDF

Make changes. Then export as PNG file by visting to File > Export As > Name file as “demo.png” > click the Export button:

Wrapping up

In this quick tutorial, we learned about various Linux command-line utilities for pdf files. We saw how we could efficiently transform pdf to image or vice-versa to use Linux commands. We also looked into ImageMagick to create, edit, compose and convert bitmap images.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • Paul campbell Dec 1, 2020 @ 6:41

    Looks great

  • Pablo AB Jan 25, 2021 @ 6:22

    From curl

    # pdfimages - Utility for extracting images from PDFs.
    # Extract all images from a PDF file and save them as PNGs:
    pdfimages -png path/to/file.pdf filename_prefix

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum