Caity Neighbor
The collection
HighSpecimen № 05Work

Synthetic Invoice Generation

Testing a Model With Data That Didn't Exist

Generates a controlled, diverse set of invoices to train a document-processing model when no usable dataset is available.

System snapshot

Built a spreadsheet-and-script system that generated a controlled, representative set of invoices to train a document-processing model when no usable dataset was on hand.

A Model With Nothing to Learn From

A document-processing model learns from a representative sample of documents: enough of them to cover the range of variation it has to handle.

As a solutions architect, I was testing a new type of document-processing model, separate from the platform's usual one. It was a vision-language model, built to read documents it hadn't seen before without being trained from scratch.

To train it and then test it, I still needed that sample, and I didn't have one. We were also in a time crunch, and the training run needed at least 120 documents.

So I built the documents myself from synthetic data, and I chose invoices because I knew the format well.


The Options That Didn't Work

Two obvious paths were open, and neither worked:

  • Use actual invoices. I didn't have a usable set to work from.
  • Generate purely random data. A model learns nothing useful from noise, and unbounded values produce invoices that don't resemble actual ones.

Four Layouts, One Set of Fields

The training set had to meet two requirements:

  • Diverse, so the model saw every layout instead of memorizing a single shape.
  • Consistent, so the same fields always showed up in a form the model could be trained to extract.

I built four invoice layouts that all carried the same set of extracted fields: a handful of header fields, such as vendor, invoice number, invoice date, due date, and total. Vendors, addresses, totals, and line items changed from invoice to invoice, within set bounds. The set of fields the model had to learn stayed the same across all four layouts. That gave the model a representative sample: varied layouts and values, with the same fields to extract every time.


How It's Built

The system ran on a spreadsheet and a Google Apps Script, in five parts.

  1. 01

    Source data

    I generated the underlying field values in Mockaroo, a mock-data generator, then cleaned and formatted them in Google Sheets. The result was 24 structured fields with 200 rows of source values each, covering vendor details, customer details, financial data, and invoice metadata. The model extracted only the header fields. The rest, including the line items, filled out each invoice.

  2. 02

    Line-item engine

    A separate sheet defined item types with bounded quantity and price ranges. Each invoice drew its line items from that sheet, so the totals stayed internally consistent.

  3. 03

    Layouts

    There were four invoice layouts, each with three line items and the same extracted fields, so the output varied in format without changing what the model had to learn.

  4. 04

    Controlled randomization

    The spreadsheet's RAND() function drove the variation, held inside the range each field allowed. A forced refresh between documents made each one come out different.

  5. 05

    Automation

    A Google Apps Script stepped through the layouts, froze a fresh set of values, exported each invoice to PDF, and cleaned up after itself, so a batch could run unattended.

system flow

Output Examples

Here are a few samples from the four layouts.

Invoice Samples

Four Problems in the Script

Getting the Apps Script to produce clean, varied PDFs took far longer than the design did. I hit four problems, and each needed its own fix.

The random values wouldn't refresh on their own.

RAND() didn't recalculate reliably before the script grabbed values, so documents came out repeating each other. I forced the recalculation with a FLUSH() and a tuned delay. The delay gave the randomizer time to finish before the script captured anything.

Live formulas had to be frozen mid-run.

Before each export, the script replaced the live formulas with their current values, so the values couldn't be recalculated during the export.

The layouts broke in opposite ways on export.

One layout corrupted on export unless the script trimmed it to a hard bounding box, deleting every stray row and column past the layout. Deleting anything from the other three broke them, so they got a freeze-only path that deleted nothing. I diagnosed both problems and ran the two paths in the same batch.

Apps Script would export only the entire spreadsheet.

To get one clean invoice, the script hid every other sheet, exported, and restored them. If any step failed, the script still restored the spreadsheet to the state it found it in before the next document.


Impact

Result № 01

Produced the 120 documents the training run required, split evenly across the four layouts.

Result № 02

Replaced what would have been a full night of manual document-building with an unattended batch run. Each document took seconds instead of minutes to set up and export.

Result № 03

The model trained on the set and extracted nearly every header field correctly from a second batch of generated invoices. That batch reused the same invoices with a few values changed, so it was a best-case test.

Result № 04

The labeling stayed hands-on. Even with generation automated, I hand-selected the extracted fields on all 120 invoices to build the training labels.


What I'd Improve

Improvement № 01

Configurable run controls

The document counts and layout selection were hardcoded. The loop generated ten per layout, so reaching 120 meant editing it by hand to run the extra passes. A small control panel in the sheet (how many, which layouts, naming) would make each run a setting instead of a code change.

Improvement № 02

Run logging

Tracking each run (timestamp, count, layout, and any failed exports) would make batches easier to trust and debug.

Improvement № 03

Less reliance on timing

The forced refresh leans on a fixed delay. A more direct way to force recalculation would make runs both faster and less fragile.

Improvement № 04

Messier test documents

Every generated invoice was a clean digital file, while actual invoices often arrive scanned, rotated, smudged, or smeared. The model extracted nearly every field correctly from clean samples, and testing it against messier ones would show whether it holds up under realistic conditions.


On the AI

AI helped me plan the approach, think through the design, and write the Apps Script.

The generator itself is rule-based: values come from Mockaroo and spreadsheet formulas, and nothing in the pipeline uses AI to generate data.

System Details

Signals
Data ModelingSystem Orchestration
Built with
Google Sheets · Apps Script · Mockaroo
Artifacts