from pathlib import Path
import fitz

OUTPUT = Path(__file__).with_name('merged-header-table.pdf')
PAGE = fitz.paper_rect('a4')
LEFT, TOP = 54, 96
COL_WIDTHS = [120, 180, 90, 110]
ROW_HEIGHT = 36

# First row merges columns 1 and 2; following rows use all four columns.
ROWS = [
    ['Account Summary', 'Qty', 'Amount'],
    ['D-401', 'Managed archive', '3', '$210.00'],
    ['D-402', 'Restore drill', '1', '$95.00'],
]

doc = fitz.open()
page = doc.new_page(width=PAGE.width, height=PAGE.height)
page.insert_text((LEFT, 42), 'Merged Header Service Table', fontsize=16)
page.insert_text((LEFT, 60), 'Synthetic PDF-to-Word regression fixture', fontsize=9)

x = [LEFT]
for width in COL_WIDTHS:
    x.append(x[-1] + width)
y = [TOP + index * ROW_HEIGHT for index in range(4)]

# Outer border and all horizontal rules as individual line operators.
for xx in (x[0], x[-1]):
    page.draw_line((xx, y[0]), (xx, y[-1]), width=1.0, color=(0, 0, 0))
for yy in y:
    page.draw_line((x[0], yy), (x[-1], yy), width=1.0, color=(0, 0, 0))

# Column boundaries 3 and 4 span the full table; the boundary between columns 1 and 2 starts below the merged header.
page.draw_line((x[1], y[1]), (x[1], y[-1]), width=1.0, color=(0, 0, 0))
page.draw_line((x[2], y[0]), (x[2], y[-1]), width=1.0, color=(0, 0, 0))
page.draw_line((x[3], y[0]), (x[3], y[-1]), width=1.0, color=(0, 0, 0))

page.insert_text((x[0] + 8, TOP + 23), ROWS[0][0], fontsize=10)
page.insert_text((x[2] + 8, TOP + 23), ROWS[0][1], fontsize=10)
page.insert_text((x[3] + 8, TOP + 23), ROWS[0][2], fontsize=10)
for row_index, row in enumerate(ROWS[1:], start=1):
    baseline = TOP + row_index * ROW_HEIGHT + 23
    for col_index, text in enumerate(row):
        page.insert_text((x[col_index] + 8, baseline), text, fontsize=10)

page.insert_text((LEFT, y[-1] + 30), 'Contract candidate: preserve merged header semantics without losing row order.', fontsize=9)
doc.set_metadata({
    'title': 'Merged Header Service Table',
    'author': 'DocBig synthetic regression fixture',
    'subject': 'PDF to Word merged-header table candidate',
})
doc.save(OUTPUT, garbage=4, deflate=True)
doc.close()
print(f'WROTE={OUTPUT.name} bytes={OUTPUT.stat().st_size}')
