PDF to text from the command line: pdftotext and curl
Updated · Tested with Poppler 25.03, OCRmyPDF 17.14, curl
In short
Install the Poppler tools and run pdftotext file.pdf - to print a PDF's text. Add -layout to keep columns and tables in place, or -raw for the order stored in the file. pdftotext cannot read scans; for those use OCRmyPDF, which adds the missing text first.
For one file or a folder of them, you do not need to write a program. pdftotext is free, fast, and one package away on Linux and macOS. The examples use company-extract.pdf.
Install it
pdftotext is part of the Poppler tools.
# Debian or Ubuntu
sudo apt-get install poppler-utils
# macOS
brew install popplerThree modes, three results
pdftotext company-extract.pdf -The - sends the text to the screen instead of a file. By default, a label and its value come out as separate paragraphs:
COMPANY EXTRACT
Company name
Harbor Lane Logistics Ltd
Company number-layout keeps the page's shape, with spaces where the page has gaps:
pdftotext -layout company-extract.pdf -COMPANY EXTRACT
Company name Harbor Lane Logistics Ltd
Company number 51-482913-6-raw returns the text in the order the file stores it:
pdftotext -raw company-extract.pdf -COMPANY EXTRACT
Company name Harbor Lane Logistics Ltd
Company number 51-482913-6
Status Active| Mode | Use it for |
|---|---|
| Default | Running text, where you want paragraphs |
-layout | Tables, forms, anything where a row is one record |
-raw | A second try when the other two put things in the wrong order |
On a two-column page, -layout puts the columns side by side, the default mode mixed the paragraphs of the two columns, and -raw read the left column and then the right. Which is correct depends on the file, so try all three on a sample before you process a folder.
Useful options
pdftotext -f 2 -l 5 report.pdf - # pages 2 to 5 only
pdftotext -layout report.pdf report.txt # write to a file
pdftotext -upw sands2026 protected.pdf - # a password-protected file
pdftotext -bbox report.pdf words.html # every word with its position-bbox writes one line per word, with its box in points from the top left of the page:
<word xMin="68.362205" yMin="129.027651" xMax="117.596020" yMax="144.008986">Company</word>
<word xMin="120.455893" yMin="129.027651" xMax="149.912586" yMax="144.008986">name</word>
<word xMin="204.425197" yMin="129.027651" xMax="241.251563" yMax="144.008986">Harbor</word>Before you extract, pdfinfo tells you what you have:
pdfinfo company-extract.pdfProducer: WeasyPrint 70.0
Pages: 1
Encrypted: no
Page size: 595.276 x 841.89 pts (A4)A whole folder
mkdir -p text
for f in extracts/*.pdf; do
pdftotext -layout "$f" "text/$(basename "$f" .pdf).txt"
doneKeep the quotes around "$f": file names with spaces break the loop without them.
What pdftotext cannot do
- Scans. For company-scan.pdf it prints an empty page, with no error. A scan is a picture; it needs OCR.
- Fonts without a Unicode map. For such a file it prints gibberish. See why you can't copy text from some PDFs.
- Wrong passwords. It stops with
Command Line Error: Incorrect password. - Other formats. Word and Excel files need other tools.
An empty result with exit code 0 is the dangerous one, because a script will carry on as if it had worked. Check the size of each output file.
OCR from the command line
OCRmyPDF reads a scanned PDF and can write the text to a file next to it:
pip install ocrmypdf
ocrmypdf --sidecar scan.txt company-scan.pdf searchable.pdf
head -3 scan.txtCOMPANY EXTRACT
Company name Harbor Lane Logistics LtdIt needs Tesseract installed as well, and one language pack for each language you read.
The same with curl and an API
If you would rather not install Poppler, one curl command sends a PDF or HTML file to 100sands and gets the text back as JSON. jq prints the text itself:
curl -s https://api.100sands.com/v1/text \
-H "Authorization: Bearer $SANDS_API_KEY" \
-F [email protected] | jq -r .textThe JSON has this shape:
{
"pages": 1,
"text": "COMPANY EXTRACT\nCompany name\tHarbor Lane Logistics Ltd\nCompany number\t51-482913-6\n..."
}Lines come in reading order, in every language, with a tab between a label and its value and a form feed (\f) between pages. It reads the font without a Unicode map correctly. It does not do OCR, so it is not a replacement for OCRmyPDF: a scan such as company-scan.pdf is refused with status 422 and the code NO_TEXT_IN_FILE, and a password-protected file with 422 and ENCRYPTED_PDF. A file it cannot read comes back as a status code with a reason, not as an empty answer, so for a script add -w "%{http_code}" and check it.
To loop over a folder:
for f in inbox/*.pdf; do
curl -s https://api.100sands.com/v1/text \
-H "Authorization: Bearer $SANDS_API_KEY" \
-F "file=@$f" | jq -r .text > "text/$(basename "$f" .pdf).txt"
doneThe Free plan reads 100 documents a month, and a document is one file, whatever its page count.