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.
2 Answers 2
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 %%.
2 Comments
%% 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.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.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.