fix: improve PDF layout generation for Direct track

Key fixes:
- Skip large vector_graphics charts (>50% page coverage) that cover text
- Fix font fallback to use NotoSansSC for CJK support instead of Helvetica
- Improve translated table rendering with dynamic font sizing
- Add merged cell (row_span/col_span) support for reflow tables
- Skip text elements inside table bboxes to avoid duplication

Archive openspec proposal: fix-pdf-table-rendering

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
egg
2025-12-03 14:55:00 +08:00
parent 08adf3d01d
commit 1b5c7f39a8
5 changed files with 405 additions and 111 deletions

View File

@@ -0,0 +1,68 @@
# Design: Fix PDF Table Rendering
## Context
OCR track produces tables with:
- `cell_boxes`: Accurate pixel coordinates for each cell border
- `cells`: Content with row/col indices and row_span/col_span
- `embedded_images`: Images within table cells
Current implementations fail to use these correctly:
- **Reflow PDF**: Ignores merged cells, misaligns content
- **Translated Layout PDF**: Creates new Table object instead of using cell_boxes
## Goals / Non-Goals
**Goals:**
- Translated Layout PDF tables match untranslated Layout PDF quality
- Reflow PDF tables are readable and correctly structured
- Embedded images appear in both formats
**Non-Goals:**
- Perfect pixel-level replication of original table styling
- Support for complex nested tables
## Decisions
### Decision 1: Translated Layout PDF uses Layered Rendering
**What**: Draw cell borders using `cell_boxes`, then render translated text in each cell separately
**Why**: This matches the working approach in `_draw_table_with_cell_boxes()` for untranslated PDFs
```python
# Step 1: Draw borders using cell_boxes
for cell_box in cell_boxes:
pdf_canvas.rect(x, y, width, height)
# Step 2: Render text for each cell
for cell in cells:
cell_bbox = find_matching_cell_box(cell, cell_boxes)
draw_text_in_bbox(translated_content, cell_bbox)
```
### Decision 2: Reflow PDF uses ReportLab SPAN for merged cells
**What**: Apply `('SPAN', (col1, row1), (col2, row2))` style for merged cells
**Why**: ReportLab's Table natively supports merged cells via TableStyle
```python
# Build span commands from cell data
for cell in cells:
if cell.row_span > 1 or cell.col_span > 1:
spans.append(('SPAN',
(cell.col, cell.row),
(cell.col + cell.col_span - 1, cell.row + cell.row_span - 1)))
```
### Decision 3: Column widths from cell_boxes ratio
**What**: Calculate column widths proportionally from cell_boxes
**Why**: Preserves original table structure in reflow mode
## Risks / Trade-offs
| Risk | Mitigation |
|------|------------|
| Text overflow in translated cells | Shrink font (min 8pt) or truncate with ellipsis |
| cell_boxes not matching cells count | Fall back to equal-width columns |
| Complex merged cell patterns | Handle simple spans, skip complex patterns |
## Open Questions
- Should reflow PDF preserve exact column width ratios or allow ReportLab auto-sizing?
- How to handle cells with both text and images?

View File

@@ -0,0 +1,18 @@
# Change: Fix PDF Table Rendering Issues
## Why
OCR track PDF exports have significant table rendering problems:
1. **Reflow PDF** (both translated and untranslated): Tables are misaligned due to missing row_span/col_span support
2. **Translated Layout PDF**: Table borders disappear and text overlaps because it doesn't use the accurate `cell_boxes` positioning
## What Changes
- **Translated Layout PDF**: Adopt layered rendering approach (borders + text separately) using `cell_boxes` from metadata
- **Reflow PDF Tables**: Fix cell extraction and add basic merged cell support
- Ensure embedded images in tables are rendered correctly in all PDF formats
## Impact
- Affected specs: result-export
- Affected code:
- `backend/app/services/pdf_generator_service.py`
- `_draw_translated_table()` - needs complete rewrite
- `_create_reflow_table()` - needs merged cell support

View File

@@ -0,0 +1,41 @@
## MODIFIED Requirements
### Requirement: Translated Layout PDF Generation
The system SHALL generate layout-preserving PDFs with translated content that maintain accurate table structure.
#### Scenario: Table with accurate borders
- **GIVEN** an OCR result with tables containing `cell_boxes` metadata
- **WHEN** generating translated layout PDF
- **THEN** table cell borders SHALL be drawn at positions matching `cell_boxes`
- **AND** translated text SHALL be rendered within each cell's bounding box
#### Scenario: Text overflow handling
- **GIVEN** translated text longer than original text
- **WHEN** text exceeds cell bounding box
- **THEN** the system SHALL reduce font size (minimum 8pt) to fit content
- **OR** truncate with ellipsis if minimum font size is insufficient
#### Scenario: Embedded images in tables
- **GIVEN** a table with `embedded_images` in metadata
- **WHEN** generating translated layout PDF
- **THEN** images SHALL be rendered at their original positions within the table
### Requirement: Reflow PDF Table Rendering
The system SHALL generate reflow PDFs with properly structured tables including merged cell support.
#### Scenario: Basic table rendering
- **GIVEN** an OCR result with table cells containing `row`, `col`, `content`
- **WHEN** generating reflow PDF
- **THEN** cells SHALL be grouped by row and column indices
- **AND** table SHALL render with visible borders
#### Scenario: Merged cells support
- **GIVEN** table cells with `row_span` or `col_span` greater than 1
- **WHEN** generating reflow PDF
- **THEN** the system SHALL apply appropriate cell spanning
- **AND** merged cells SHALL display content without duplication
#### Scenario: Column width calculation
- **GIVEN** a table with `cell_boxes` metadata
- **WHEN** generating reflow PDF
- **THEN** column widths SHOULD be proportional to original cell widths

View File

@@ -0,0 +1,20 @@
# Tasks: Fix PDF Table Rendering
## 1. Translated Layout PDF - Table Fix (P0)
- [ ] 1.1 Refactor `_draw_translated_table()` to use layered rendering approach
- [ ] 1.2 Use `cell_boxes` from metadata for accurate border positioning
- [ ] 1.3 Render translated text within each cell's bbox using Paragraph with wordWrap
- [ ] 1.4 Handle text overflow (shrink font to minimum 8pt or truncate)
- [ ] 1.5 Draw embedded images at correct positions
## 2. Reflow PDF - Table Fix (P1)
- [ ] 2.1 Fix `_create_reflow_table()` cell extraction from content dict
- [ ] 2.2 Add row_span/col_span handling using ReportLab SPAN style
- [ ] 2.3 Calculate proportional column widths based on cell_boxes
- [ ] 2.4 Embed images in table cells instead of after table
## 3. Testing & Validation
- [ ] 3.1 Test with task 48b9e849-f6e3-462f-83a1-911ded701958 (has merged cells)
- [ ] 3.2 Verify translated layout PDF has visible borders
- [ ] 3.3 Verify reflow PDF tables align correctly
- [ ] 3.4 Verify embedded images appear in both formats