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 how to use Ghostscript to convert JPEG images to PDF.
Install Ghostscript
Ghostscript can be installed via apt
in Ubuntu.
1 | $ apt update |
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.
1 | $ gs \ |
Usually the file path of viewjpeg.ps
does not have to be explicitly specified.
1 | $ gs \ |
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.
1 | # MIT License |
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.
1 | $ bash jpeg2pdf.sh jpeg_dir output.pdf |
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.
1 | $ gs \ |
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/