What are the parameters that I should pass? The Ghostscript version I'm using is 8.71.
-
1@RobertFleming This does not seem to be true in 2020. I just read `viewjpeg.ps` and it seems to do a simple byte-copy. It explicitly states "Ghostscript with IJG JPEG v6 or later will decode progressive JPEG, but only if you (…) enable that feature.". – Hermann Apr 25 '20 at 17:17
6 Answers
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.

- 1,337
- 17
- 13
-
1Thanks for this. Using any of the other methods takes forever with any nontrivial number of images. – emacsomancer Dec 21 '14 at 19:29
-
1GhostScript 9.50 does not seems to be suffering from this `generation loss` anymore. – malat Aug 03 '20 at 12:18
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
.
-
3
-
https://superuser.com/questions/1530841/failing-to-convert-jpg-to-pdf-with-ghostscript – malat Aug 03 '20 at 12:05
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, <pipitas@gmail.com>
# 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}"

- 8,943
- 3
- 20
- 31

- 86,724
- 23
- 248
- 345
-
hay i used ur script without `identify -format` and pdf was generated but .jpg images are aligned at botton how can i bring them to top of the pdf page. and `identify -format "%[fx:(w)] %[fx:(h)]" some.jpeg ` returns null so i ca't use it. – run Feb 22 '12 at 08:56
-
@run: which OS are you on to run `identify`? What does `identify -version` tell you? You know you could also use `identify some.jpeg` manually which will return a line indicating the WxH dimensions of the JPEG in pixels, which you can then use to to insert to the commandline manually.... – Kurt Pfeifle Feb 22 '12 at 17:40
-
unix. and i achiecev it by `dimension=$(identify -format "%w %h" "${x}")` but is there any way other than using `width and height` to set image on top of the page instead of bottom. i would be delighted if i get any links to get good documentation on this. – run Feb 23 '12 at 06:03
-
-
@run: No, you can only get the image to the lower left corner in one go. But, **after** you placed the image on the PDF page successfully, you could employ a second step `gs` commandline to shift the image. That's why I wrote *'...script makes each PDF page use the same page dimensions as the original JPEG for the page'*. – Kurt Pfeifle Feb 23 '12 at 09:58
-
yes i do agree with you pipitas , and my version is `Version: ImageMagick 6.2.8 03/31/08 Q16 file:/usr/share/ImageMagick-6.2.8/doc/index.html` also is it possible to make same page `potrait` – run Feb 23 '12 at 12:03
-
Was very useful 9 years later. The same as a bash one-liner: `for i in *.jpg ; do echo "<> setpagedevice ($i) viewJPEG showpage" ; done | gs -sDEVICE=pdfwrite -o foo.pdf viewjpeg.ps -c -`. – Hermann Apr 25 '20 at 17:13
-
@KurtPfeifle viewjpeg.ps works perfectly, but didn't you try viewgif.ps by any chance? I have run into strange behavior of viewgif.ps from standard GS 9.54.0. The problem is that `gswin64c.exe -dNOSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite "-sOutputFile=output.pdf" -f "viewgif.ps" -c "(Hydrangeas.gif) <> setpagedevice viewGIF showpage"` creates a PDF with a picture underscaled to ~10% of the page area. Hydrangeas.gif here is just a standard Hydrangeas.jpg from Windows 7 re-saved as gif in MS Paint. Can I fit that gif to a PDF page size? Regards. – bimjhi Jun 02 '21 at 22:43
-
@bimjhi: Why should I try *viewgif.ps* if the question is about converting JPEG files? – Kurt Pfeifle Jun 03 '21 at 17:22
-
@KurtPfeifle My bad. In the meantime, I was given help from a Ghostscript developer who pointed out that viewgif.ps needs `-r72` parameter to be passed to gswin64c.exe. This code works: `gswin64c -r72 -sDEVICE=pdfwrite -o out.pdf --permit-file-read="C:/Temp/" lib/viewgif.ps -c "(C:/Temp/Hydrangeas.gif) << /PageSize 2 index viewGIFgetsize 2 array astore >> setpagedevice viewGIF showpage"` – bimjhi Jun 03 '21 at 17:48
alternatively on some linux distros convert pic1.jpg pic2.jpg out.pdf
does the job with mixed results

- 77
- 1
- 2
-
Worked for me well. Warning: very memory-hungry, merging 80 JPEG files took 30 mins of swapping here. – liori Oct 16 '11 at 20:53
-
1That's because ImageMagick fully decodes and re-encodes the JPEG data. Not only is this slow and memory-intensive, but also results in [generation loss](http://en.wikipedia.org/wiki/Generation_loss). See img2pdf elsewhere on this page. – Robert Fleming May 14 '13 at 20:52
-
Try GraphicsMagick. It is a fork of ImageMagick that often runs significantly more quickly. It lacks some of IM's features, but is mostly compatible. – E Brown May 10 '18 at 19:59
I have Ghostscript version 9.10, so the command with the below line DIDN'T WORKED FOR ME
/usr/local/share/ghostscript/9.02/lib/viewjpeg.ps
so I modifid the command and edited the line and insted used this, IT WORKED FOR ME
viewjpeg.ps
So the NEW MODIFIED COMMAND IS below:
gs \
-sDEVICE=pdfwrite \
-o foo.pdf \
viewjpeg.ps \
-c "(1st.jpg) viewJPEG showpage \
(2nd.jpg) viewJPEG showpage \
(3rd.jpg) viewJPEG showpage \
(last.jpg) viewJPEG showpage"

- 9,775
- 7
- 56
- 69
GhostScript is a PostScript interpreter, so it does not directly support a JPEG input, only a JPEG output. That said, it does support JPEG decompression (except for progressive JPEG).
There's a PostScript program and accompanying shell script that you can use to take advantage of this called jpeg2eps. To get a PDF, most systems have the script pstopdf
available for use, which runs each of the input files (you would use the output of the jpeg2eps script) through GhostScript.

- 31,641
- 6
- 68
- 95