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.

Shell
# Debian or Ubuntu
sudo apt-get install poppler-utils
# macOS
brew install poppler

Three modes, three results

Shell
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:

Output
COMPANY EXTRACT
Company name

Harbor Lane Logistics Ltd

Company number

-layout keeps the page's shape, with spaces where the page has gaps:

Shell
pdftotext -layout company-extract.pdf -
Output
COMPANY EXTRACT

Company name                     Harbor Lane Logistics Ltd

Company number                   51-482913-6

-raw returns the text in the order the file stores it:

Shell
pdftotext -raw company-extract.pdf -
Output
COMPANY EXTRACT
Company name Harbor Lane Logistics Ltd
Company number 51-482913-6
Status Active
ModeUse it for
DefaultRunning text, where you want paragraphs
-layoutTables, forms, anything where a row is one record
-rawA 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

Shell
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:

Output
<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:

Shell
pdfinfo company-extract.pdf
Output
Producer:        WeasyPrint 70.0
Pages:           1
Encrypted:       no
Page size:       595.276 x 841.89 pts (A4)

A whole folder

Shell
mkdir -p text
for f in extracts/*.pdf; do
  pdftotext -layout "$f" "text/$(basename "$f" .pdf).txt"
done

Keep 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:

Shell
pip install ocrmypdf
ocrmypdf --sidecar scan.txt company-scan.pdf searchable.pdf
head -3 scan.txt
Output
COMPANY EXTRACT

Company name Harbor Lane Logistics Ltd

It 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:

Shell
curl -s https://api.100sands.com/v1/text \
  -H "Authorization: Bearer $SANDS_API_KEY" \
  -F [email protected] | jq -r .text

The JSON has this shape:

JSON
{
  "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:

Shell
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"
done

The Free plan reads 100 documents a month, and a document is one file, whatever its page count.

Questions

How do I convert a PDF to text on Linux or macOS?

Install poppler-utils (Linux) or poppler (macOS, with Homebrew), then run pdftotext file.pdf output.txt. Use a dash in place of the output name to print to the screen.

What does pdftotext -layout do?

It keeps the text where it is on the page, using spaces to hold columns apart. Use it for tables and forms. Without it, pdftotext returns paragraphs and may put a label and its value on separate lines.

Why does pdftotext return nothing?

The PDF is a scan: it holds pictures of pages and no text. pdftotext prints an empty page and reports no error. Run OCR on the file first, for example with OCRmyPDF.

How do I convert a whole folder of PDFs to text?

Loop over the files in the shell: for f in *.pdf; do pdftotext -layout "$f" "${f%.pdf}.txt"; done. Keep the quotes so that file names with spaces work.

Try it on your own documents

A free plan every month, no credit card. See the PDF to text API.

Start free