Using Ghostscript to convert JPEG to PDF

前端 未结 6 778
轻奢々
轻奢々 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, <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}"
    
    0 讨论(0)
  • 2020-12-13 03:01

    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"
    
    0 讨论(0)
  • 2020-12-13 03:02
    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.

    0 讨论(0)
  • 2020-12-13 03:02

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

    0 讨论(0)
  • 2020-12-13 03:10

    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.

    0 讨论(0)
  • 2020-12-13 03:18

    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.

    0 讨论(0)
提交回复
热议问题