Search code examples
ghostscripteps

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.


Solution

  • 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.

    full page A4 PNG output

    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.