How can I make a program overlay text ON TOP of a PostScript file? - image

How can I make a Ghostscript overlay text on top of a PostScript file?
I found part of a solution here:
How can I make a program overlay text on a postscript file?
which suggests to do:
gs -o figLabel.pdf -sDEVICE=pdfwrite \
-c "/Helvetica findfont 15 scalefont setfont 50 200 moveto (text) show" \
-f fig.eps`
However in that case the text is behind the image.
Is there an option in Ghostscript to force the text to be in front of the image?

PostScript uses an opaque painting model, so each new thing drawn obscures anything previously drawn. In your command line, the text is drawn and then the EPS file is drawn. It sounds like you want the reverse behavior. So do
gs -o figLabel.pdf -sDEVICE=pdfwrite \
-f fig.eps \
-c "/Helvetica findfont 15 scalefont setfont 50 200 moveto (text) show showpage"
This should work IF the EPS file obeys the rule that it should not call showpage itself. Otherwise, we'll need to add workarounds for that.
If the EPS file calls showpage (even though it ought not to do this), we need to redefine the name /showpage so it does nothing, and save the old definition to call at the end.
gs -o figLabel.pdf -sDEVICE=pdfwrite \
-c "/realshowpage /showpage load def /showpage {} def" \
-f fig.eps \
-c "/Helvetica findfont 15 scalefont setfont 50 200 moveto (text) show realshowpage"

Related

wkhtmltopdf how to set caching of html footer and header?

Testing wkhtmltopdf for generating huge pdf reports.
Finaly pdf is about 600 pages. After ~500 pages got the same error
Error:failed to load file .......... with network status code 201 and http status code 0 - Error opening kn_footer.html: Too many open files
The value of fs.file-max was about 1.6M. After error I was increased it to 2097152 but it does not help.
Trying to add file cacheing but it's not working. Command line looks like:
wkhtmltopdf --cache-dir /tmp/ --allow /path/to/my/dir/ --margin-top 20 --load-error-handling ignore --orientation landscape --page-size A4 page kn_utf.html --footer-html kn_footer.html --header-html kn_header.html --footer-spacing 1 kn.pdf
Is there any way to say wkhtmltopdf get header and footer at once or close that files after each iteration?
I too was unable to get any sort of caching to work on a 532 page document. I did achieve some success by converting my footer.html file into wkhtmltopdf footer text. You might also consider splitting it into two documents, run wkhtmltodpf twice, and then use pdfunite to merge the two temporary PDFs into the final document. If you're interested in the text footer approach, here's the set of options I used. Don't forget to set the bottom margin (-B option), otherwise the text footer will get dropped off the page.
wkhtmltopdf \
-O landscape \
-s letter \
-L 20 \
-R 20 \
-T 20 \
-B 20 \
--exclude-from-outline \
--image-dpi 300 \
--header-html $BASE_DIR/header.html \
--footer-line \
--footer-spacing 10 \
--footer-left "$FOOTER_LEFT" \
--footer-center "$FOOTER_CENTER" \
--footer-right "$FOOTER_RIGHT" \
--footer-font-size 10 \
--cache-dir $BASE_DIR/cache \
--user-style-sheet $BASE_DIR/some.css \
$TARGET_DIR/[im]*.html \
$TARGET_DIR/final.pdf

How should I get UN-rotated JPEG image file from ghostscript?

I am running with crazy issue in ghostscript 9.15.I have two commands for converting PDF to JPEG images,which has difference in height and width only.
Command 1st
-q -dUseCropBox -dBATCH -dDOINTERPOLATE -dMaxBitmap=300000000 -dAlignToPixels=0 -dNOPAUSE -dSAFER -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dPDFFitPage -dPrinted=false -r144 -g200x200 -sDEVICE=jpeg -dJPEGQ=100 -sOutputFile="output.jpg" "input.pdf" -c quit
Command 2nd
-q -dUseCropBox -dBATCH -dDOINTERPOLATE -dMaxBitmap=300000000 -dAlignToPixels=0 -dNOPAUSE -dSAFER -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dPDFFitPage -dPrinted=false -r144 -g200x199 -sDEVICE=jpeg -dJPEGQ=100 -sOutputFile="output.jpg" "input.pdf" -c quit
As you can see, command has difference in -g parameter only.
In first command I have used -g200x200 and in second command -g200x199.
First command gives rotated JPEG while second command gives UN-rotated JPEG file.Is there any utility inside ghostscript which differentiate my command from height and width perspective.
How should I get UN-rotated JPEG image file by using same height and width(-g200x200) while converting PDF to JPEG image file ?
The main answer is 'don't use PDFFitPage'. That feature automatically rotates the output to fit 'best' on the declared media. If that isn't an option then you will have to do some PostScript programming.
You can fnd the code which does the resizing (and rotation) in ghostpdl/gs/Resource/Init/pdf_main.ps, the function /pdf_PDF2PS_matrix

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.

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