Using Ghostscript to convert JPEG to PDF

https://gitlab.mister-muffin.de/josch/img2pdf

As mentioned by PleaseStand, GhostScript (edit: prior to v9.23) will decode the JPEG data, resulting in generation loss, as well as performance "ten to hundred" times worse than img2pdf.

Edit: Ghostscript 9.23 (2018-03-21) added a "JPEG Passthrough" capability that resolves the generation-loss/performance issue.

ImageMagick (i.e. convert) also decodes and re-encodes the images.


I've been using the same basic command line Henry gave in his answer for quite some time now in a simple Bash script, with a few tweaks.

My full script converts multiple JPEG images to a multipage PDF, using this modified command:

gs \
 -sDEVICE=pdfwrite \
 -o foo.pdf \
  /usr/local/share/ghostscript/9.02/lib/viewjpeg.ps \
 -c "(1st.jpg)  viewJPEG showpage \
     (2nd.jpg)  viewJPEG showpage \
     (3rd.jpg)  viewJPEG showpage \
     (last.jpg) viewJPEG showpage"

It is called like this:

jpegs2pdf.sh output.pdf file1.jpeg [file2.jpeg [file2.jpeg [...]]]

The problem is that this command would use the same (default) page size of Ghostscript (usually Letter or A4 in portrait mode), and each JPEG image will be scaled to fit this pagewidth and/or pageheight, being placed on the lower left corner.

My script makes each PDF page use the same page dimensions as the original JPEG for the page. For auto-discovery of the JPEG's dimensions, I use ImageMagick's identify command:

 identify -format "%[fx:(w)] %[fx:(h)]" some.jpeg

Here is the code of the full script:

#!/bin/bash
#
#############################################################################
#  
#  Shellscript to convert a set of JPEG files to a multipage PDF.
#
#  Requirements: (1) Ghostscript needs to be installed on the local system.
#                (2) ImageMagick needs to be installed on the local system.
#
#  Usage:  jpegs2pdf.sh output.pdf file1.jpeg [file2.jpeg [file2.jpeg [...]]]
#
#  Copyright (c) 2007, <[email protected]>
#                Use, distribute and modify without any restrictions.
#
#  Versions:
#          v1.0.0, Jul 12 2007:  initial version
#          v1.0.1, Jan 07 2011:  set viewJPEG.ps path (self-compiled GS 9.02)
#
#############################################################################
    
outfile=$1
shift

param=""
for i in "$@" ; do
   dimension=$(identify -format "%[fx:(w)] %[fx:(h)]" "${i}")
   param="${param} <</PageSize [${dimension}]>> setpagedevice (${i}) viewJPEG showpage"
done

gs \
  -dNOSAFER \
  -sDEVICE=pdfwrite \
  -dPDFSETTINGS=/prepress \
  -o "$outfile" \
   /usr/local/share/ghostscript/9.02/lib/viewjpeg.ps \
  -c "${param}"

gs \
 -dNOSAFER \
 -sDEVICE=pdfwrite \
 -o foo.pdf \
  /usr/local/share/ghostscript/8.71/lib/viewjpeg.ps \
 -c \(my.jpg\) viewJPEG

reads my.jpg and produces foo.pdf. You will have to find where your installation installed the PostScript program viewjpeg.ps.

Tags:

Ghostscript