Using Ghostscript in server mode to convert PDFs to PNGs - ghostscript

while i am able to convert a specific page of a PDF to a PNG like so:
gs \
-dSAFER \
-dBATCH \
-dNOPAUSE \
-sDEVICE=png16m \
-dGraphicsAlphaBits=4 \
-sOutputFile=gymnastics-20.png \
-dFirstPage=20 \
-dLastPage=20 \
gymnastics.pdf
i am wondering if i can somehow use ghostscript's JOBSERVER mode to process several conversions without having to incur the cost of starting up ghostscript each time.
from: http://pages.cs.wisc.edu/~ghost/doc/svn/Use.htm
-dJOBSERVER
Define \004 (^D) to start a new encapsulated job used for compatibility with Adobe PS Interpreters that ordinarily run under a job server. The -dNOOUTERSAVE switch is ignored if -dJOBSERVER is specified since job servers always execute the input PostScript under a save level, although the exitserver operator can be used to escape from the encapsulated job and execute as if the -dNOOUTERSAVE was specified.
This also requires that the input be from stdin, otherwise an error will result (Error: /invalidrestore in --restore--).
Example usage is:
gs ... -dJOBSERVER - < inputfile.ps
-or-
cat inputfile.ps | gs ... -dJOBSERVER -
Note: The ^D does not result in an end-of-file action on stdin as it may on some PostScript printers that rely on TBCP (Tagged Binary Communication Protocol) to cause an out-of-band ^D to signal EOF in a stream input data. This means that direct file actions on stdin such as flushfile and closefile will affect processing of data beyond the ^D in the stream.
the idea is to run ghostscript in-process. the script would receive a request for a particular page of a pdf and would use ghostscript to generate the specified image. i'd rather not start up a new ghostscript process every time.

So why can't you simply use a command like this:
gs \
-sDEVICE=png16m \
-dGraphicsAlphaBits=4 \
-o pngimages_%03d.png \
\
-dFirstPage=20 \
-dLastPage=20 \
gymnastics.pdf
\
-dFirstPage=3 \
-dLastPage=3 \
sports.pdf
\
-dFirstPage=33 \
-dLastPage=33 \
athletics.pdf
\
-dFirstPage=4 \
-dLastPage=4 \
lazyness.pdf
This will generate several PNG images from different PDFs in a single go.

Related

How to batch convert PostScript files to PNGs via folder action with use of ImageMagick

