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
2
$ apt update
$ apt install -y ghostscript

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
2
3
4
5
6
7
8
9
10
11
12
$ gs \
-q \
-dNOSAFER \
-sPAPERSIZE=letter \
-dNOPAUSE \
-dBATCH \
-sDEVICE=pdfwrite \
-sOutputFile=output.pdf \
/usr/share/ghostscript/9.50/lib/viewjpeg.ps \
-c \
\(IMG-0001.JPG\) viewJPEG showpage \
\(IMG-0002.JPG\) viewJPEG showpage

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

1
2
3
4
5
6
7
8
9
10
11
12
$ gs \
-q \
-dNOSAFER \
-sPAPERSIZE=letter \
-dNOPAUSE \
-dBATCH \
-sDEVICE=pdfwrite \
-sOutputFile=output.pdf \
viewjpeg.ps \
-c \
\(IMG-0001.JPG\) viewJPEG showpage \
\(IMG-0002.JPG\) viewJPEG showpage

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.

jpeg2pdf.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# MIT License

# Copyright (c) 2021 Lei Mao <https://leimao.github.io/>

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# Usage
# bash jpeg2pdf.sh jpeg_dir output.pdf

dir=$1
output_file=$2

args=""

for file in `find ${dir} -name "*.JPG" -o -name "*.JPEG" -o -name "*.jpg" -o -name "*.jpeg" -type f`;
do
args="${args} (${file}) viewJPEG showpage"
done

gs \
-q \
-dNOSAFER \
-sPAPERSIZE=letter \
-dNOPAUSE \
-dBATCH \
-sDEVICE=pdfwrite \
-sOutputFile=${output_file} \
viewjpeg.ps \
-c \
${args}

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
2
3
4
5
6
7
8
9
$ gs \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/ebook \
-dNOPAUSE \
-dQUIET \
-dBATCH \
-sOutputFile=output.pdf \
input.pdf

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/

Author

Lei Mao

Posted on

02-21-2022

Updated on

02-21-2022

Licensed under


Comments