51

On Linux - How to extract text from a .pdf in which text really is text, not a scanned image? I want something I can use on the command line / in a script, not interactively. (I don't want to convert to .tif and use OCR - text is already available in the .pdf file, so why introduce inaccuracies from imperfect OCR?)

1

4 Answers 4

59

pdftotext that comes with poppler will try to extract any text found in the PDF.

3
  • 1
    Thanks for your quick response, Ignacio! I was already checking out pdftotext that comes w xpdf (from foolabs.com) - your answer prompted me to take another look, and I got it working. Poppler appears to have evolved from xpdf, so I will take a look at that too. Thanks again!
    – RobM
    Nov 5, 2010 at 19:56
  • 2
    pdftotext input.pdf output.txt
    – Jack M
    Sep 12, 2020 at 9:52
  • pdftotext input.pdf - (source) Jun 20, 2023 at 21:24
19

Ignacio's answer is just fine. In fact, it'd be the first thing on my list. Well, that and perhaps to suggest the pdftohtml tool that also comes with poppler, combined with pdfreflow if you want to try to reassemble the text into paragraphs, etc. (Of course, this will give you HTML output, but converting HTML to plain text can be done in many ways.)

Here are some other options too.

The ebook-convert command line tool from Calibre, which can convert .PDFs to plain text (or RTF or a number of ebook formats, like ePub, etc.)

pdftxtextract from Podofo

Abiword can be called from the commandline to convert between any formats it can input from/export to, and with the appropriate import plugin, this includes PDFs:

abiword --to=txt file.pdf

(In fairness, I think AbiWord and calibre both use the poppler libraries, but I'm not positive.)

1
  • 1
    Thanks frabjous! In this case, I'm just extracting the text so that I can scan for specific strings (vendor names, account numbers) and patterns (invoice numbers and dates) - so no need to reformat or redisplay it. I appreciate the corroboration and the alternatives - and I'm sure others will too! -- Rob
    – RobM
    Nov 17, 2010 at 17:15
0

In addition to pdftotext and pdftohtml you can combine it with pandoc to convert the pdf to other formats like markdown

mdpdf () {
    pdftohtml -s -stdout "${@}" |
        pandoc -f html -t markdown -o "${1%.pdf}".md ;
}
0

Comparison of how methods handle paragraphs

One issue with pdftotext from poppler-utils 22.12.0 which was mentioned by Ignacio is that it adds newlines within paragraphs when the paragraph is longer than the PDF page width, e.g. something like:

1:1 In the beginning God created the heaven and
the earth.
1:2 And the earth was without form, and void; and
darkness was upon the face of the deep. And the
Spirit of God moved upon the face of the waters.
1:3 And God said, Let there be light: and there
was light.
1:4 And God saw the light, that it was good: and
God divided the light from the darkness.

These extra newlines make the txt files really bad to read on a device like a Kindle.

I have found however that ebook-convert mentioned by frabjous overcomes this very well, and produces something like:

1:1 In the beginning God created the heaven and the earth.

1:2 And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters.

1:3 And God said, Let there be light: and there was light.

1:4 And God saw the light, that it was good: and God divided the light from the darkness.

which maintains paragraphs in single lines, regardless of how long the paragraph is, and adds a double newline between paragraphs, and behaves much better on a Kindle.

I'm going to test methods mentioned in other answers with this test PDF generated from this Libreoffice .odt file:

pdftotext output:

Title of my file
Table of Contents
H1 1......................................................................................................................................................1
H2 1 1...............................................................................................................................................1
H2 1 2...............................................................................................................................................1
H1 2......................................................................................................................................................1
H2 2 1...............................................................................................................................................1
H2 2 2...............................................................................................................................................1

H1 1
H2 1 1
H2 1 2
First very important paragraph.
And now a very very very very very very very very very very very very very very very very very
very very very very very very very long paragraph that gets split across two lines.
Reference to H1 1 on page: 1
https://commons.wikimedia.org/wiki/File:Fractal_Broccoli.jpg

H1 2
H2 2 1
H2 2 2

ebook-convert output:

Title of my file

Table of Contents

H1 1......................................................................................................................................................1

H2 1 1...............................................................................................................................................1

H2 1 2...............................................................................................................................................1

H1 2......................................................................................................................................................1

H2 2 1...............................................................................................................................................1

H2 2 2...............................................................................................................................................1

H1 1

H2 1 1

H2 1 2

First very important paragraph.

And now a very very very very very very very very very very very very very very very very very very very very very very very very long paragraph that gets split across two lines.

Reference to H1 1 on page: 1

https://commons.wikimedia.org/wiki/File:Fractal_Broccoli.jpg

H1 2

H2 2 1

H2 2 2





Document Outline


H1 1 H2 1 1

H2 1 2





H1 2 H2 2 1

H2 2 2

The line break aspect was also asked more specifically at: https://unix.stackexchange.com/questions/691579/how-to-convert-pdf-file-to-text-without-breaking-lines

Tested on Ubuntu 23.04, poppler-utils 22.12.0, calibre 6.11.0.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .