Skip to content

Prepare documents for OCR and AI ​

Apps that read receipts and invoices usually send the photo to OCR, such as Tesseract or a cloud service, or to an AI model that reads images. Both do worse with real photos: taken at an angle, with shadows, and with half the table around the paper. OCR skips words, and the model spends its effort on the background. In this example, the user straightens and crops the page first, and the image leaves in black and white.

PaperPic doesn't read the text. Your server does, with whatever service you pick, and the photo stays in the browser until your code sends the result.

This function sends the image to your server, which reads it and replies with the fields it found:

ts
interface Receipt {
  date: string
  merchant: string
  total: string
}

async function readReceipt(blob: Blob, type: string): Promise<Receipt> {
  const body = new FormData()
  body.append('image', blob, `receipt.${type.split('/')[1]}`)
  const response = await fetch('/api/receipts/read', { method: 'POST', body })
  if (!response.ok) {
    throw new Error(`Reading failed with status ${response.status}`)
  }
  return response.json()
}

The edit starts in black and white as soon as the photo is on screen, so the user only has to straighten the page and apply the changes:

html
<label>
  Photo of the receipt
  <input id="photo" type="file" accept="image/*" capture="environment" />
</label>
<div id="viewer" style="height: 480px"></div>
<p id="status" role="status"></p>
ts
import { PaperPic } from '@paperpic/core'
import '@paperpic/core/style.css'

const input = document.querySelector<HTMLInputElement>('#photo')!
const status = document.querySelector('#status')!

const pic = new PaperPic(document.querySelector<HTMLElement>('#viewer')!, {
  alt: 'The receipt you attached',
  tools: [
    'perspective',
    'crop',
    'rotate',
    'finish',
    'redact',
  ],
  maxOutputSize: 2000,
})

input.addEventListener('change', () => {
  const file = input.files?.[0]
  input.value = ''
  if (file) {
    status.textContent = ''
    pic.setOptions({ src: file })
  }
})

// Black and white is one edit, so the user can still undo it or choose Gray in Clean up
pic.on('load', () => {
  pic.edit()
  pic.setFinish({ mode: 'mono' })
})

pic.on('apply', async ({ blob, type }) => {
  status.textContent = 'Reading…'
  try {
    const { merchant, total } = await readReceipt(blob, type)
    status.textContent = `${merchant}: ${total}`
  }
  catch {
    status.textContent = 'The receipt could not be read'
  }
})

Notes ​

  • Straighten helps OCR more than anything else: it flattens the page so the lines of text run level, and crops away the table. It finds the paper by itself when it opens, so the user only checks the corners. tools leaves out Flip and Adjust, which a receipt rarely needs.
  • Black and white removes shadows and uneven light, which OCR engines such as Tesseract read as noise, and makes a small PNG. AI models read color, though: for documents with photos, stamps or a highlighted total, leave out setFinish() or let the user choose Gray.
  • AI models shrink large images before they read them, and many charge by size. 2000 pixels on the longest side keeps small print legible and the upload light. Check the limits of your model or OCR service.
  • If the image goes to an outside service, the user can first cover an account number, a signature or a face with Redact. Apply writes those areas into the pixels, so the service never receives what was under them. Always send the result, never the source.
  • With an AI model, your server can ask for exactly the fields it needs, such as the merchant, the date and the total, as JSON, so its reply already has the shape that readReceipt returns.

Released under the MIT License.