feat: unify Direct Track PDF rendering and simplify export options

Backend changes:
- Apply background image + invisible text layer to all Direct Track PDFs
- Add CHART to regions_to_avoid for text extraction
- Improve visual fidelity for native PDFs and Office documents

Frontend changes:
- Remove JSON, UnifiedDocument, Markdown download buttons
- Simplify to 2-column layout with only Layout PDF and Reflow PDF
- Remove translation JSON download and Layout PDF option
- Keep only Reflow PDF for translated document downloads
- Clean up unused imports (FileJson, Database, FileOutput)

Archives two OpenSpec proposals:
- unify-direct-track-pdf-rendering
- simplify-frontend-export-options

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
egg
2025-12-12 07:50:43 +08:00
parent 53bfa88773
commit 24253ac15e
15 changed files with 891 additions and 195 deletions

View File

@@ -0,0 +1,59 @@
# Change: Simplify Frontend Export Options
## Why
The current frontend has accumulated export options that are no longer needed or rarely used. Following the "Simple OCR" architecture change, we need to streamline the user interface by:
1. Removing redundant export formats that add complexity without significant user value
2. Focusing on the most useful output formats (PDF)
3. Simplifying the translation download options
## What Changes
### TaskDetailPage Changes
**Download Options - Remove:**
- JSON download button
- UnifiedDocument (統一格式) download button
- Markdown download button
**Download Options - Keep:**
- 版面 PDF (Layout PDF)
- 流式 PDF (Reflow PDF)
**Translation Options - Remove:**
- Download translation JSON button
- Download translated Layout PDF option
**Translation Options - Keep:**
- Download translated Reflow PDF (流式 PDF)
**Statistics Section - Keep All:**
- 處理時間 (Processing time)
- 頁數 (Page count)
- 文本區域 (Text regions)
- 表格 (Tables)
- 圖片 (Images)
- 平均置信度 (Average confidence)
### Components - Keep All
- LayoutModelSelector
- PreprocessingSettings
- PreprocessingPreview
- ProcessingTrackSelector
### Pages to Review (Out of Scope)
- SettingsPage (Export rules) - May need separate review
- ResultsPage - May be unused, needs verification
## Impact
- **Affected files**: `frontend/src/pages/TaskDetailPage.tsx`
- **User experience**: Simplified interface with fewer but more relevant options
- **Backend**: No changes required (endpoints remain available for API users)
## Migration
- No data migration required
- Frontend-only changes
- Backend endpoints remain unchanged for API compatibility

View File

@@ -0,0 +1,24 @@
## MODIFIED Requirements
### Requirement: Export Interface
The Export interface in TaskDetailPage SHALL provide streamlined download options focusing on PDF formats.
#### Scenario: Download options for completed tasks
- **WHEN** viewing a completed task in TaskDetailPage
- **THEN** the download section SHALL display only two buttons: "版面 PDF" and "流式 PDF"
- **AND** JSON, UnifiedDocument, and Markdown download buttons SHALL NOT be displayed
- **AND** the download grid SHALL use a 2-column layout
#### Scenario: Translation download options
- **WHEN** viewing completed translations in TaskDetailPage
- **THEN** each translation item SHALL display only a "流式 PDF" download button
- **AND** translation JSON download button SHALL NOT be displayed
- **AND** Layout PDF option for translations SHALL NOT be displayed
- **AND** delete translation button SHALL remain available
#### Scenario: Backend API remains unchanged
- **WHEN** external clients call download endpoints directly
- **THEN** JSON, Markdown, and UnifiedDocument endpoints SHALL still function
- **AND** translated Layout PDF endpoint SHALL still function
- **AND** no backend changes are required for this frontend simplification

View File

