Bash – How to determine the page count from a postscript file (generated by Opera)

bashoperapostscriptprinting

I don't know the postscript language.

I have a duplex printing emulation system written in bash. It prints the odd pages first and then the even pages. It needs to know if there's an odd page count so it can eject the last odd page that doesn't have a corresponding even side. It also uses page counts for reporting purposes.

I didn't know how to do this correctly, so I wrote code that looks at the end and, if necessary, the beginning of the postscript file searching for "%%Pages:" which is followed by a page count. This works on almost everything except files printed by the Opera browser.

Can anyone suggest another way to get this information?

Postscript files tend to be rather large with a lot of non-human-readable content, so I haven't yet spent a lot of time pouring over the ones that come out of Opera.

TIA

The current code is at:

http://sourceforge.net/projects/duplexpr/

function ps_page_ct

Best Answer

The following Ghostscript command will reliably count the pages in your PostScript file -- but it can be rather slow, because it requires the file to be completely interpreted (run), as @afrazier already stated in a comment:

gs \
 -o /dev/null \
 -sDEVICE=bbox \
  input.ps 2>&1 \
| grep HiResBoundingBox \
| wc -l
Related Question