You copy a few paragraphs out of a PDF — a paper, a manual, a contract — and paste them. What lands looks like this:
Measurements taken over the first season con-
firm that the effect of the new calibration is
strongest at low boat speeds, where the paddle
wheel reads furthest from the GPS.
37
Above six knots the difference falls below the
resolution of the instrument, and the figures
agree within a tenth of a knot.
A PDF does not store paragraphs. It stores lines of text placed on a page, and copying hands you exactly that:
- a line break at the end of every line on the page;
- words the typesetter hyphenated at the edge of the line, like con- / firm;
- the page number, dropped in wherever the page ended;
- ligatures — ff, fi and fl stored as single characters: ff, fi, fl.
The ligatures are the sneaky part. They look right, so nothing seems wrong until a search for “effect” fails to find “effect”, or the spell checker underlines a word you know is spelled correctly.
The Terminal way
pbpaste | perl -0777 -CSD -pe 's/-\n(?=\p{Ll})//g; s/\n(?!\n)/ /g' | pbcopy
That deletes every hyphen at the end of a line that is followed by a lowercase letter, then turns every single line break into a space. On the example above it gives:
Measurements taken over the first season confirm that the effect of the new calibration is strongest at low boat speeds, where the paddle wheel reads furthest from the GPS. 37 Above six knots the difference falls below the resolution of the instrument, and the figures agree within a tenth of a knot.
The page number is now in the middle of a sentence, and the ligatures are untouched. The hyphen rule is blind, too: a compound like well-known that happened to break across two lines comes out as wellknown. And because every single line break becomes a space, headings and lists are flattened into the paragraph around them.
The KlarText way
Copy the text from the PDF, then paste it with:
Edit ▸ Paste from PDF (⌥⇧⌘V)
The same example arrives as:
Measurements taken over the first season confirm that the effect of the new calibration is strongest at low boat speeds, where the paddle wheel reads furthest from the GPS.
Above six knots the difference falls below the resolution of the instrument, and the figures agree within a tenth of a knot.
- Lines inside a paragraph are joined, and a word split across two lines is rejoined — con- and firm become confirm — while a compound like well-known keeps its hyphen. KlarText decides by checking the macOS dictionary, in the language of the text you pasted.
- Page numbers are dropped. In a longer paste, a line that repeats page after page — the book title or chapter name printed at the top of every page — is removed as well.
- Ligatures (fi fl ff ffi ffl ſt st) become plain letters, and soft hyphens and invisible zero-width characters are removed, so search and spell checking work again.
- Section titles and list items stay on their own lines instead of being run into the surrounding text.
Like every paste command, it does its work on the way in: the broken version never appears in your document.
If the text is already in the document
Select it and choose Edit ▸ Text Transforms ▸ Clean Up PDF Text. With nothing selected, it cleans the whole document. It is one step to undo, like any other edit.
Choosing what gets cleaned
Each pass can be switched on or off in KlarText ▸ Settings ▸ Editor, under PDF Text Cleanup. Three passes are off until you ask for them: marking section titles with # for Markdown, folding curly quotes, dashes and bullets to plain ASCII, and separating text from a two-column layout — which is always a guess, because copied text carries no information about where the columns were on the page.
A related problem
Text copied from emails, mailing-list archives and terminal windows has the same hard line breaks without the page numbers and ligatures. For that, see how to reflow hard-wrapped text.