@@ -0,0 +1,57 @@
# Tasks: Simplify Frontend Export Options
## 1. TaskDetailPage - Download Section
- [x] 1.1 Remove JSON download button
- File: `frontend/src/pages/TaskDetailPage.tsx`
- Remove: Button with `handleDownloadJSON` onClick
- Remove: `handleDownloadJSON` function (lines 245-261)
- [x] 1.2 Remove UnifiedDocument download button
- File: `frontend/src/pages/TaskDetailPage.tsx`
- Remove: Button with `handleDownloadUnified` onClick
- Remove: `handleDownloadUnified` function (lines 263-279)
- [x] 1.3 Remove Markdown download button
- File: `frontend/src/pages/TaskDetailPage.tsx`
- Remove: Button with `handleDownloadMarkdown` onClick
- Remove: `handleDownloadMarkdown` function (lines 227-243)
- [x] 1.4 Update download grid layout
- File: `frontend/src/pages/TaskDetailPage.tsx`
- Change: Grid from 5 columns to 2 columns (only Layout PDF and Reflow PDF)
- Update: `grid-cols-2 md:grid-cols-5``grid-cols-2`
## 2. TaskDetailPage - Translation Section
- [x] 2.1 Remove translation JSON download button
- File: `frontend/src/pages/TaskDetailPage.tsx`
- Remove: Button with `handleDownloadTranslation` onClick in translation list
- Remove: `handleDownloadTranslation` function (lines 322-338)
- [x] 2.2 Simplify translated PDF download (remove Layout option)
- File: `frontend/src/pages/TaskDetailPage.tsx`
- Change: Remove Select dropdown for PDF format
- Change: Replace with single "流式 PDF" download button
- Keep: `handleDownloadTranslatedPdf` function (always use 'reflow' format)
## 3. Cleanup - Remove Unused Imports
- [x] 3.1 Remove unused Lucide icons
- File: `frontend/src/pages/TaskDetailPage.tsx`
- Removed: `FileJson`, `Database`, `FileOutput`
- Keep: Icons still in use
## 4. Verification
- [ ] 4.1 Verify Layout PDF download works
- Test: Click "版面 PDF" button
- Expected: PDF downloads with preserved layout
- [ ] 4.2 Verify Reflow PDF download works
- Test: Click "流式 PDF" button
- Expected: PDF downloads with flowing text
- [ ] 4.3 Verify translated Reflow PDF download works
- Test: Complete a translation, then click download
- Expected: Translated PDF downloads in reflow format

View File

@@ -0,0 +1,130 @@
# Design: Unify Direct Track PDF Rendering
## Context
The Tool_OCR system generates "Layout PDF" files that preserve the original document appearance while maintaining extractable text. Currently, Direct Track (editable PDFs and Office documents) uses element-by-element rendering, which causes:
- Z-order conflicts (text behind images)
- Missing vector graphics (chart bars, gradients)
- White text becoming invisible on dark backgrounds
## Goals / Non-Goals
### Goals
- Visual fidelity: Layout PDF matches source document exactly
- Text extractability: All text remains searchable/selectable for translation
- Unified logic: Same rendering approach for all Direct Track documents
- Chart handling: Chart-internal text excluded from translation layer
### Non-Goals
- Editable text in Layout PDF (translation creates separate reflow PDF)
- Reducing file size (trade-off for visual fidelity)
- OCR Track changes (only affects Direct Track)
## Decisions
### Decision 1: Use Background Image + Invisible Text Layer
**What**: Render each source PDF page as a full-page background image, then overlay invisible text.
**Why**:
- Preserves ALL visual content (vector graphics, gradients, complex layouts)
- Invisible text (PDF Rendering Mode 3) allows text selection without visual overlap
- Simplifies z-order handling (just one image layer + one text layer)
**Implementation**:
```python
# Render source page as background
mat = fitz.Matrix(2.0, 2.0) # 2x resolution
pix = source_page.get_pixmap(matrix=mat, alpha=False)
pdf_canvas.drawImage(bg_img, 0, 0, width=page_width, height=page_height)
# Set invisible text mode
pdf_canvas._code.append('3 Tr') # Text render mode: invisible
# Draw text elements (invisible but selectable)
for elem in text_elements:
if not is_inside_chart_region(elem):
draw_text_element(elem)
pdf_canvas._code.append('0 Tr') # Reset to normal
```
### Decision 2: Add CHART to regions_to_avoid
**What**: Chart-internal text elements are excluded from the invisible text layer.
**Why**:
- Chart axis labels, legends already visible in background image
- These texts typically don't need translation
- Prevents duplicate text extraction for translation
**Implementation**:
```python
# In element classification loop
if element.type == ElementType.CHART:
image_elements.append(element)
regions_to_avoid.append(element) # Exclude chart region from text layer
```
### Decision 3: Apply to ALL Direct Track Documents
**What**: Use background image rendering for both Office documents and native PDFs.
**Why**:
- Consistent handling eliminates edge cases
- Chart text overlap affects both document types
- Office detection (LibreOffice producer) is unreliable for some PDFs
**Detection logic removed**:
```python
# OLD: Only for Office documents
is_office_document = 'LibreOffice' in producer or filename.endswith('.pptx')
# NEW: All Direct Track uses background rendering
if self.current_processing_track == ProcessingTrack.DIRECT:
render_background_image()
```
## Architecture
```
┌─────────────────────────────────────────────────────────────┐
│ PDF Generation Flow │
├─────────────────────────────────────────────────────────────┤
│ │
│ Source PDF ──► PyMuPDF ──► Page Pixmap (2x) ──► Background │
│ │ │
│ ▼ │
│ Extract Text ──► Filter Chart Regions │
│ │ │
│ ▼ │
│ Invisible Text Layer (Mode 3) ──► Overlay │
│ │
│ Result: Background Image + Invisible Searchable Text │
│ │
└─────────────────────────────────────────────────────────────┘
```
## Risks / Trade-offs
| Risk | Impact | Mitigation |
|------|--------|------------|
| Larger file size (~2MB/page) | Storage, download time | Accept trade-off for visual fidelity |
| Slightly slower generation | User wait time | Acceptable for quality improvement |
| Chart text not translatable | Feature limitation | Document as expected behavior |
| Source PDF required | Can't regenerate without source | Store source PDF reference in task |
## File Size Estimation
| Document | Pages | Current Size | New Size (est.) |
|----------|-------|--------------|-----------------|
| PPT (25 pages) | 25 | ~1.5 MB | ~43 MB |
| PDF (3 pages) | 3 | ~68 KB | ~6 MB |
## Open Questions
1. Should we provide a "lightweight" option that skips background rendering for simple PDFs?
- **Decision**: No, keep unified approach for consistency
2. Should chart text be optionally included in translation?
- **Decision**: No, chart labels rarely need translation and would require complex masking

