# PDF Viewer for BIgo Reader

The PDF viewer provides a simple interface for viewing book PDFs alongside the text content.

## Features

- **Navigation**: First, Previous, Next, Last page buttons
- **Zoom**: Zoom in/out with +/- buttons or Fit to width
- **Scrolling**: Horizontal and vertical scrollbars for large pages
- **Keyboard shortcut**: F2 to open PDF from main window

## PDF Backends

The PDF viewer supports two backends (auto-detected):

1. **PyMuPDF (fitz)** - Preferred, faster rendering
2. **pypdfium2** - Alternative backend

At least one backend must be installed:

```bash
pip install PyMuPDF
# or
pip install pypdfium2
```

## Usage

### Opening PDF

1. Open a book in the main reader window
2. Press **F2** or click **📄 PDF** button
3. Or use menu: **View → View PDF**

The PDF button/menu item is only enabled if:
- The book has a `PDF_Path` field in the database
- The PDF file exists at that path

### Navigation

- **<< First**: Go to first page
- **< Prev**: Previous page
- **> Next**: Next page
- **>> Last**: Go to last page
- **Page display**: Shows current page / total pages

### Zoom Controls

- **- button**: Zoom out (minimum 20%)
- **+ button**: Zoom in (maximum 400%)
- **Fit button**: Fit page width to window

## Window Management

- Only one PDF viewer window per main window
- Clicking PDF button when viewer is open brings it to front
- Viewer window is independent and can be moved/resized
- Default size: 700x900 pixels

## Implementation Details

### Class: PDFViewerWindow

Located in: `bigo_reader/pdf/pdf_viewer.py`

**Constructor parameters:**
- `parent`: MainReaderWindow instance
- `pdf_path`: Path to PDF file
- `book_name`: Display name for window title

**Key methods:**
- `goto_page(page_num)`: Jump to specific page (1-indexed)
- `_render_page()`: Render current page to canvas
- `_zoom_in()`, `_zoom_out()`, `_fit_width()`: Zoom controls
- `_first_page()`, `_prev_page()`, `_next_page()`, `_last_page()`: Navigation

### Backend Selection

The viewer automatically selects the first available backend:

1. Try `import fitz` (PyMuPDF)
2. If not available, try `import pypdfium2`
3. If neither available, show error and close

### Rendering Process

**PyMuPDF (fitz):**
```python
page = doc[page_index]
mat = fitz.Matrix(scale, scale)
pix = page.get_pixmap(matrix=mat)
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
```

**pypdfium2:**
```python
page = doc[page_index]
scale_factor = scale * 2  # pypdfium2 needs higher scale
pil_image = page.render(scale=scale_factor).to_pil()
```

Both backends produce PIL Images which are converted to `ImageTk.PhotoImage` for display.

## Integration with Main Window

The PDF viewer integrates with `MainReaderWindow`:

1. **Book loading**: Checks for `PDF_Path` field, enables/disables PDF button
2. **Reference**: `self.pdf_viewer` holds current viewer instance
3. **Opening**: `_open_pdf()` method creates viewer or brings to front
4. **Cleanup**: Viewer sets `parent.pdf_viewer = None` on close

## Future Enhancements

- [ ] Synchronize PDF page with text page
- [ ] Bookmark PDF pages
- [ ] Search in PDF
- [ ] Extract text from PDF
- [ ] Print PDF pages
- [ ] Annotations and highlights
- [ ] Thumbnail navigation

## Error Handling

The viewer handles several error conditions:

- **No PDF backends**: Shows error and closes immediately
- **PIL not installed**: Shows error and closes
- **PDF file not found**: Shows error message
- **PDF_Path empty**: Shows warning message
- **PDF corrupted**: Shows exception details

## See Also

- [Main Window](../ui/main_window.py) - Integration point
- [Database Manager](../data/db_manager.py) - PDF_Path retrieval
- [bigo_book/bigo_book_reader.py](../../bigo_book/bigo_book_reader.py) - Original BookViewer
