How to Add Page Numbers Opposite The Bound Edge with Postscript / Ghostscript - 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"

Related

Ghostscript change trimbox

I would like to apply different boxs with ghostscript command line on a file.
I test that for create a new file :
gswin32c -dPDFX -dBATCH -dNOPAUSE -sColorConversionStrategy=CMYK -sDEVICE=pdfwrite -sOutputFile=file_out.pdf -c "[/BleedBox [200 200 200 200] /PAGES pdfmark << /PDFXTrimBoxToMediaBoxOffset [10 10 10 10] >> setdistillerparams" -f
This code it's ok and create a file with good trimbox/bleedbox.
Now I need to change trimbox on a existing file and i test with this command :
gswin32c -dPDFX -dBATCH -dNOPAUSE -sColorConversionStrategy=CMYK -sDEVICE=pdfwrite -sOutputFile=file_out_with_trimbox.pdf -c "[/BleedBox [200 200 200 200] /PAGES pdfmark << /PDFXTrimBoxToMediaBoxOffset [10 10 10 10] >> setdistillerparams" -f file_in_without_trimbox.pdf
The new file "file_out_with_trimbox.pdf" content my image pdf but the trimbox/bleedbox doesnt' change.
Can you help me ?
thanks

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

How to bind a widget to a Virtuell Event

I want so register multiple widgets to one virtual event. This is the idea of the code. But I do not find out, how I can send down from the toplevel "." to any widgets, where the virtual event is registered.
1 #!/bin/sh
2 # \
3 exec tclsh "$0" "$#"
4 package require Tk
5 proc send { } {
6 puts "send event"
7 event generate ***HERE IS MY PROBLEM *** <<TEST>>
8 }
9 label .l1 -text label1
10 label .l2 -text label2
11 button .b1 -text event -command send
12 bind .l1 <<TEST>> {puts "here 1"}
13 bind .l2 <<TEST>> {puts "here 2"}
14 grid .l1
15 grid .l2
16 grid .b1
Found this solution:
7 event generate . <<TEST>>
...
14 set tags [bindtags .]
15 bindtags . [list {*}$tags .l1 .l2]
Will be there a house keeping necessary after "destroy .l1" ?

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

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.

Resources