bettersight-statement-extra.../docs/unrecognized_layout_test.py

118 lines
4.1 KiB
Python

"""
Builds a minimal, hand-constructed PDF with gibberish column headers
(so the keyword detector AND layout_quirks both correctly fail to
resolve it) but real, transaction-shaped rows at known coordinates -
for manually testing the layout mapper's last-resort tier
(ManualMappingView.vue, /map-layout). Same raw-PDF-construction
technique used throughout this project's own test history (no
reportlab/fpdf available) - explicit per-word Tm/Tj positioning so
pdfplumber reports exact, predictable x/y coordinates.
`top` below is measured from the PAGE'S OWN TOP edge (0 = top),
pdfplumber's own convention - converted to PDF's bottom-up y just
before writing each Tm.
Run: python3 docs/unrecognized_layout_test.py
Produces: docs/unrecognized_layout_test.pdf
"""
import zlib
PAGE_W, PAGE_H = 612, 792 # US Letter, points
words = []
def add(text, x, top):
words.append((text, x, top))
add("Statement Sample Co.", 50, 60)
add("Opening balance 1,000.00 as of 01 Jan 2027", 50, 90)
# Gibberish header - deliberately NOT matching any real column
# vocabulary (date/description/debit/credit/balance/amount/status/etc)
# so tier 1 (keyword detector) fails, and nothing here is a mechanical
# quirk (no wrapped label, no stray symbol) so tier 2 (layout_quirks)
# correctly falls through too.
HEADER_TOP = 130
add("Qfield1", 50, HEADER_TOP)
add("Qfield2", 150, HEADER_TOP)
add("Qfield3", 360, HEADER_TOP)
add("Qfield4", 440, HEADER_TOP)
add("Qfield5", 520, HEADER_TOP)
# Three real transaction-shaped rows, going DOWN the page - a clean,
# self-consistent running-balance chain (1000.00 -> 975.00 -> 2975.00
# -> 2825.00) once correctly mapped, so Preview's own
# statement_balance/running_balance checks have something real to
# confirm.
TXN_ROWS = [
(160, "03 Jan 2027", "Coffee Shop Purchase", "25.00", "", "975.00"),
(180, "05 Jan 2027", "Salary Deposit", "", "2000.00", "2975.00"),
(200, "10 Jan 2027", "Electric Bill Payment", "150.00", "", "2825.00"),
]
for top, date, desc, debit, credit, balance in TXN_ROWS:
add(date, 50, top)
add(desc, 150, top)
if debit:
add(debit, 360, top)
if credit:
add(credit, 440, top)
add(balance, 520, top)
# --- Build the content stream (BT/Tm/Tj per word, Helvetica 10pt) ---
lines = ["BT", "/F1 10 Tf"]
for text, x, top in words:
y = PAGE_H - top # pdfplumber's "top" (from page top) -> PDF's bottom-up y
escaped = text.replace("\\", "\\\\").replace("(", "\\(").replace(")", "\\)")
lines.append(f"1 0 0 1 {x} {y} Tm")
lines.append(f"({escaped}) Tj")
lines.append("ET")
content = "\n".join(lines).encode("latin-1")
compressed = zlib.compress(content)
objects = []
def obj(body):
objects.append(body)
return len(objects) # 1-based object number
font_num = obj(b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>")
content_num = obj(
b"<< /Length " + str(len(compressed)).encode() + b" /Filter /FlateDecode >>\nstream\n"
+ compressed + b"\nendstream"
)
page_num = obj(
b"<< /Type /Page /Parent 4 0 R /MediaBox [0 0 "
+ f"{PAGE_W} {PAGE_H}".encode() + b"] /Resources << /Font << /F1 "
+ str(font_num).encode() + b" 0 R >> >> /Contents "
+ str(content_num).encode() + b" 0 R >>"
)
pages_num = obj(b"<< /Type /Pages /Kids [" + str(page_num).encode() + b" 0 R] /Count 1 >>")
catalog_num = obj(b"<< /Type /Catalog /Pages " + str(pages_num).encode() + b" 0 R >>")
# --- Assemble the file, tracking byte offsets for xref ---
out = bytearray(b"%PDF-1.4\n")
offsets = [0] # object 0 is the free-list head
for i, body in enumerate(objects, start=1):
offsets.append(len(out))
out += f"{i} 0 obj\n".encode() + body + b"\nendobj\n"
xref_offset = len(out)
out += f"xref\n0 {len(objects) + 1}\n".encode()
out += b"0000000000 65535 f \n"
for off in offsets[1:]:
out += f"{off:010d} 00000 n \n".encode()
out += (
b"trailer\n<< /Size " + str(len(objects) + 1).encode()
+ b" /Root " + str(catalog_num).encode() + b" 0 R >>\nstartxref\n"
+ str(xref_offset).encode() + b"\n%%EOF"
)
with open("docs/unrecognized_layout_test.pdf", "wb") as f:
f.write(out)
print(f"Wrote docs/unrecognized_layout_test.pdf ({len(out)} bytes), {len(objects)} objects")