ruby rmagick FailedToExecuteCommand `"gs" - ruby

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

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

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.

Count PDF pages from stdin with Ghostscript (PostScript)

Well I found on stackoverflow how to count pages of PDF file using Ghostscript by executing the following command on a shell
gs -q -dNODISPLAY -c "($PATH_TO_PDF) (r) file runpdfbegin pdfpagecount = quit"')
I would like to get the pdf from stdin.
I'll played a little bit around, but with no success.
My approach was:
gs -q -dNODISPLAY - -c "(%stdin) (r) file runpdfbegin pdfpagecount = quit"')
I get no output.
Any hints or suggestions?
You cannot work with PDF files from stdin, as the PDF format makes it more or less essential to be able to have random access to all parts of the file.
In the cases where Ghostscript reads a PDF file from stdin it first copies it to a local file then works on that, so it isn't working from stdin anyway.
In short, this can't be done.
This works:
gs -q -dNODISPLAY -c "($PATH_TO_PDF) (r) file runpdfbegin pdfpagecount = quit";
I think the problem with your attempt to use
gs -q -dNODISPLAY -c "($PATH_TO_PDF) (r) file runpdfbegin pdfpagecount = quit"')
was the unmatched closing bracket after QUIT
It can be done.
String ars = "-q -dNODISPLAY -dNOPAUSE -sDEVICE=tiffg3 -r150.4 -o" + outputImagesPath + "%d.tiff -sPAPERSIZE=a4 " + inputPDFFile + " -c quit";
Process proc = new Process();
proc.StartInfo.FileName = ghostScriptPath;
proc.StartInfo.Arguments = ars;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
//Raise Your Complete Job Event Here And User Directory.GetFiles("..").Count

Resources