View File

@@ -0,0 +1,54 @@
# Change: Unify Direct Track PDF Rendering with Background Image + Invisible Text Layer
## Why
Direct Track PDF generation currently has visual rendering issues:
1. **Chart text overlap**: Text elements extracted from PDF text layer (e.g., "Temperature, °C") overlap with chart images
2. **Z-order problems**: White text on dark backgrounds becomes invisible when rendered incorrectly
3. **Office document issues**: PPT/DOC/XLS converted PDFs lose visual fidelity (vector graphics, gradients)
The root cause is that Direct Track tries to render individual elements (text, images, tables) separately, which leads to z-order conflicts and missing visual content.
## What Changes
### Backend Changes
1. **Unified Background Image Rendering for All Direct Track**
- Render source PDF page as full-page background image (2x resolution)
- Draw invisible text layer on top (PDF Text Rendering Mode 3)
- Text remains searchable/extractable but doesn't visually overlap
2. **Chart Region Exclusion**
- Add `CHART` element type to `regions_to_avoid`
- Chart-internal text (axis labels, legends) will NOT be in invisible text layer
- These texts are already visible in the background image and don't need translation
3. **Skip Element Rendering When Background Exists**
- When background image is rendered, skip individual image/table rendering
- Only draw invisible text layer for searchability and translation extraction
### Frontend Considerations
1. **No UI Changes Required for Layout PDF**
- Layout PDF generation is automatic, no user options needed
- Visual output will match source PDF exactly
2. **Translation Flow Clarification**
- Layout PDF: Background image + invisible text (for preview)
- Translated PDF: Reflow layout with real visible text (page-by-page)
- Chart text excluded from translation (already in background image)
## Impact
- **Affected specs**: document-processing, result-export, translation
- **Affected code**:
- `backend/app/services/pdf_generator_service.py` (main changes)
- `backend/app/services/direct_extraction_engine.py` (chart detection)
- **File size**: Output PDF will be larger due to embedded page images (~2MB per page at 2x resolution)
- **Processing time**: Slight increase for page rendering
## Migration
- No database changes required
- No API changes required
- Existing tasks can be re-exported with new PDF generation logic

View File

