Pagecraft

ocr

Make a scan searchable

Read the words on a scanned page and put them in the file, without changing how it looks.

Reads
a scanned PDF or a photograph of a page — application/pdf, image/png, image/jpeg, image/webp
Writes
one file — PDFs
Tool page
/ocr-pdf

Example

Make a scan searchable without changing how it looks.

Given

  • scan.pdf — a scan: pages that are pictures of words

From code

            import { loadDocument, runOperation } from '@pagecraft/core';

const scan = await loadDocument({ bytes, name: 'scan.pdf' });

const [output] = await runOperation('ocr', [scan], {
  languages: [
    'eng',
  ],
  dpi: 300,
});
          

From a shell

            pagecraft ocr scan.pdf --languages eng --dpi 300
          

What comes back

One PDF of 1 page, containing “Quarterly report”.

This example is run by the test suite. If it stopped being true, the build would fail.

Options

languages--languages

string[], any of

Language

What the page is written in. Add more than one for a document that mixes them.

Default: ['eng']

  • eng — English
  • ara — Arabic
  • ben — Bengali
  • bul — Bulgarian
  • cat — Catalan
  • ces — Czech
  • chi_sim — Chinese (Simplified)
  • chi_tra — Chinese (Traditional)
  • dan — Danish
  • deu — German
  • ell — Greek
  • est — Estonian
  • fas — Persian
  • fin — Finnish
  • fra — French
  • heb — Hebrew
  • hin — Hindi
  • hrv — Croatian
  • hun — Hungarian
  • ind — Indonesian
  • isl — Icelandic
  • ita — Italian
  • jpn — Japanese
  • kan — Kannada
  • kor — Korean
  • lav — Latvian
  • lit — Lithuanian
  • mal — Malayalam
  • mar — Marathi
  • msa — Malay
  • nld — Dutch
  • nor — Norwegian
  • pol — Polish
  • por — Portuguese
  • ron — Romanian
  • rus — Russian
  • slk — Slovak
  • slv — Slovenian
  • spa — Spanish
  • sqi — Albanian
  • srp — Serbian
  • swa — Swahili
  • swe — Swedish
  • tam — Tamil
  • tel — Telugu
  • tha — Thai
  • tur — Turkish
  • ukr — Ukrainian
  • urd — Urdu
  • vie — Vietnamese

dpi--dpi

integer (dpi)

Density

How finely the page is drawn before it is read. 300 suits most scans.

Default: 300

deskew--deskew

boolean

Straighten a crooked page

A page fed through the scanner askew reads badly. This finds the angle and turns the picture back through it before reading, without changing the page itself.

Default: true

binarise--binarise

boolean

Sharpen to black and white

Faint and uneven scans read better as ink and paper. Only what is read is sharpened; the page keeps every shade it had.

Default: true

force--force

boolean

Read pages that already have text

A page whose words are already in the file is left alone, so that they are not written twice.

Default: false

pages--pages

string, a page selection: '1-3, 7, 9-', 'even', 'last'

Pages

Which pages to read: '1-3, 7, 9-'.

Default: 'all'

What it cannot do

  • The reading is done by whatever engine the host has plugged in. How good it is, and which languages it can read at all, are that engine’s to answer.
  • The page is not changed: the words go underneath the picture of them. A bad scan stays a bad scan, and the recognition is only as good as it.
  • Confidence says how sure the engine is, which is not the same as whether it is right.
  • A page carrying so much as a stamp counts as having text and is left alone. Read it anyway and it will carry both its own words and the recognised ones.
  • On a crooked page the words are written along the crooked lines, so that selecting one lands on it. Every word is still found by a search, but a reader listing the text of such a page reads it in the order the words sit at rather than the order they were written in.