from pathlib import Path
import fitz

OUTPUT = Path(__file__).with_name('bordered-three-row-table.pdf')
PAGE = fitz.paper_rect('a4')
LEFT, TOP = 54, 96
COL_WIDTHS = [120, 210, 90, 110]
ROW_HEIGHT = 34
ROWS = [
    ['Code', 'Service', 'Qty', 'Amount'],
    ['B-201', 'Security review', '2', '$300.00'],
    ['B-202', 'Backup restore', '1', '$125.00'],
]

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

x_positions = [LEFT]
for width in COL_WIDTHS:
    x_positions.append(x_positions[-1] + width)
y_positions = [TOP + index * ROW_HEIGHT for index in range(len(ROWS) + 1)]

for x in x_positions:
    page.draw_line((x, y_positions[0]), (x, y_positions[-1]), width=1.0, color=(0, 0, 0))
for y in y_positions:
    page.draw_line((x_positions[0], y), (x_positions[-1], y), width=1.0, color=(0, 0, 0))

for row_index, row in enumerate(ROWS):
    baseline = TOP + row_index * ROW_HEIGHT + 22
    for col_index, text in enumerate(row):
        page.insert_text((x_positions[col_index] + 8, baseline), text, fontsize=10)

page.insert_text((LEFT, y_positions[-1] + 30), 'Contract: visible borders permit safe reconstruction of this three-row table.', fontsize=9)
doc.set_metadata({
    'title': 'Bordered Three-Row Service Table',
    'author': 'DocBig synthetic regression fixture',
    'subject': 'PDF to Word bordered three-row table',
})
doc.save(OUTPUT, garbage=4, deflate=True)
doc.close()
print(f'WROTE={OUTPUT.name} bytes={OUTPUT.stat().st_size}')
