Ghostscript: How can I move/scale eps content on target image? - transformation

I want to create a png file from an eps file with ghostscript. But I want to put it on a bigger image and control exactly where and how big (this means, I want to scale and move it).
I assume, that one should add some postscript commands in a "-c" argument to ghostscript.
I tried arguments like -c " 0.8 0.8 scale " and -c " 1 1 moveto ".
But in this case no png file was created, without other error message.
My question: what is the proper way to do this?
Example command used by me:
gswin32c.exe -dPARANOIDSAFER -r288 -dDEVICEHEIGHTPOINTS=195 -dDEVICEWIDTHPOINTS=256 -c " 1 1 moveto " -sDEVICE=png16m -o "testout.png" "input.EPS"

You need to read the Adobe EPSF specification
especially Section 2.6 "Graphics state". You will need to use scale as you suggest, and moveto. Note that 1 1 moveto moves the current point by 1/72 of an inch, so likely you won't be able to see any difference. Although you have used a -c switch to introduce PostScript, you haven't followed it with a -f to revert back to command line processing.
Try:
gswin32c.exe -sDEVICE=png16m -r288 -dDEVICEHEIGHTPOINTS=195 -dDEVICEWIDTHPOINTS=256 -sOutputFile="testout.png" -c "72 72 moveto" -f "input.EPS"
That should move the rendered image up and right by one inch each which you should be able to see. Using -sOutputFile rather than -o is in case you get something back on stdout. -o implies -dBATCH and -dNOPAUSE.

Related

xmgrace: changing color of the XY graph in batch execution

I am using gracebat integrated into my bash script to make simple XY graph from 2D numerical data
gracebat input.xvg -hdevice PNG -fixed 800 600 -world 0 0.02 610 0.5 -printfile output.png -hardcopy
This simple execution without GUI produces the XY graph totally in black color!
would it be possible to quickly change to color of the line (I guess should be s0 color in the batch file) without using batch file for gracebat: e.g. via some option of gracebat or alternatively via editing of the input.xvg (via sed or any shell utility) specifying color or the line:
# s0 line color 2
Alternatively, if the grace has not good possibilities for graph customization, I would be grateful for simple example of the gnuplot batch execution.
What does xmgrace -help say? Please check the corresponding manuals and homepages and do a search on SO and the web before asking such questions. I don't now xmgrace and its documentation.
But a gnuplot command line would be using the command line option -e: (check help command line options).
gnuplot -e "set term pngcairo size 800,600; set output 'myFile.png'; plot 'myFile.dat' u 1:2 w l lc 'red' "

Is there a way to position new text within an existing pdf keeping such that it's editable after?

I want to create a pdf with pictures and text formatted a certain way — they all have to be arbitrarily positioned. I want to create such pdf on the server with terminal commands.
As I searched for the solution, I didn't find one. So I compiled some ideas to solve the problem; however, I still looking for a more direct one. I used the postscript compiler ENSCRIPT as a filter and then created a pdf by using the PS2PDF tool. Then I used the PDFJAM tool to position text in arbitrary position within the existing pdf. The following is the code I use to produce such a document. Although even this solution would help people who have the same problem, I welcome your suggestions of more efficient ways to handle this.
#!/bin/bash
# the following creates PDF with text
# options:
# -B — no header
# -f — font family and size
# --columns — make text grouped in columns
# -o - — to pass output
echo "Quick brown fox jumps over the lazy dog" | \
enscript -B -f "Times-Roman12" --columns=5 -o - | \
ps2pdf - pdf_with_text.pdf
# the following tool creates pdf_with_text-pdfjam.pdf from the pdf_with_text.pdf moving it in the exact position of the page
# options:
# -q — quiet mode
# --scale — scaling pdf_with_text.pdf if you need that
# --paper — resulting paper type
# --landscape — resulting paper orientation
# --offset — the most important part, set the offset of the top left corner of the source document in the resulting document. 0,0 point is in the middle (weird!)
pdfjam -q --scale 0.20 pdf_with_text.pdf --paper letter \
--landscape --offset "2cm -3cm"
# then we combine a new document and the existing one:
pdftk ./existing.pdf stamp pdf_with_text-pdfjam.pdf output new_combined.pdf
=========================
I got the combined pdf:
new_combined.pdf
with the text, I echoed in the first string, with the applied offset from the middle of the page.

