1

I found a pointer to a great book on PostScript: Thinking in Postscript.

In Chapter 14 - Using Files and Input/Output Techniques on page 171 there is an example operation:

(%stdin) (r) file

When I run that in the following command:

gswin64c - -c "(%stdin) (r) file" < input.pdf

I get the error output:

Error: /undefinedfilename in --file--
Operand stack:
 (stdin) (r)
Execution stack:
 %interp_exit .runexec2 --nostringval-- --nostringval-- 
 --nostringval-- 2 %stopped_push --nostringval--
 --nostringval-- --nostringval-- false 1 %stopped_push 
 .runexec2 --nostringval-- --nostringval-- --nost
 ringval-- 2 %stopped_push --nostringval--
Dictionary stack:
 --dict:1182/1684(ro)(G)-- --dict:1/20(G)-- --dict:77/200(L)--
Current allocation mode is local
Last OS error: No such file or directory
GPL Ghostscript 9.14: Unrecoverable error, exit code 1

What am I doing wrong?

EDIT: Kudos to joojaa! I am running in NT Batch script. The %% suggestion got me over this hurdle.

asked Mar 27, 2014 at 15:43

2 Answers 2

3

Loose the first - that would direct the file to the command stream of ghostscript. So your command should look as follows:

gswin64c -c "(%stdin) (r) file" < input.pdf

To test that this works do something minimal make a text file with some text for example test.txt:

it works
line 2

and try:

gswin64c -q -c "(%stdin) (r) file 20 string read line pop pstack" < test.txt

should produce:

(it works)
GS<1>

Now if you run this inside a batch file

Then the % sign needs to be doubled as follows:

gswin64c -q -c "(%%stdin) (r) file" < input.pdf

Because the batch interpreter reserves the % sign for its own processing and the escape sequence is %%.

answered Mar 28, 2014 at 4:32
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect!! doubling the %% removed the error all by itself. Note: I get Error: /typecheck in --read-- running your example. I'm rendering an in-memory PDF binary file to bitmap and now have the added requirement to read meta-data (keywords, author, etc). I'm hoping to do this with no file I/O and didn't want to invoke the interpreter twice, or worse yet use two different libraries to get the job done. Needless to say, once I started executing postscript commands directly, things started to unravel.
Not sure if this matters to you, but I was able to circumvent all problems I was having with stdin by wrapping the document inside a set of PDF commands (basically embedded the whole file in a command string). There's an amazing bug thread here about it: bugs.ghostscript.com/show_bug.cgi?id=691739 Brilliant! Even lets me read meta data and display in what amounts to a single pass I think.
2

From the error message, it looks like you may have omitted the % from the special-filename (%stdin). Edit: this guess is wrong. See joojaa's answer.

Another issue you may run into is that often postscript interpreters are run in "SAFER" mode which disables file operations, and may signal either of these errors: invalidfileaccess or undefinedfilename.

Another issue is, why are you redirecting input from a pdf?

Another issue is that ghostscript processes command-line options one-at-a-time, so the - directing it to read from stdin (which comes from the pdf, since the file-redirection happens in shell before ghostscript begins executing) happens before -c "(%stdin) (r) file". So it executes the pdf, and then tries to open stdin. But of course there's no data left in stdin after it processes the whole pdf file. So you should also try putting the - option after the -c "whatever" option.


Finally, the file operator merely opens the file. It's like calling fopen in C. It doesn't actually read anything. For that you need to use one of the file-reading operators, like read, readstring, or readline.

answered Mar 28, 2014 at 4:29

4 Comments

LOL you beat me to it.
It looks like our two answers give complementary information. I don't see this as a problem. :)
I'm passing a PDF because that's what I'm working with. Without the -c option ghostscript seems to render PDF just fine. I know file merely opens a handle, but I picked on the smallest failing unit. Believe me I have many other failures to pick on !
As to before or after - I had tried both but perhaps neither will seem to work if there's no way to reset %stdin to 0 position.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.