2

I'm trying to automate some post-processing on scanned PDF documents. One tool I found was ghostscript that would compress the scanned images nicely. However when I try to call gs from inside my python script it fails miserably:

Error: /undefinedfilename in (/tmp/step-02-1.pdf)
Operand stack:

Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push
Dictionary stack:
   --dict:1164/1684(ro)(G)--   --dict:0/20(G)--   --dict:77/200(L)--
Current allocation mode is local
Last OS error: 2
GPL Ghostscript 9.05: Unrecoverable error, exit code 1

The command line I'm trying to run is

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.5 -dPDFSETTINGS=/screen\
   -dSAFER -dNOPAUSE -dQUIET -dBATCH -sOutputFile=/tmp/step-03-1.pdf\
   /tmp/step-02-1.pdf

Since this is a python script, I tried both os.system and os.popen to execute it, but to no avail. If I run the above command line from the shell it all works. Any idea where my error could be?

1 Answer 1

2

Well Ghostscript can't find the file, have you tried something simple like 'ls /tmp/step-02-1.pdf' instead of invoking GS ?

Does that work ?

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, the file is definitely there. What really concerns me is that when run from the shell, the command runs correctly without any problems.
I don't doubt the file is there, but possibly the Python environment is running in a way which masks it, which is why I suggest executing something simple like 'ls'
D'oh try running without -dSAFER
Tried running without -dSAFER and the file is being created by python in a previous instruction, but I'll try for completeness.
Ah, you were right, I was post-processing the files asynchronously, and the output from the previous step was not finished when I called ghostscript.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.