The simplest approach:
pdftotext input.pdf - | wc -w
pdftotext is in the poppler-utils package (sudo apt install poppler-utils or sudo yum install poppler-utils). The - sends output to stdout instead of a file.
Other options
pdfminer:
pdf2txt.py input.pdf | wc -w
Install with pip install pdfminer.six.
Python with PyPDF2:
import PyPDF2
with open("input.pdf", "rb") as f:
reader = PyPDF2.PdfReader(f)
total = sum(len(page.extract_text().split()) for page in reader.pages)
print(total)
Note: PyPDF2’s PdfFileReader and getPage() are deprecated in newer versions — use PdfReader and index into reader.pages instead.
Accuracy
None of these methods are perfect. PDFs don’t store text in a linear format, so extraction can produce garbled output for complex layouts, columns, or scanned documents. For a rough word count they’re fine. For anything precise, you’ll need to inspect the extracted text manually.