Using Ghostscript to convert JPEG to PDF

人走茶凉 提交于 2019-11-27 10:49:45

问题


What are the parameters that I should pass? The Ghostscript version I'm using is 8.71.


回答1:


gs \
 -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.




回答2:


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

As mentioned by PleaseStand, GhostScript will decode the JPEG data, resulting in generation loss, as well as performance "ten to hundred" times worse than img2pdf.

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




回答3:


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 \
  -sDEVICE=pdfwrite \
  -dPDFSETTINGS=/prepress \
  -o "$outfile" \
   /usr/local/share/ghostscript/9.02/lib/viewjpeg.ps \
  -c "${param}"



回答4:


alternatively on some linux distros convert pic1.jpg pic2.jpg out.pdf does the job with mixed results




回答5:


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"



回答6:


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.



来源:https://stackoverflow.com/questions/4283245/using-ghostscript-to-convert-jpeg-to-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!