Count PDF pages from stdin with Ghostscript (PostScript)

ghostscript, postscript, shell

Solution

You cannot work with PDF files from stdin, as the PDF format makes it more or less essential to be able to have random access to all parts of the file.

In the cases where Ghostscript reads a PDF file from stdin it first copies it to a local file then works on that, so it isn't working from stdin anyway.

In short, this can't be done.

Problem

Well I found on stackoverflow how to count pages of PDF file using Ghostscript by executing the following command on a shell ``` gs -q -dNODISPLAY -c "($PATH_TO_PDF) (r) file runpdfbegin pdfpagecount = quit"') ``` I would like to get the pdf from stdin. I'll played a little bit around, but with no success. My approach was: ``` gs -q -dNODISPLAY - -c "(%stdin) (r) file runpdfbegin pdfpagecount = quit"') ``` I get no output. Any hints or suggestions?

Original source

Related problems