Ghostscript change trimbox - ghostscript

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

Related

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

ruby rmagick FailedToExecuteCommand `"gs"

I run in loop converting of pdf scans (base64 data) to jpg images with rmagick with this code
Magick::Image.read_inline(data) do
self.quality = 100
self.density = '150'
self.interlace = Magick::NoInterlace
end.each_with_index do |img, i|
img.write(rpvs_dir+"temp-#{i}.jpg")
end
it looks it works fine but after 10-15 documents it starts throwing this error
FailedToExecuteCommand `"gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE
-dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4
"-r150x150" "-sOutputFile=/tmp/magick-17139yIssgZl8Dg7O%d"
"-f/tmp/magick-17139VEtM2IeDiPfF" "-f/tmp/magick-17139ANIid4R9Yoov"'
(-1) # error/delegate.c/ExternalDelegateCommand/461
then some documents are fine and it starts again.
is it related to memory or what might be the problem? from error message I cannot find out what might be wrong

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;

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