Is it possible to use EPS file (created with eps2write) in EndPage procedure? - ghostscript

For testing purposes, let's draw some meaningless rectangles:
gswin32c -q -o f.pdf -sDEVICE=pdfwrite -c "<</PageSize[595 842]>>setpagedevice 0 0 595 842 rectfill showpage"
+
gswin32c -q -o f.eps -sDEVICE=eps2write -f f.pdf
And ps.ps file:
<<
/EndPage {
exch pop
2 ne dup {
1 dict begin
/showpage {} def
(f.eps) run
end
} if
}
>> setpagedevice
And then
gswin32c -q -o out.pdf -sDEVICE=pdfwrite -f ps.ps -f f.pdf
produces an error:
%%[ Error handled by opdfread.ps : GPL Ghostscript 9.15: Unrecoverable
error, exit code 1 Unrecoverable error: typecheck in if Operand stack:
typecheck ebuf false false --nostringval--
On the other hand, if I create EPS with another tool, e.g. xpdf's pdftops:
pdftops -eps f.pdf f.eps
... then EPS can be placed as e.g. watermark or logo with above command:
gswin32c -q -o out.pdf -sDEVICE=pdfwrite -f ps.ps -f f.pdf
So the question is, is it possible to use Ghostscript's eps2write for the purpose, maybe I'm missing something.
I tried to bracket (f.eps) run with calls to BeginEPSF and EndEPSF as defined in Adobe's EPSF Format Specification, but it didn't help. And after decoding prologue which eps2write creates (which, itself, gives same error if run from EndPage), it looks to me that it violates section on Illegal and Restricted Operators of mentioned Specification.
Edit:
I think there's an issue with the Immediately Evaluated Names if code is run from EndPage. In prologue, which eps2write creates, there's a fragment not very far from the beginning:
//SetPageSize{
//RotatePages//FitPages or//CenterPages or{
mark(/RotatePages, /FitPages and CenterPages are not allowed with /SetPageSize)//error exec
}if
}
{
//FitPages//CenterPages and{
mark(CenterPages is not allowed with /FitPages)//error exec
}if
}
ifelse
If I frame it like this:
SetPageSize ==
//SetPageSize ==
{
//SetPageSize{
//RotatePages//FitPages or//CenterPages or{
mark(/RotatePages, /FitPages and CenterPages are not allowed with /SetPageSize)//error exec
}if
}
{
//FitPages//CenterPages and{
mark(CenterPages is not allowed with /FitPages)//error exec
}if
}
ifelse
} stopped { (***\n) print } if
And modify ps.ps slightly:
<<
/EndPage {
exch pop
2 ne dup {
1 dict begin
/showpage {} def
(prologue.ps) run
end
} if
}
>> setpagedevice
Then this command:
gswin32c -q -o out.pdf -sDEVICE=pdfwrite -f ps.ps -f f.pdf
...gives this output:
false
/SetPageSize
***
%%[ Error handled by opdfread.ps : GPL Ghostscript 9.14: Unrecoverable error, exit code 1
I.e. it fails in above fragment (and for obvious reason, I think) and then fails somewhere else further down within prologue.ps.

OK so I did something similar to your expereience. I started with a simple PostScript file (testeps.ps):
%!
0 1 0 setrgbcolor
0 0 100 100 rectfill
showpage
I then ran that through Ghostscript using the eps2write device:
./gs -sDEVICE=eps2write -sOutputFile=out.eps -dCompressPages=false testeps.ps
I then constructed another test file (test.ps):
%!
<<
/EndPage {
exch pop
2 ne dup {
1 dict begin
/showpage {} def
(/temp/out.eps) run
end
} if
}
>> setpagedevice
1 0 0 setrgbcolor
0 100 100 100 rectfill
showpage
and ran that through GS:
./gs test.ps
The file ran to completion, and contained appropriately coloured rectangles at the correct positions on the page.
Its possible this is something which has been fixed (you don't say what version of Ghostscript you are using). The next release (9.16) is due very shortly, or you can build iot yourself from source, I'd suggest you try it when its available.

Related

Change back DPI settings in a bash script

I would like to run a program that does not properly support my desired resolution+DPI settings.
Also I want to change my default GTK theme to a lighter one.
What I currently have:
#!/bin/bash
xfconf-query -c xsettings -p /Xft/DPI -s 0
GTK_THEME=/usr/share/themes/Adwaita/gtk-2.0/gtkrc /home/unknown/scripts/ch_resolution.py --output DP-0 --resolution 2560x1440 beersmith3
This sets my DPI settings to 0, changes the gtk-theme, runs a python script that changes my resolution and runs the program, and on program exit changes it back. This is working properly.
Now I want to change back my DPI settings to 136 on program exit
xfconf-query -c xsettings -p /Xft/DPI -s 136
My guess is I need to use a while loop but have no idea how to do it.
ch_resolution.py
#!/usr/bin/env python3
import argparse
import re
import subprocess
import sys
parser = argparse.ArgumentParser()
parser.add_argument('--output', required=True)
parser.add_argument('--resolution', required=True)
parser.add_argument('APP')
args = parser.parse_args()
device_context = '' # track what device's modes we are looking at
modes = [] # keep track of all the devices and modes discovered
current_modes = [] # remember the user's current settings
# Run xrandr and ask it what devices and modes are supported
xrandrinfo = subprocess.Popen('xrandr -q', shell=True, stdout=subprocess.PIPE)
output = xrandrinfo.communicate()[0].decode().split('\n')
for line in output:
# luckily the various data from xrandr are separated by whitespace...
foo = line.split()
# Check to see if the second word in the line indicates a new context
# -- if so, keep track of the context of the device we're seeing
if len(foo) >= 2: # throw out any weirdly formatted lines
if foo[1] == 'disconnected':
# we have a new context, but it should be ignored
device_context = ''
if foo[1] == 'connected':
# we have a new context that we want to test
device_context = foo[0]
elif device_context != '': # we've previously seen a 'connected' dev
# mode names seem to always be of the format [horiz]x[vert]
# (there can be non-mode information inside of a device context!)
if foo[0].find('x') != -1:
modes.append((device_context, foo[0]))
# we also want to remember what the current mode is, which xrandr
# marks with a '*' character, so we can set things back the way
# we found them at the end:
if line.find('*') != -1:
current_modes.append((device_context, foo[0]))
for mode in modes:
if args.output == mode[0] and args.resolution == mode[1]:
cmd = 'xrandr --output ' + mode[0] + ' --mode ' + mode[1]
subprocess.call(cmd, shell=True)
break
else:
print('Unable to set mode ' + args.resolution + ' for output ' + args.output)
sys.exit(1)
subprocess.call(args.APP, shell=True)
# Put things back the way we found them
for mode in current_modes:
cmd = 'xrandr --output ' + mode[0] + ' --mode ' + mode[1]
subprocess.call(cmd, shell=True)
edit:
Thanks #AndreLDM for pointing out that I do not need a separate python script to change the resolution, I don't know why I didn't think of that.
I changed it so I don't need the python script and it is working properly now. If I can improve this script please tell me!
#!/bin/bash
xrandr --output DP-0 --mode 2560x1440
xfconf-query -c xsettings -p /Xft/DPI -s 0
GTK_THEME=/usr/share/themes/Adwaita/gtk-2.0/gtkrc beersmith3
if [ $? == 0 ]
then
xrandr --output DP-0 --mode 3840x2160
xfconf-query -c xsettings -p /Xft/DPI -s 136
exit 0
else
xrandr --output DP-0 --mode 3840x2160
xfconf-query -c xsettings -p /Xft/DPI -s 136
exit 1
fi

How to Add Page Numbers Opposite The Bound Edge with Postscript / Ghostscript

I have a 2,000+ page PDF that I need to add page numbers to. The PDF is already setup for binding. Meaning there is extra margin on right and lefthand pages to accommodate the binding. I found the following solution from Brian M. Hunt, but it always places the page number in the same position. I know I can modify the, "sub 20 sub 20" value to change the position, but how do I make the position conditional so that righthand pages have the number near the right edge and lefthand pages have the number near the left edge?
gs \
-dBATCH -dNOPAUSE \
-sDEVICE=pdfwrite -dPDFSETTINGS=/prepress \
-sOutputFile=/path/to/merged.pdf \
-c 'globaldict /MyPageCount 1 put << /EndPage {exch pop 0 eq dup {/Helvetica 12 selectfont MyPageCount =string cvs dup stringwidth pop currentpagedevice /PageSize get 0 get exch sub 20 sub 20 moveto show globaldict /MyPageCount MyPageCount 1 add put } if } bind >> setpagedevice'
-f input1.pdf -f input2.pdf
You already have MyPageCount, so you know the page number. Using that you can determine if the page count is odd or even. Then you just select different positional logic.
Something like :
MyPageCount 2 mod 1 eq
{
%%insert odd numbered page positioning
}{
%%insert even numbered page positioning
} ifelse
[add simplified program]
gs -q -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -dPDFX -sOutputFile=step_4.pdf \
-c 'globaldict /MyPageCount 1 put
<<
/EndPage {
exch pop 0 eq dup {
/Helvetica 12 selectfont MyPageCount 256 string cvs dup stringwidth pop currentpagedevice
/PageSize get 0 get exch sub
MyPageCount 2 mod 1 eq {
60
}
{
505
} ifelse
sub 36 moveto
show globaldict
/MyPageCount MyPageCount 1 add put
} if
} bind
>>
setpagedevice' \
-f "test_in.pdf"
Note that =string is unique to Ghostscript (ie won't work on any other PostScript interpreter) and pulls a pre-defined 256 character string from userdict. There are dangers to using this that I doubt you realise, because its a subtlety of PostScript. I have instead replaced it with the portable (ie standard PostScript) construction 256 string which has the same effect for your purposes, but creates a scratch string that can't be reused unexpectedly, unlike =string.
You have set -dPDFX (to create a PDF/X-3 compliant file) but you haven't supplied a PDF/X definition file, this means that the resulting file may not be PDF/X compliant. The process is documented here
Thanks to KenS's suggestion I was able to get the page numbers added with the following script.
gs -q -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -dPDFX -sOutputFile=step_4.pdf \
-c 'globaldict /MyPageCount 1 put
<<
/EndPage {
MyPageCount 2 mod 1 eq
{
exch pop 0 eq dup {
/Helvetica 12 selectfont MyPageCount =string cvs dup stringwidth pop currentpagedevice
/PageSize get 0 get exch sub 60 sub 36 moveto show globaldict
/MyPageCount MyPageCount 1 add put
} if
}{
exch pop 0 eq dup {
/Helvetica 12 selectfont MyPageCount =string cvs dup stringwidth pop currentpagedevice
/PageSize get 0 get exch sub 505 sub 36 moveto show globaldict
/MyPageCount MyPageCount 1 add put
} if
} ifelse
} bind
>>
setpagedevice' \
-f "test_in.pdf"

GhostScript auto pagenumbering

I want to export one certain page from a pdf document to an image and automatically fill the page number in the file name. When I run the following code:
gs \
-sDEVICE=jpeg \
-o outfile-%03.jpeg \
-dFirstPage=12 \
-dLastPage=12 \
wo.pdf
I get: outfile-001.jpeg instead of outfile-012.jpeg.
I've wrote a bash script for the job:
function extract_nth_page(){
printf -v j "outfile-%05g.png" $1
echo $j
gs -q -dNOPAUSE -sDEVICE=png16m -r600 -dFirstPage=$1 -dLastPage=$1 -sOutputFile=$j $2 -c quit
return 0
}
# Extracts page number 42 from myFile.pdf to outfile-00042.png
extract_nth_page 42 myFile.pdf

Neural-Style output flag

I'm messing around with jcjohnson/neural-style. I have it up and running just fine. However, I cannot for the life of me make the -output_image or -init flags work. My gut says it's some very simple misunderstanding on my part.
-output_image is driving me especially nuts because it should just be a string to use as the filename, the script even appends the file formate in function that takes -output_image as an argument. I've tried single, double, and no quotes, as well as changing the order of the flags. My gut says it's something trivially simple because nobody is complaining about this on-line anywhere,but I'm a little in over my head because I'm not especially proficient with either bash, torch, or lua.
I'm using this simple script I hacked together that works just fine if I leave off both of the troublesome flags
#!/bin/bash
echo "#### Starting Job B ####"
th neural_style.lua \
-gpu -1 \
-print_iter 1 \
-save_iter 10 \
-num_iterations 3000 \
-style_image a_style.jpg \
-content_image a_content.jpg \
-style_scale 0.15 \
-style_weight 25 \
-content_weight 1e3 \
-output_image a \
-init a_460.png \
The above throws "Invalid argument: "
if it helps, in the script 'neural_style.lua'
the flags are defined as
cmd:option('-init', 'random', 'random|image')
...
cmd:option('-output_image', 'out.png')
and used as
local function maybe_save(t)
local should_save = params.save_iter > 0 and t % params.save_iter == 0
should_save = should_save or t == params.num_iterations
if should_save then
local disp = deprocess(img:double())
disp = image.minmax{tensor=disp, min=0, max=1}
local filename = build_filename(params.output_image, t)
if t == params.num_iterations then
filename = params.output_image -- ## LOOK HERE ##
end
-- Maybe perform postprocessing for color-independent style transfer
if params.original_colors == 1 then
disp = original_colors(content_image, disp)
end
image.save(filename, disp) -- ## LOOK HERE ##
end
end
as well as
if params.init == 'random' then
img = torch.randn(content_image:size()):float():mul(0.001)
elseif params.init == 'image' then
img = content_image_caffe:clone():float()
else
error('Invalid init type') -- ## NOT THE ERROR THROWN #
end
if params.gpu >= 0 then

ghostscript unexpectedly returned exit value 1

Calling ghostscript from a Perl script I get this error:
"/usr/local/bin/gs" unexpectedly returned exit value 1
In $! this string can be found: "Inappropriate ioctl for device" - I am not sure, if this is related to the gs error.
Searching for the documentation of ghostscript, I found this page:
http://www.ghostscript.com/doc/current/API.htm#return_codes
Unfortunatley 1 is not listed there.
Any idea what's going wrong here?
EDIT: After changing the code a bit, I realized that the file given to gs was a JPEG and not a PDF. That's probably the reason but does not explain the exit-code of 1.
For the reference here is the Perl-code which throws the above error message:
my ($stdout, $stderr, $exit) = capture {
my $cmd = '/usr/local/bin/gs';
my #args = (qw( -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=- )) ;
push #args, $tmp_fn;
use IPC::System::Simple qw(system);
system($cmd, #args);
};
die "ERROR compressing pdf: $stderr" if $stderr;

Resources