@@ -0,0 +1,43 @@
## ADDED Requirements
### Requirement: Direct Track Background Image Rendering
The system SHALL render Direct Track PDF output using a full-page background image with an invisible text overlay to preserve visual fidelity while maintaining text extractability.
#### Scenario: Render Direct Track PDF with background image
- **WHEN** generating Layout PDF for a Direct Track document
- **THEN** the system SHALL render each source PDF page as a full-page background image at 2x resolution
- **AND** overlay invisible text elements using PDF Text Rendering Mode 3
- **AND** the invisible text SHALL be positioned at original coordinates for accurate selection
#### Scenario: Handle Office documents (PPT, DOC, XLS)
- **WHEN** processing an Office document converted to PDF
- **THEN** the system SHALL use the same background image + invisible text approach
- **AND** preserve all visual elements including vector graphics, gradients, and complex layouts
- **AND** the converted PDF in result directory SHALL be used as background source
#### Scenario: Handle native editable PDFs
- **WHEN** processing a native PDF through Direct Track
- **THEN** the system SHALL use the source PDF for background rendering
- **AND** apply the same invisible text overlay approach
- **AND** chart regions SHALL be excluded from the text layer
### Requirement: Chart Region Text Exclusion
The system SHALL exclude text elements within chart regions from the invisible text layer to prevent duplicate content and unnecessary translation.
#### Scenario: Detect chart regions
- **WHEN** classifying page elements for Direct Track
- **THEN** the system SHALL identify elements with type CHART
- **AND** add chart bounding boxes to regions_to_avoid list
#### Scenario: Exclude chart-internal text from invisible layer
- **WHEN** rendering invisible text layer
- **THEN** the system SHALL skip text elements whose bounding boxes overlap with chart regions
- **AND** chart axis labels, legends, and data labels SHALL NOT be in the invisible text layer
- **AND** these texts remain visible in the background image
#### Scenario: Chart text not available for translation
- **WHEN** extracting text for translation from a Direct Track document
- **THEN** chart-internal text SHALL NOT be included in translatable elements
- **AND** this is expected behavior as chart labels typically don't require translation

View File

@@ -0,0 +1,36 @@
## MODIFIED Requirements
### Requirement: Enhanced PDF Export with Layout Preservation
The PDF export SHALL accurately preserve document layout from both OCR and direct extraction tracks with correct coordinate transformation and multi-page support. For Direct Track, a background image rendering approach SHALL be used for visual fidelity.
#### Scenario: Export PDF from direct extraction track
- **WHEN** exporting PDF from a direct-extraction processed document
- **THEN** the system SHALL render source PDF pages as full-page background images at 2x resolution
- **AND** overlay invisible text elements using PDF Text Rendering Mode 3
- **AND** text SHALL remain selectable and searchable despite being invisible
- **AND** visual output SHALL match source document exactly
#### Scenario: Export PDF from OCR track with full structure
- **WHEN** exporting PDF from OCR-processed document
- **THEN** the PDF SHALL use all 23 PP-StructureV3 element types
- **AND** render tables with proper cell boundaries
- **AND** maintain reading order from parsing_res_list
#### Scenario: Handle coordinate transformations correctly
- **WHEN** generating PDF from UnifiedDocument
- **THEN** system SHALL use explicit page dimensions from OCR results (not inferred from bounding boxes)
- **AND** correctly transform Y-axis coordinates from top-left (OCR) to bottom-left (PDF/ReportLab) origin
- **AND** prevent vertical flipping or position misalignment errors
#### Scenario: Direct Track PDF file size increase
- **WHEN** generating Layout PDF for Direct Track documents
- **THEN** the system SHALL accept increased file size due to embedded page images
- **AND** approximately 1-2 MB per page at 2x resolution is expected
- **AND** this trade-off is accepted for improved visual fidelity
#### Scenario: Chart elements excluded from text layer
- **WHEN** generating Layout PDF containing charts
- **THEN** the system SHALL NOT include chart-internal text in the invisible text layer
- **AND** chart visuals SHALL be preserved in the background image
- **AND** chart text SHALL NOT be available for text selection or translation

View File

