Manish Manish - 7 months ago 12
Bash Question

How to write shell script for finding number of pages in PDF?

I am generating a PDF dynamically. How can I check the number of pages in the PDF using a shell script?

Answer

Without any extra package:

foo=$(strings < pdffile.pdf | sed -n 's|.*/Count -\{0,1\}\([0-9]\{1,\}\).*|\1|p' | sort -rn | head -n 1)

Using pdfinfo:

foo=$(pdfinfo pdffile.pdf | grep Pages | awk '{print $2}')

Using pdftk:

foo=$(pdftk pdffile.pdf dump_data|grep NumberOfPages| awk '{print $2}')