# Convert JPEG Image to PDF Using Ghostscript

## Introduction

In many scenarios, we will have to convert a sequence of JPEG images to a PDF. Those JPEG images could often be the ones from a scanner. However, there are not too many applications that can do this.

Ghostscript is a suite of software based on an interpreter for Adobe Systems’ PostScript and Portable Document Format (PDF) page description languages. In this blog post, I would like to discuss about how to use Ghostscript to convert JPEG images to PDF.

## Install Ghostscript

Ghostscript can be installed via apt in Ubuntu.

## Image To PDF

### Multiple Images to PDF

To convert a series of JPEG images to PDF, we could run the following command in the terminal.

Usually the file path of viewjpeg.ps does not have to be explicitly specified.

### Image Directory to PDF

To convert a directory of JPEG images with extensions of .JPEG, .JPG, .jpeg, and .jpg, please use the following bash script. The same bash script is also available on Gist.

As long as the JPEG image file names are ordered, a PDF containing all the ordered JPEG images can be generated using the following command.

## Shrink PDF Size

Because the PDF was compiled from images, its size could be very large. However, many web applications prevented us from uploading very large PDF files. To reduce the size of the PDF, we could run the following command.

Summary of -dPDFSETTINGS in terms of resolution and image quality:

• -dPDFSETTINGS=/screen lower quality, smaller size. (72 dpi)
• -dPDFSETTINGS=/ebook for better quality, but slightly larger pdfs. (150 dpi)
• -dPDFSETTINGS=/prepress output similar to Acrobat Distiller “Prepress Optimized” setting (300 dpi)
• -dPDFSETTINGS=/printer selects output similar to the Acrobat Distiller “Print Optimized” setting (300 dpi)
• -dPDFSETTINGS=/default selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file

## Miscellaneous

There are other ways to convert JPEG images to PDF, such as Img2Pdf and ImageMagick. Img2Pdf often works but ImageMagick often did not work.

## References

Convert JPEG Image to PDF Using Ghostscript

https://leimao.github.io/blog/JPEG-Image-to-PDF-Ghostscript/

Lei Mao

02-21-2022

02-21-2022