Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top