Command Palette

Search for a command to run...

Where the Line Breaks Decide What You Get

Where the Line Breaks Decide What You Get

T
Toolz Team
|Oct 23, 2026|17 min read
Prefer Toolz.dev on Google

Part of the PDF Tools collection

PDF to Text

Pull the text out of a PDF and save it as a .txt file, in your browser. Keep the page layout or reflow it into paragraphs, with or without page markers.

Use the PDF to Text

A PDF does not contain lines of text. It contains glyphs at coordinates, and the lines you can see are a thing your eye assembles. Any extractor has to assemble them too, from three numbers per run and a couple of thresholds, and almost every oddity in extracted text traces back to one of those thresholds making a reasonable call that happened to be wrong. The one I fixed while writing this turned a surname broken across a line, "Osei-" then "Mensah", into "Osei- Mensah": hyphen kept, as intended, with a space after it that never belonged there.

TL;DR: Extraction is line reassembly, and the settings that matter are invisible. Runs within half a font height of each other are one line; a vertical gap more than 1.45 times the page's usual gap starts a paragraph; a horizontal gap under a quarter of the font height is not a word space. Choose reflow to paste into an editor and preserve for tables and code. A PDF averaging under 12 characters a page is a scan and needs OCR instead, which the tool says rather than handing you an empty file. PDF to text runs in the browser.

Why does a PDF need "extracting" at all?

Because the text in it was written as drawing instructions, and reading them back is not the reverse of writing them.

A content stream says "set this font at this size, move here, show these glyphs". The specification describes a painting model, and the text operators sit beside the ones for drawing lines and filling shapes. There is no object meaning line, paragraph or column. What pdf.js hands back is a flat list of positioned runs, each with a string, an x, a y, a width and a font height, in the order the page was painted. On a test page that order is not reading order, and a single run can be one character.

Everything else is reconstruction. Three thresholds do almost all of it, and they are worth knowing because they explain the failures.

Threshold Value What it decides
Line tolerance 0.5 x font height Whether two runs sit on the same line
Paragraph ratio 1.45 x the page's median gap Whether a vertical gap starts a new paragraph
Space ratio 0.25 x font height Whether a horizontal gap is a word space

Each is a fraction of the text's own size rather than a fixed number of points, so a footnote and a heading on the same page get different tolerances, and a document set in 9 point behaves like one set in 14.

Why is the word-space threshold necessary?

Because without it every word comes apart.

A PDF parser frequently splits a single word across several runs with no gap between them, for kerning or for a font change mid-word. Joining runs on a space unconditionally turns filename into f i l e n a m e. Joining with nothing at all runs separate words together.

So the rule is a gap wider than a quarter of the font height counts as a space, which is roughly the width of a real word space in most typefaces. Below that, the runs are parts of one word.

This is the threshold you will never notice working and will immediately notice failing, usually on a heading set with wide letter-spacing, where every letter is far enough apart to look like a word.

What is the difference between reflow and preserve?

Reflow gives you prose. Preserve gives you the page.

Here is one paragraph laid out over three lines, followed by another, run through both modes:

--- preserve ---
The committee reviewed the figures and agreed that the
revised protocol should be circulated before the end of
the month.
A second paragraph begins here after a larger gap.

--- reflow ---
The committee reviewed the figures and agreed that the revised protocol should be circulated before the end of the month.

A second paragraph begins here after a larger gap.

Reflow joined the three lines into one sentence and put a blank line between the paragraphs. Preserve kept every line break the page was laid out with, including the one in the middle of a sentence.

Use reflow Use preserve
Pasting into a document or an editor A table, where the columns are the content
Quoting a passage A code listing, where line breaks are syntax
Feeding text to a script or a model An address block or a form
Anything you will reformat anyway Anything where the layout carries meaning

Reflow is the default and the right one most of the time. The mistake people make is using it on a table, where joining the rows into prose destroys the only structure the table had.

How does it decide where a paragraph ends?

By comparing each vertical gap against the page's own typical gap.

Every gap between consecutive lines is measured, the median is taken, and any gap more than 1.45 times that median starts a new paragraph. So a single-spaced page does not come back as one paragraph per line, and a double-spaced one does not come back as a single block.

Using the median rather than a fixed number is what makes this work across documents with different leading. It is also where it fails: a page with very few lines has no reliable median. A title page, a short note or a cover sheet often comes back as one run-on paragraph, which is the same weakness the Word conversion has, for the same reason.

The fix is manual and quick. Paragraph breaks are obvious to a reader in a way they are not to a threshold.

What happened to words broken across lines?

They are rejoined, and the rule for which hyphens to drop is a guess that is right more often than not.

When a line ends in a hyphen, the question is whether the typesetter put it there to break a word or whether it belongs to the word. The rule used here is case: if the next line starts lowercase, the hyphen was the typesetter's and goes; if it starts with a capital or a digit, the hyphen is part of the text and stays.

Broken across lines Result Right?
self- + contained selfcontained yes
re- + encode reencode yes
client- + Side client-Side yes
Osei- + Mensah Osei-Mensah yes
COVID- + 19 COVID-19 yes
2026- + 03 2026-03 yes
well- + known wellknown no

The last row is not fixable by any rule. "well-known" broken at its own hyphen and "wellknown" broken by justification produce the identical pair of lines, and telling them apart needs a dictionary rather than a threshold. The same applies to "re-" and "encode" above, which happens to be right only because "reencode" is not a word anybody writes.

The rows in the middle are the ones that changed while this guide was being written. The non-break case used to add a space along with the kept hyphen, producing Osei- Mensah and 2026- 03. The hyphen belongs to the word either way, so a space after it is never correct, and the test that asserted the old output has been corrected along with the behaviour.