PDF to JPEG conversion using Ghostscript

I'm using Ghostscript to convert my PDF files to JPEGs with Ghostscript which works great.
For my output images I'm using %03d in the file name, so the file names come out 001, 002 ... and so on according to the page numbers.
But i want in some case the numbers to start from an higher number.
For example I process a file with two pages so the output images are page001.jpg, page002.jpg
Now I want to process another PDF and instead of replacing those files, I want to create page003.jpg, page004.jpg.
How can this be done?
This is my full command line I'm using now:
'C:\gs\gs9.14\bin \gswin64c -dNOPAUSE -sDEVICE=png16m \
-sOutputFile=page-%03d.jpg -r100x100 -q' . $pdf_file. '-c quit'
Here is a workaround trick, that you could use:
gswin64c.exe ^
-sDEVICE=png16m ^
-sOutputFile=page-%03d.jpg ^
-r100x100 ^
-c "showpage showpage" ^
-f filename.pdf
The -c "showpage showpage" inserts two empty pages into the output. The output files will be named
page-001.jpg + page-002.jpg + page-003.jpg + page-004.jpg
So the first two are white-only JPEGs and should be deleted afterwards.
You can extend this command with any number of empty pages you want.
Update
Of course, if you know in advance that you want to convert several different PDF files to images where you want the counting for a new PDF to continue exactly from where the last PDF ended, you could do this:
gswin64c.exe ^
-sDEVICE=jpeg ^
-sOutputFile=page-%03d.jpg ^
-r100x100 ^
-f file1.pdf ^
-f file2.pdf ^
-f file3.pdf ^
-f [...]
BTW, your original command requests .jpg file suffixes, while the Ghostscript device is png16m. This doesn't match. Initially I blindly copied your command, but now I've corrected it.
You cannot do that with the standard version of Ghostscript, the output file numbers are given as the emitted page number (so if you had a 10 page file, with /NumCOpies 2, you would get files numbered 0 to 19).
Of course, you can process the two files on the same command line, I think that will give you the second file with page numbers beginning where the first set left off.
Otherwise you will have to modify the source code of the Ghostscript device.

How to change Ghostscript output file (in printer spooler)

I am using Ghostscript to print a PDF by command line arguments. But it shows the printed document's name as Ghostscript output in printer spooler. I want to change it to a custom name (as letter's name).
Please refer to the documentation in http://www.ghostscript.com/doc/9.06/Devices.htm#Win
In particular there is an example in section 10.2 with explanation of the parameters that can be specified in section 10.3. Just changing the document name can be done by:
mark /UserSettings <</DocumentName (MyDocName)>> (mswinpr2) finddevice putdeviceprops setdevice
This can be put into a file as in the example, or can be in a string on the command line following a -c option. If you use -c instead of putting the above PostScript in a setup file, put it in as the last option before -f and the input-filename.
Note: You should not specify the -sDEVICE=mswinpr2 on the command line -- the setdevice takes care of this. I tested this on my laptop with the command line:
gswin32c \
-dNOPAUSE -dBATCH \
-c "mark /UserSettings <</DocumentName (MyDocName)>> (mswinpr2) finddevice putdeviceprops setdevice" \
-f examples/colorcir.ps
If the sPAPERSIZE switch is ignored, perhaps it is possible to force the page size
by using the appropriate switches.
The
How to Use Ghostscript
manual says in the section
Choosing paper size:
Otherwise you can set the page size using the pair of switches
-dDEVICEWIDTHPOINTS=w -dDEVICEHEIGHTPOINTS=h
Where w be the desired paper width and h be the desired paper height in points (units of 1/72 of an inch).
The section
Appendix: Paper sizes known to Ghostscript,
the A4 size is defined as
w=595 and h=842.
For this purpose you must use a setup.ps file as in this link under section: 6.2 Supported options (device properties)
This provides a partial solution to the issue, which is perhaps acceptable for your needs.
The options to control the output size are (markdown inserts spaces, but there should be none):
-dDEVICEWIDTHPOINTS=w, -dDEVICEHEIGHTPOINTS=h
(w,h=595,842 / 612,792 for A4 / letter, respectively), Ref.
-sPAPERSIZE=size, Ref.
-gnumber1xnumber2
(number1,number2=5953,8419 / 6120,7920 for A4 / letter, respectively), Ref.
-dDEVICEWIDTH=number1, -dDEVICEHEIGHT=number2
(number1,number2=5953,8419 / 6120,7920 for A4 / letter, respectively), Ref.
I have tested the four of them, printing a pdf file to pdf, trying to convert (letter -> A4) and (A4 -> letter).
The command line I used is
gswin32c -dPDF -dBATCH -dNOPAUSE -dFIXEDMEDIA <size setting flags> -sDEVICE=pdfwrite -sOutputFile=output.pdf input.pdf
A4 -> letter always worked well.
letter -> A4 only worked for the width, so output.pdf was always 210mm x 279.4mm.
Using, e.g., -dDEVICEWIDTHPOINTS alone for letter -> A4 produced the same result.
Adding flag -dPDFFitPage was also useful for partially adapting size (in cases I want to do that).
My conclusion is that there is some bug in Ghostscript specifically related to letter -> A4 conversion. Whether this could be handled via gs_setpd.ps (e.g., this; this would still be a bug), or a crafty combination of (-dUseBleedBox, -dUseTrimBox, -dUseArtBox, -dUseCropBox) I don't know.

