Using Ghostscript to convert JPEG to PDF

前端 未结 6 872
轻奢々
轻奢々 2020-12-13 02:50

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

6条回答
  •  佛祖请我去吃肉
    2020-12-13 03:01

    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, 
    #                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} <> setpagedevice (${i}) viewJPEG showpage"
    done
    
    gs \
      -dNOSAFER
      -sDEVICE=pdfwrite \
      -dPDFSETTINGS=/prepress \
      -o "$outfile" \
       /usr/local/share/ghostscript/9.02/lib/viewjpeg.ps \
      -c "${param}"
    

提交回复
热议问题