How does it know a PDF is a scan?

By counting characters per page, with a threshold low enough that only a genuine scan trips it.

A scanned document is a PDF full of images. There is no text layer, so extraction returns nothing, and the useful response is to say so rather than hand over an empty file.

  0 chars over 3 pages   treated as a scan
 30 chars over 3 pages   treated as a scan
 36 chars over 3 pages   has a text layer
 11 chars over 1 page    treated as a scan
 12 chars over 1 page    has a text layer

The line is twelve characters a page. That is not zero on purpose: a scanned page often carries a little incidental text from the scanner, a page number stamped by the software, or a watermark. A threshold at zero would call those documents extractable and return three page numbers.

If your PDF is a scan, the next step is OCR, and the measured account of what breaks OCR has the measured version of that: tilt past three degrees costs you far more than low resolution does.

What about columns?

They interleave, and this is the limit worth knowing before you trust any extraction.

Lines are grouped by baseline first, so two columns of text sitting at the same vertical positions are read across rather than down. In a test of a two-column report page the result was every line of the left column followed immediately by the line beside it:

Revenue rose in the third Headcount grew by four, quarter on the back of
two all in support. The hiring large renewals signed in plan for the fourth

Every word is present. The order is useless, and the output reads as fluent nonsense rather than as an error.

Academic papers and newspapers often survive this, because the gutter between columns is wide and regular enough for a page segmentation step to find. A report with a sidebar, a pull quote or a two-column table frequently does not.

The reliable approach on a columned document is to check the first paragraph of each page. If the sentences are spliced, extract one column at a time by cropping, or accept that you are getting a word bag rather than prose.

Can you get a table out of a PDF?

The values, yes. The table, no, and the reason is that the PDF almost certainly does not contain a table either.

A table in a PDF is usually text drawn at coordinates with some lines drawn around it. The lines are graphics operations with nothing in the file connecting them to the text they enclose, and the cells are not cells; they are runs that happen to line up. So an extractor sees rows of text and reads them across:

Region Q2 Q3 EMEA 412 455 AMER 388 401 APAC 120 166

Every figure survived. You cannot tell from that line which quarter 412 belongs to without counting positions and knowing the shape of the original.

Preserve mode helps a little, because it keeps the row structure as line breaks, which at least tells you where each row ended. It still gives you no column boundaries.

Two things do work. A tagged PDF, exported carefully from Word or InDesign with accessibility structure, carries real table markup and converts properly; these are a minority and you can usually tell because the document was made by someone who cared. And for an untagged table that matters, copying it into a spreadsheet by hand is faster than fighting any tool, which is an unsatisfying answer that has survived every attempt to improve on it.

What do the page markers do?

They keep the page numbers, which matters more than it sounds for anything you will cite or refer back to.

With markers on, each page is labelled and empty pages are still counted:

--- Page 1 ---

First page text.

--- Page 2 ---

--- Page 3 ---

Third page text.

Page 2 had no extractable text and still appears. That is deliberate: a scanned sheet between two text pages would otherwise vanish and every later page number would be wrong by one.

With markers off, the empty page leaves no trace and the two pages of text are simply joined. That is the right choice when you are going to paste the text somewhere and the pagination is an artefact of the PDF rather than part of the document.

Is extracted text the same as the original?

The characters are. Everything else is gone, and the list is worth reading before you rely on the output.

Bold, italic, font, size, colour, tables as tables, images, headers and footers as distinct from body text, footnotes as distinct from the sentence they annotate, links as links, and the reading order of any complex layout. What you get is characters and paragraph breaks.

For most uses that is exactly right. Searching, quoting, counting, feeding a script, pasting into an editor: all of those want the words and nothing else.

Where it bites is a document whose meaning lives partly in its formatting. A contract where a defined term is bold, a spec where a code sample is distinguished by its font, a report where a footnote changes what a sentence claims. Extracted text flattens all of it, and nothing in the output says so.

Should you extract to text or convert to Word?

Text if a machine will read it, Word if a person will edit it.

Both run the same reassembly, so the quality of the words is identical. The difference is only what gets written at the end: a .txt file, or the same paragraphs inside a zip of XML.

You want to Use
Grep, search, or feed a script PDF to text
Count words or analyse the content PDF to text
Edit the prose and send it on PDF to Word
Keep the layout exactly Neither; get the original file

The last row is the one people arrive wanting, and no extraction gives it to you. The Word conversion guide goes through why in detail, with a table that comes back as a single line of values.

How large a PDF can it handle?

Large, and the limit is the tab rather than the format.

The whole file is read into page memory and parsed there, and the extracted text is held alongside it. A few hundred pages of ordinary text is unremarkable; a thousand-page scan at high resolution is a different proposition because the images come along for the ride even though none of them yields text.

If a document is big enough to worry about, the signal is the parse rather than the extraction. Pulling the text out of an already-parsed page is cheap; getting the file into the parser is what takes the time and the memory.

Why does this run in the browser?

Because a parse needs no server, and the PDFs people extract are rarely public.

The file is read from your disk into the tab, parsed there with pdf.js compiled for the web, reassembled into text and handed back. No request carries the document, nothing is stored, and there is no copy anywhere to be retained under terms you did not read.

The documents in question are contracts, statements, medical letters, court filings, papers under embargo, internal reports. The conventional alternative uploads the whole file to a service in exchange for an operation that is a parse and some arithmetic on coordinates.

The cost is the tab's memory on very large files, and no OCR for scans. Both are visible before you start rather than discovered afterwards.

Comments

0 comments

0/2000 characters

No comments yet. Be the first to share your thoughts!