Manish Manish - 2 years ago 90
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 Source

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}')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download