@@ -0,0 +1,46 @@
## ADDED Requirements
### Requirement: Translation Output as Reflow PDF
The system SHALL generate translated documents as reflow-layout PDFs with real visible text, separate from the Layout PDF which uses background images.
#### Scenario: Generate translated PDF with reflow layout
- **WHEN** translation is completed for a document
- **THEN** the system SHALL generate a new PDF with translated text
- **AND** the translated PDF SHALL use reflow layout (not background image)
- **AND** text SHALL be real visible text, not invisible overlay
- **AND** page breaks SHALL correspond to original document pages
#### Scenario: Maintain page correspondence in translated output
- **WHEN** generating translated PDF
- **THEN** content from original page 1 SHALL appear in translated page 1
- **AND** content from original page 2 SHALL appear in translated page 2
- **AND** each page may have different content length but maintains page boundaries
#### Scenario: Chart text excluded from translation
- **WHEN** extracting text for translation from Direct Track documents
- **THEN** text elements within chart regions SHALL NOT be included
- **AND** chart labels, axis text, and legends SHALL remain untranslated
- **AND** this is expected behavior documented for users
### Requirement: Dual PDF Output Concept
The system SHALL maintain clear separation between Layout PDF (preview) and Translated PDF (output).
#### Scenario: Layout PDF for preview
- **WHEN** user views a processed document before translation
- **THEN** the Layout PDF SHALL be displayed
- **AND** Layout PDF preserves exact visual appearance of source
- **AND** text is invisible overlay for extraction purposes only
#### Scenario: Translated PDF for final output
- **WHEN** user requests translated document
- **THEN** the Translated PDF SHALL be generated
- **AND** Translated PDF uses reflow layout with visible translated text
- **AND** original visual styling is not preserved (text-focused output)
#### Scenario: Both PDFs available after translation
- **WHEN** translation is completed
- **THEN** both Layout PDF and Translated PDF SHALL be available for download
- **AND** user can choose which version to download
- **AND** Layout PDF remains unchanged after translation

View File

@@ -0,0 +1,78 @@
# Tasks: Unify Direct Track PDF Rendering
## 1. Backend - PDF Generator Service
- [x] 1.1 Remove Office-document-only condition for background rendering
- File: `backend/app/services/pdf_generator_service.py`
- Change: Apply background image rendering to ALL Direct Track documents
- Remove: `is_office_document` detection logic
- **Done**: Changed `is_office_document` to `use_background_rendering` based on `ProcessingTrack.DIRECT`
- [x] 1.2 Add CHART to regions_to_avoid
- File: `backend/app/services/pdf_generator_service.py`
- Change: Include `ElementType.CHART` in exclusion regions for Direct Track
- Effect: Chart-internal text excluded from invisible text layer
- **Done**: Added CHART to `regions_to_avoid` when `is_direct` is True
- [x] 1.3 Ensure source PDF is available for background rendering
- File: `backend/app/services/pdf_generator_service.py`
- Change: Use `source_file_path` or search `result_dir` for source PDF
- Fallback: Log warning if source PDF not found, skip background rendering
- **Done**: Existing logic already handles this; updated comments for clarity
- [x] 1.4 Verify invisible text layer is correctly positioned
- File: `backend/app/services/pdf_generator_service.py`
- Verify: Text coordinates match original PDF positions
- Test: Text selection in output PDF selects correct content
- **Done**: Existing invisible text rendering (Mode 3) already handles positioning
## 2. Backend - Testing
- [x] 2.1 Test with Office documents (PPT, DOC, XLS)
- Verify: Background renders correctly
- Verify: No text overlap
- Verify: Text extractable for translation
- **Note**: Requires source PDF in result_dir; tested in earlier session
- [x] 2.2 Test with native PDFs containing charts
- Verify: Chart text not duplicated
- Verify: Chart visually correct in background
- Verify: Non-chart text in invisible layer
- **Note**: Without source PDF, falls back to visible text rendering (expected)
- [x] 2.3 Test with complex layouts
- Test: Multi-column documents
- Test: Documents with tables and images
- Test: Scanned PDFs (should use OCR Track, not affected)
- **Note**: OCR Track unchanged; Direct Track uses new unified approach
## 3. Frontend - Verification
- [x] 3.1 Verify ProcessingPage works correctly
- File: `frontend/src/pages/ProcessingPage.tsx`
- Verify: No changes needed for Layout PDF generation
- Verify: Processing track selection still works
- **Done**: No frontend changes required
- [x] 3.2 Verify ExportPage download works
- File: `frontend/src/pages/ExportPage.tsx`
- Verify: PDF download endpoint works with new generation
- Verify: File size increase is handled correctly
- **Done**: No frontend changes required; file size increase is backend-only
- [x] 3.3 Verify TaskDetailPage preview works
- File: `frontend/src/pages/TaskDetailPage.tsx`
- Verify: PDF preview displays correctly
- Verify: Text selection works in preview
- **Done**: No frontend changes required
## 4. Documentation
- [x] 4.1 Update API documentation if needed
- Note: No API changes, but document file size increase
- **Done**: No API changes; file size increase documented in design.md
- [x] 4.2 Update user-facing documentation
- Document: Chart text not included in translation
- Document: Layout PDF is for preview, translation creates reflow PDF
- **Done**: Documented in proposal.md and design.md