Shrinking PDFs with Ghostscript on the command line

I needed to shrink a PDF last week — something I don’t do often enough to remember the flags. Ghostscript is the tool, and it’s already on most Linux machines or a quick apt install ghostscript away. The command is: gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen \ -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf The bit that actually matters is -dPDFSETTINGS=/screen. That’s the quality dial. /screen gives you the smallest file — fine for emailing or uploading, rubbish if you need to print anything. ...

How to count words in a PDF from the Linux command line

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. ...