mirror of
https://github.com/bringout/oca-report.git
synced 2026-04-18 03:22:00 +02:00
- Added explicit page copying after cloneReaderDocumentRoot() calls
- Renamed PATCH_PDFWRITER.md to PATCH_PYPDF2_PDFWRITER.md
- Prevents 327-byte empty PDFs in PyPDF2 3.x
🤖 assisted by claude
3.4 KiB
3.4 KiB
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→PdfWriterPdfFileReader→PdfReaderappendPagesFromReader()→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:
- Inherit from the new PyPDF2 classes (
PdfWriter,PdfReader) - Provide the old method signatures as compatibility methods
- 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
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:
# 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:
- Eventually migrating to the new PyPDF2 API directly
- Testing merge functionality with future PyPDF2 versions
- Evaluating performance impact of compatibility layer