I'm trying to assign a folder action to the a folder with PS files that would automatically convert the PS files droppped in the folder to the PNG files. My shell script looks like follow:
for img in "$#"; do
filename = ${img%.*}
convert "$img" -background white -flatten "$filename.png"
done
and settings for my Automator folder action are provided in the screenshot below
I'm experiencing two problems:
When I drop *.ps files onto the folder the Automator action starts but does not produce files. I'm guessing the the problem is concerned with not passing the file name to the Shell Script but I'm not able to find solution to this.
When I attempt to execute the conversion directly from the terminal with use of the command: convert b.ps b.png the produced image is cut, like in the screenshot below
.
I would like to fix so the Automator action:
- Takes all the files that I decide to specify via the Filter Finder Items option
- Converts them to high resolution, quality PNGs respecting the original PS file sizes (without cutting them or providing extra margins)
(You should spell out clearly in your question, that you are working on Mac OSX.)
You may have encountered a bug in ImageMagick when it comes to converting PS files (see also this discussion in the IM forum about it). Try to add -verbose to your convert command to see what exactly goes on.
Fact is, that ImageMagick cannot by itself consume PostScript (or PDF) input. It has to employ a delegate to do that for you -- and that delegate usually is Ghostscript.
It's a better approach for your task if you made your shell script differentiate between different input types: if you get PS or PDF, let Ghostscript do the job directly:
gs \
-o output.png \
-sDEVICE=pngalpha \
-dAlignToPixels=0 \
-dGridFitTT=2 \
-dTextAlphaBits=4 \
-dGraphicsAlphaBits=4 \
-r72x72 \
input.ps-or-pdf
Should you need further post-processing of the generated output.png (such as making the background white instead of transparent you could pipe the output to an ImageMagick convert-command now....
Update
Since it has been asked in a comment: if you want to use the same filename for output, but replace the .ps suffix by a .png suffix, use this Bashism:
inputfilename=../somedir/somefile.ps
gs \
-o ${inputfilename/.ps/.png} \
-sDEVICE=pngalpha \
-dAlignToPixels=0 \
-dGridFitTT=2 \
-dTextAlphaBits=4 \
-dGraphicsAlphaBits=4 \
-r72x72 \
${inputfilename}
or this one
-o $(dirname ${inputfilename})/$(basename ${inputfilename}).png
Both will allow you to keep the original directory (in case your 'inputfilename' includes an absolute or relative path). The first one is not as flexible with the input file's suffix: it works only for .ps -- for PDF you'd get a .pdf.png suffix...
Update 2
First determine which is the real BoundingBox covered by the original PostScript. This is different from the declared BoundingBox, that may (or may not!) be stated in a %%BoundingBox line of the PostScript code. Ghostscript's -sDEVICE=bbox will do that for you:
gs -q -o - -sDEVICE=bbox a.ps
%%BoundingBox: 102 118 866 698
%%HiResBoundingBox: 102.257997 118.502434 865.278747 697.013979
Now you can use this info to determine how many pixels horizontally and how many pixels vertically you want the PNG output file to be sized. I'll pick 940 pixels wide and 760 pixels high (to allow for some margin around the output. Use -g940x760 with Ghostscript to set this as the page size:
inputfilename=a.ps
gs \
-o ${inputfilename/.ps/.png} \
-sDEVICE=pngalpha \
-dAlignToPixels=0 \
-dGridFitTT=2 \
-dTextAlphaBits=4 \
-dGraphicsAlphaBits=4 \
-g940x760 \
${inputfilename}
The output is here:

Render a Blank Page with Ghostscript

How can I use ghostscript to create a blank page? I would like to do this when merging multiple PDFs together-- something like:
`gs -dNOPAUSE -o /path/to/output input1.pdf <blank-page-here> input2.pdf
To spell out more explicitely what KenS suggested:
gs \
-o new.pdf \
-sDEVICE=pdfwrite \
-f input1.pdf \
-c showpage \
-f input2.pdf \
-c showpage \
-f input3.pdf \
-c showpage
will insert an additional blank page into the new.pdf after the data of each input{1,2,3}.pdf has been processed.
Just send some PostScript, the 'showpage' operator terminates a page if there's nothing on it, it will be blank.
You can either stick that in a file or use the -c -f switches.
Please note that the pdfwrite device does not merge files. It interprets the content of the input to create marking operations which are fed to the device. The device then takes action on those operations, for rendering devices it renders to a bitmap, in the case of pdfwrite it reassembles them into a PDF file.
So the output from your command line is not a 'merge' of the input files, its a brand new file whose only relationship with the input files is that the marks made on the page are the same.

Ghostscript: Convert PDF into CMYK EPS

is it possible to convert a PDF into a CMYK EPS using ghostscript?
The following command works fine - but the result is RGB I guess:
gswin32c.exe -sDEVICE=epswrite -o test.eps test.pdf
This command:
gswin32c.exe \
-sDEVICE=epswrite \
-sProcessColorModel=DeviceCMYK \
-sColorConversionStrategy=CMYK \
-o test.eps \
test.pdf
gives me the following error message
Unrecoverable error: rangecheck in .putdeviceprops
Thanks,
Ronald
Currently the answer is basically no. There are various tricks which work more or less, but nothing simple and reliable.
We will be reworking the colour code in pdfwrite/ps2write and colour conversion will be addressed at that time.
pipitas I had seen this post, but myOpenID was down again....

How to transform an high def PDF to low def using command line tools?

I've a unix server (mac osx in fact) which transform actually PS files to PDF files. It does this through ps2pdf, with those parameters:
ps2pdf14 \
-dPDFSETTINGS=/prepress \
-dEPSCrop \
-dColorImageResolution=72 \
-dColorConversionStrategy=/LeaveColorUnchanged \
INPUT_FILE \
OUTPUT_FILE
But now I've to adapt this script to have a PDF file as input instead as PS.
So I guess that ps2pdf will not work anymore, and I need something which can reduce the quality of the pdf.
Do you know a tool like this?
The ps2pdf14 script just runs the ps2pdfwr script with -dCompatibilityLevel=1.4, which in turn uses gs with various parameters. You can examine that script to see the options.
You could run gs directly, putting in the various options added by the scripts and your own -d options (which are passed directly to gs). I.e. try:
gs \
-sDEVICE=pdfwrite \
-dPDFSETTINGS=/prepress \
-dEPSCrop \
-dColorImageResolution=72 \
-dColorConversionStrategy=/LeaveColorUnchanged \
-q \
-dNOPAUSE \
-dBATCH \
-sOutputFile=OUTPUT_FILE \
INPUT_FILE
Your command should works with PDFs: Ghostscript (backend for ps2pdf) accept PDF as input file. I just tested ps2pdf from Ghostscript 9.04 and it works

Using Ghostscript to convert JPEG to PDF

What are the parameters that I should pass? The Ghostscript version I'm using is 8.71.
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.
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.
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}"
alternatively on some linux distros convert pic1.jpg pic2.jpg out.pdf does the job with mixed results
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"
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.

Resources