The problem
Small accounting practices still key supplier invoices into the books by hand: both GSTINs, the invoice number and date, every line, the taxable value, CGST and SGST or IGST, and the total. It is slow, and the mistakes that cost money are easy to miss: a GSTIN with one wrong character, tax at the wrong rate, a total that does not add up, or the same bill booked twice.
The client in this demo is Sundaram & Co., a made-up practice in Madurai. The workflow does the typing and the checking, and a person only looks at what it flags, with the reason in front of them.
The data
Five invoices I made for the demo, from made-up suppliers in Madurai, Bodinayakanur, Bengaluru and Pune to made-up buyers in Madurai. Each has both GSTINs, an invoice number and date, the place of supply, line items with HSN or SAC codes, the taxable value, CGST and SGST or IGST, the total and the amount in words. The GSTINs follow the real format and have valid check characters, except where I broke one on purpose; any match with a real business is a coincidence.
I wrote them as HTML in two common layouts, a boxed one like most billing software prints and a plainer modern one, rendered them to images with Playwright, and kept the expected values beside them so the tests know the right answers.
They cover a clean invoice within Tamil Nadu, one from Karnataka charged IGST, a quick scan (tilted 1.3°, on grey paper, with speckle and blur), one whose total does not add up, and one whose supplier GSTIN has the wrong check character.
The model
Tesseract reads the text. It is the long-standing open-source OCR engine; here its LSTM recogniser runs as WebAssembly in a Web Worker, with the English model from tessdata_best. It reports every word with its box on the page and how sure it is.
The image is prepared first, on a canvas: scaled so the letters are tall enough, turned grey, the light evened out (the paper's brightness is estimated block by block and divided out, so shadows and grey paper come out white), speckle removed with a median filter when the paper is grainy, the contrast stretched, and the page straightened by finding the angle at which the ink lines up into the sharpest rows.
A second look. Boxed layouts sometimes make Tesseract run a small label and its value together into one garbled word. Words it is unsure of are cropped out and read again on their own, and the more confident reading wins. It was needed on 4 of the 5 samples.
Finding the fields is rules, not a model. The words are rebuilt into rows and segments, labels such as “Invoice No.”, “Place of Supply” and “Grand Total” are found (forgiving a misread letter), and each value is taken beside or below its label. GSTINs are found by their shape, with letters and digits the OCR confuses put right where the format allows only one. Line items are read from the right of each row under the table's header: amount, rate, quantity, then the HSN or SAC code.
11 checks apply the rules an accountant would: each GSTIN's check character (a Luhn sum in base 36), CGST and SGST within a state or IGST between states, quantity × rate on every line, the lines adding up to the taxable value, tax at its rate within ₹1, the totals, the amount in words, a real date not in the future, and no duplicate in this session's ledger.
Rules suit this job: every value traces back to a box on the page, every flag comes with its reason, and nothing beyond the OCR engine has to be downloaded.
The pipeline
- 01Load
A sample, an upload, or a phone's camera. The image is decoded in the tab.
- 02Prepare
Scale, grey, even out the light, remove speckle, stretch the contrast, straighten.
- 03Read text
Tesseract reads the page in a Web Worker, then takes a second look at unclear patches.
- 04Find fields
Rows and segments from the word boxes; labels, values, GSTINs and the line items.
- 05Check
11 GST checks, run again on every edit, each with a plain reason.
- 06Export
A JSON record, a CSV row, and this session's ledger as a CSV for the books.
Results
Measured when this page was built, with the same code the demo runs, on what the demo's OCR read from each sample (saved for the unit tests, with its timings). What it gets wrong are misread characters no rule can see: the invoice number on the Hebbal Packaging Industries invoice, “HPI/2627/1187” read as “HP1/2627/1187”; the line 1 description on the Pandian Electricals invoice, “PVC copper wire 2.5 sq mm, 90 m coil” read as “PVC copper wire 2.5 sg mm, 90 m coil”. That is why every field shows how sure the OCR was and stays editable.
For a client
For a practice like Sundaram & Co. I would add the client's own suppliers as a lookup, so a known GSTIN fills in the name and state, and confirm unknown GSTINs against the GST portal's public search through a small server.
Real invoices vary far more than five samples. I would collect a few hundred of the client's, measure accuracy field by field and supplier by supplier, and add a layout or vision-language model for the layouts the rules cannot handle, keeping the checks as they are.
PDFs that carry their own text would skip the OCR altogether, and e-invoices with a signed QR code can be verified directly.
The ledger would post to the accounting system through its import format or API, with flagged invoices sent to a review queue instead of the books.
Stack
| Part | What it does here | Licence |
|---|---|---|
| Tesseract.js | Runs the Tesseract OCR engine, compiled to WebAssembly, in a Web Worker | Apache 2.0 |
| tessdata_best (English) | The LSTM model that recognises English text, integerised for Tesseract.js | Apache 2.0 |
| My image preparation | Evening out the light, speckle removal, contrast and deskew on canvas pixels, in TypeScript and unit-tested | Mine |
| My field rules and GST checks | Rows and segments from word boxes, labels and values, GSTIN repair and check character, tax and totals checks | Mine |
| Playwright | Rendered the sample invoices to images, and tests the demo in four browsers | Apache 2.0 |
| Preact | The interactive demo | MIT |
| Astro | The page, built as static HTML | MIT |
Privacy
The invoice never leaves your browser: the image is decoded, prepared and read in this tab, and the OCR engine runs in a Web Worker on your device.
The engine and its English model, about 7 MB, come from this site rather than a third party, and your browser keeps them, so the next visit starts at once. The ledger lives in this tab's session storage and is gone when you close it.
Limits
- The samples are made up and printed cleanly. Real photos with folds, stamps, handwriting or deep shadows read worse, and the rules know only the layouts and labels common on Indian GST invoices.
- Tesseract reads English here; invoices printed in Tamil or Hindi would need those language models too.
- A page photographed sideways or upside down is not turned upright: only tilts of up to 5° are straightened.
- The checks show an invoice is consistent, not that it is genuine. A GSTIN can have the right check character and still not be registered, which only the GST portal can confirm.
- One GST rate per invoice, simple round off and no cess: invoices with several rates or a cess need more rules.