Ghostscript PDF to PNG: output is always 595x842 (A4)

I try to convert PDF to PNG, but ouput image is always A4, however the source PDF is very huge. Here are my commands:
-dNOPAUSE ^
-dBATCH ^
-dSAFER ^
-sDEVICE=png16m ^
-dFirstPage=1 ^
-sOutputFile="D:\PDF.png" ^
"D:\PDF.pdf" ^
-sPAPERSIZE=a1
I tried several options (-r, -g, -sDEFAULTPAPERSIZE), but none worked.
How can I force the output image dimensions?
P.S: my PDF file
Your linked-to PDF file has only 1 page. That means your commandline parameter -dFirstPage=1 doesn't have any influence.
Also, your -sPAPERSIZE=a1 parameter should not be last (it doesn't have any influence here -- so Ghostscript takes the default size from the pagesize of the input PDF, which is A4). Instead it should appear somewhere before the "D:\PDF.pdf" (which must be last).
It looks like you want a PNG with the size of A1, and your OS is Windows (guessing from the partial commandline you provided)?
Try this instead (it adds -dPDFFitPage=true to the commandline and puts the arguments into a correct order, while also shortening it a bit using the -o trick):
gswin32c.exe ^
-o "D:\PDF.png ^
-sDEVICE=png16m ^
-sPAPERSIZE=a1 ^
-dPDFFitPage=true ^
"D:\PDF.pdf"
This should give you a PNG with the size of 1684x2384 pixel at 72dpi (which is the builtin default for all Ghostscript image output, used if no other resolution is specified). For different combinations of resolution and pagesize add your variation of -rXXX and -gNNNxMMM (instead of -sPAPERSIZE=a1) but by all means keep the -dPDFFitPage=true....
You can also keep the -sPAPERSIZE=a1 and add -r100 or -r36 or -r200 if you want a different resolution only. Be aware that increasing resolution may not improve the image quality compared to the default output of 72dpi. That depends on the resolution of the images that were embedded in the PDF page. But it surely increases the file size...
function pdf2png-mutool() {
#: "mutool draw [options] file [pages]"
# pages: Comma separated list of page numbers and ranges (for example: 1,5,10-15,20-N), where
# the character N denotes the last page. If no pages are specified, then all pages
# will be included.
local i="$1"
local out="${pdf2png_o:-${i:r}_%03d.png}"
[[ "$out" == *.png ]] || out+='.png'
command mutool draw -o "$out" -F png "$i" "${#[2,-1]}"
#: '`-r 300` to set dpi'
}

Resources