Fix PyPDF2 3.0+ compatibility issues in report_py3o

Add compatibility wrapper classes for PdfFileWriter/PdfFileReader
to support both PyPDF2 2.x and 3.x versions.

Changes:
- Add PyPDF2 compatibility classes in report_py3o/models/py3o_report.py
- Create comprehensive documentation in doc/PATCH_PDFWRITER.md
- Support for appendPagesFromReader method for PDF merging

Resolves PyPDF2.errors.DeprecationError: appendPagesFromReader is deprecated
and was removed in PyPDF2 3.0.0. Use append_pages_from_reader instead.

🤖 assisted by claude
This commit is contained in:
Ernad Husremovic 2025-09-02 19:09:50 +02:00
parent dd24c2f440
commit 7ed9174780
2 changed files with 135 additions and 2 deletions

View file

@ -0,0 +1,121 @@
# PyPDF2 Compatibility Patch - Report Py3o
## Overview
This patch addresses the PyPDF2 deprecation error in the report_py3o module when using PyPDF2 version 3.0.0 or higher. The original error was:
```
PyPDF2.errors.DeprecationError: appendPagesFromReader is deprecated and was removed in PyPDF2 3.0.0. Use append_pages_from_reader instead.
```
## Problem
In PyPDF2 3.0.0, several classes and methods were deprecated and removed:
- `PdfFileWriter``PdfWriter`
- `PdfFileReader``PdfReader`
- `appendPagesFromReader()``append_pages_from_reader()`
## Affected Functionality
The report_py3o module uses PyPDF2 for:
- Merging multiple PDF reports into single documents
- PDF concatenation and page manipulation
- LibreOffice/OpenOffice document conversion to PDF
- Advanced report generation workflows
## Solution
This patch provides backward compatibility by creating wrapper classes that:
1. Inherit from the new PyPDF2 classes (`PdfWriter`, `PdfReader`)
2. Provide the old method signatures as compatibility methods
3. Gracefully handle both old and new PyPDF2 versions
## Files Modified
### `report_py3o/models/py3o_report.py`
- Added compatibility import logic
- Created local compatibility classes with required method aliases:
- `PdfFileWriter.appendPagesFromReader()``PdfWriter.append_pages_from_reader()`
## Implementation Details
### Compatibility Import Pattern
```python
try:
from PyPDF2 import PdfWriter, PdfReader
# Create compatibility classes for PyPDF2 3.0+
class PdfFileWriter(PdfWriter):
def appendPagesFromReader(self, reader, after_page_append=None):
return self.append_pages_from_reader(reader, after_page_append)
class PdfFileReader(PdfReader):
pass
except ImportError:
try:
from PyPDF2 import PdfFileReader, PdfFileWriter
except ImportError:
logger.debug("Cannot import PyPDF2")
```
## Usage Example
The module allows generating and merging py3o reports:
```python
# Generate multiple reports
reports_path = [report1.pdf, report2.pdf, report3.pdf]
# Merge using appendPagesFromReader (now compatible)
writer = PdfFileWriter()
for path in reports_path:
reader = PdfFileReader(path)
writer.appendPagesFromReader(reader)
```
## Testing
The patch has been tested with:
- PyPDF2 3.0.0+ (new API)
- PyPDF2 2.x (old API via fallback)
- PDF merge functionality
- Multiple report concatenation
- LibreOffice document conversion workflows
## Branch Information
- **Branch**: `pdfwrite`
- **Based on**: Current master branch
- **Type**: Compatibility patch
- **Impact**: Backward compatible - no breaking changes
## Author
- **Developer**: Ernad Husremović (hernad@bring.out.ba)
- **Company**: bring.out.doo Sarajevo
- **Date**: 2025-09-02
## Related Issues
This patch resolves the PyPDF2 deprecation error encountered in:
- Py3o report merging
- Multi-document PDF generation
- LibreOffice integration workflows
## Installation
This patch is automatically applied when using the `pdfwrite` branch. No additional installation steps required.
## Dependencies
- Compatible with py3o template engine
- Works with LibreOffice/OpenOffice document conversion
- Maintains existing py3o functionality
## Future Considerations
While this patch provides immediate compatibility, consider:
1. Eventually migrating to the new PyPDF2 API directly
2. Testing merge functionality with future PyPDF2 versions
3. Evaluating performance impact of compatibility layer