How to convert PDF files to JPG format on Linux

Last updated on August 28, 2020 by Dan Nanni

While PDF (short for "Portable Document Format") is a widely used document format supported by a variety of applications on multiple platforms, you may want to convert PDF files to JPG format for several reasons. For example, you may want to embed a PDF file into PowerPoint or OpenOffice Impress presentations, in which case JPG/JPEG images will be easier to work with. Converting a PDF file into JPG also makes it unnecessary to load a separate plugin or external application for web browsers to render it. If you are looking to convert PDF files to JPG/JPEG format on Linux, the following guide will help.

Method One: ImageMagick

The easiest one-shot solution to convert a PDF file to JPG format is using ImageMagick. ImageMagic comes with a versatile command-line tool called convert which can handle the conversion easily. Assuming that you have installed ImageMagic on your Linux system, run the following command to convert input.pdf to output.jpg.

$ convert input.pdf output.jpg

Note that if input.pdf is a multi-page PDF file, the above command will produce as many JPG files as the number of pages in the PDF file (e.g., output-0.jpg, output-1.jpg, output-2.jpg, etc).

Advanced Usage of convert Command

Other usages of convert command are illustrated as follows.

To convert a PDF file to a JPG image with JPEG compression level set to 50:

$ convert -quality 50 input.pdf output.jpg

The -quality option in the command specifies image compression level. For JPG images, an allowed compression level ranges from 1 (lowest quality and highest compression) to 100 (best quality and lowest compression).

To convert a PDF file to a JPG image with width 500px:

$ convert -density 500 input.pdf output.jpg

To convert the first page of a PDF file into JPG image:

$ convert input.pdf[0] output.jpg

To convert multiple PDF files to JPG format in batch mode:

$ for i in `ls *.pdf`; do convert "$i" "$i".jpg; done

If you think that converting PDF files with convert command takes too much memory to be completed properly, you can check the available resource on your system, and limit the resource usage of convert command as follows.

$ identify -list resource
File         Area       Memory          Map         Disk    Thread         Time
------------------------------------------------------
 768      1.054GB     753.9MiB    1.9632GiB  18.446744EB         1    unlimited
$ convert -limit area 64MiB -limit memory 64MiB input.pdf output.jpg 

Method Two: Ghostscript

When convert performs conversion, it actually relies on Ghostscript to interpret PDF files. In fact, you can invoke Ghostscript directly to convert PDF files to JPG format. The advantage of doing so is that you don't have to install ImageMagick, but just use Ghostscript.

Install Ghostscript on Linux

On Debian-based system:

$ sudo apt-get install ghostscript

On Red Hat-based system:

$ sudo yum install ghostscript

To convert a PDF file to JPG format with Ghostscript:

$ gs -dNOPAUSE -sDEVICE=jpeg -dFirstPage=1 -dLastPage=5 -sOutputFile=output%d.jpg -dJPEGQ=100 -r500 -q intput.pdf -c quit 

The above command will generate a series of output files (image1.jpg, image2.jpg,..., image5.jpg) for the first 5 pages of input.pdf. The JPEG compression level is set to 100, and output image resolution is set to 500px (width).

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal (Credit Card) or Bitcoin (1M161JGAkz3oaHNvTiPFjNYkeABox8rb4g).

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean