4 Free Ways to Convert a PDF to Text File on Linux

Updated

convert pdf

Use pdftotext, and pass -layout unless you have a reason not to:

sudo apt install poppler-utils      # Debian/Ubuntu
sudo dnf install poppler-utils      # Fedora/RHEL

pdftotext -layout report.pdf report.txt

pdftk input.pdf cat output output.txt does not convert a PDF to text. cat is pdftk’s page-assembly operation and it writes a PDF, whatever you name the output file. And catdoc cannot convert PDFs; catdoc reads Microsoft Word .doc files and has never handled PDF.

Proof that pdftk does not extract text

Run pdftk input.pdf cat output output.txt against a real three-page PDF and inspect the result:

$ pdftk report.pdf cat output output.txt
$ file output.txt
output.txt: PDF document, version 1.3, 3 pages

A PDF with a .txt extension. pdftk’s operation list has no text operation in it at all — cat, shuffle, burst, rotate, generate_fdf, fill_form, background, stamp, dump_data*, update_info, attach_files, unpack_files. dump_data dumps metadata and bookmarks, not page content. Use pdftk for splitting and merging, not for text.

Why -layout matters more than the install command

Default pdftotext reads in content-stream order, which destroys anything laid out in columns. Same file, two invocations:

$ pdftotext report.pdf -
QUARTERLY REPORT — PAGE ONE
Region
North
South
East

Q1

$ pdftotext -layout report.pdf -
QUARTERLY REPORT — PAGE ONE

Region        Q1          Q2     Q3
North       1200        1350   1410
South        980        1020    995
East        1500        1490   1610

The first output silently separated every value from its row. Piped into awk or a parser, that is data corruption that looks like a successful run. -layout reconstructs the physical column positions and is what you want for tables, invoices, and statements.

Page ranges and the form feed

-f (first) and -l (last) are inclusive page numbers:

$ pdftotext -layout -f 2 -l 2 report.pdf -
APPENDIX A — PAGE TWO

Item          Code      Price
Widget        WX-1      19.99
Gadget        GD-7       4.50

By default pdftotext separates pages with a form feed (\f, 0x0C). Extracting the whole three-page file produced exactly three of them; adding -nopgbrk produced zero. Useful if you are splitting per page, confusing if you are feeding a naive line parser — so pick one deliberately.

Encrypted PDFs: pass the password, don’t strip it first

A PDF encrypted with a user password fails loudly rather than producing empty output. Encrypting the test file with AES-256 and re-running:

$ pdftotext enc.pdf out.txt
Command Line Error: Incorrect password
$ echo $?
1

$ pdftotext -upw hunter2 -layout enc.pdf -
QUARTERLY REPORT — PAGE ONE

Region        Q1          Q2     Q3
North       1200        1350   1410

-upw supplies the user (open) password and -opw the owner password. If you need to process the file repeatedly, decrypt once with qpdf instead of passing the password to every tool:

qpdf --password=hunter2 --decrypt enc.pdf plain.pdf

Do this only on files you are authorised to open. qpdf --decrypt also removes owner-password permission flags on a file you can already open, which is a licensing question, not a technical one.

Scanned PDFs contain no text, and no converter will invent it

This is the case most “ways to convert PDF to text” lists omit. A PDF from a scanner or a phone camera is a container holding a picture of a page; there is nothing to extract. pdftotext does not error — it succeeds and returns nothing:

$ pdftotext scanned.pdf -
$ pdftotext scanned.pdf - | wc -c
       1
$ echo $?
0

One byte: the page-break form feed. Exit status 0. Every tool in this article behaves the same way, so switching from pdftotext to Ghostscript to LibreOffice will not help.

Diagnose it in one command before you waste an afternoon. pdffonts lists the fonts a PDF embeds; a page with real text has some, a scan has none:

$ pdffonts report.pdf
name                                 type              encoding    emb sub uni object ID
------------------------------------ ----------------- ----------- --- --- --- ---------
AAAAAB+Monaco                        TrueType          MacRoman    yes yes no       6  0

$ pdffonts scanned.pdf
name                                 type              encoding    emb sub uni object ID
------------------------------------ ----------------- ----------- --- --- --- ---------

$ pdfimages -list scanned.pdf
page   num  type   width height color comp bpc  enc interp  object ID x-ppi y-ppi size ratio
   1     0 image    1275  1650  icc     3   8  jpeg   yes        5  0   150   150  238K 3.9%

Empty font table plus a full-page JPEG means OCR is your only option. Rasterise, then run Tesseract:

pdftoppm -r 300 -png scanned.pdf page
for f in page-*.png; do tesseract "$f" - --psm 6; done > out.txt

--psm 6 (“assume a single uniform block of text”) matters as much as -layout does above. Without it Tesseract broke the same table into one word per line. With it, the table came back — but not perfectly:

TRUTH: South 980 1020 995
OCR  : South 980 1020 955

TRUTH: Page one body line 01
OCR  : Page one body line Q1

Comparing the OCR of one rasterised page against that page’s real text layer, 9 of 45 non-blank lines differed — at 300 dpi, on clean synthetic output with no scanner noise. A digit changed from 995 to 955 without any signal that it happened. Treat OCR output as a searchable approximation, never as a source of numbers you will act on.

What was executed and what was not

Everything above ran on macOS 26.6 (arm64) against poppler 26.08.0, qpdf 12.4.0, Tesseract 5.5.3 and pdftk-java 3.3.3 from Homebrew. poppler is the same upstream project Debian and Fedora ship as poppler-utils, so the flags and behaviour are identical; the apt/dnf lines themselves were not run on Linux. The three-page test PDF was generated locally, and the “scanned” PDF was made by rasterising page one and wrapping the image back into a PDF — which is structurally what a scanner produces: an image, no font table. The test script builds a three-page PDF, an encrypted copy and an image-only copy of its own and checks the -layout, page-range, form-feed, encrypted, scanned, OCR and pdftk results above, skipping any step whose tool is not installed.

pdftotext ships in poppler-utils. poppler forked from the xpdf 3.0 codebase, and Xpdf still ships a separate pdftotext of its own, so the two are related but not the same package and their flags differ in places.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.