I am currently using lpr to print from the command line. I am also using a python backend.
Is there anyway I can see how many pages are going to print/have printed for a specific file?
Short of passing the job through a RIP (Raster Image Processor) there's no easy way to do it. In the Linux and Unix world print jobs are passed around as PostScript files. PostScript actually is a fully fledged Turing complete programming language. Which means that the Halting Problem applies to them.
So the only option is to pass it through a PostScript RIP (raster image processor) and see how many pages come out of it; don't forget to add some timeout.
Ghostscript is such a RIP and it offers a nullpage output device, which is mostly good for counting pages. Use the following command line
timeout 120s gs -dNOPAUSE -dBATCH -dSAFER -sDEVICE=nullpage <file> \
| egrep '^Page' \
| wc -l
The first command uses the timeout
command to invoke Ghostscript in a way that it doesn't wait for user input, processes a whole file in one, disables known unsafe features and produces no raster output. This pipes into grep looking for lines in the diagnostic output of gs
starting with Page
; one such line is produced for each page. Finally this is piped through wc
to count the total number of lines that match. If gs
doesn't finish after 120 seconds it is aborted.
Be aware that this is still susceptible to malicious PostScript files (even with -dSAFER
).