fix: make pypdfium plugin optional with Ghostscript fallback
- Remove check_options hook from pypdfium that raised error when pypdfium2 wasn't installed - Return None from pypdfium's rasterize_pdf_page when pypdfium2 is unavailable, allowing the hook to fall through - Restore Ghostscript's rasterize_pdf_page hook as fallback This allows OCRmyPDF to work without pypdfium2 installed, using Ghostscript for rasterization as before. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.5
parent
cf3fb6e89b
commit
938ce8e285
@@ -117,31 +117,31 @@ def check_options(options):
|
||||
)
|
||||
|
||||
|
||||
# @hookimpl
|
||||
# def rasterize_pdf_page(
|
||||
# input_file,
|
||||
# output_file,
|
||||
# raster_device,
|
||||
# raster_dpi,
|
||||
# pageno,
|
||||
# page_dpi,
|
||||
# rotation,
|
||||
# filter_vector,
|
||||
# stop_on_soft_error,
|
||||
# ):
|
||||
# """Rasterize a single page of a PDF file using Ghostscript."""
|
||||
# ghostscript.rasterize_pdf(
|
||||
# input_file,
|
||||
# output_file,
|
||||
# raster_device=raster_device,
|
||||
# raster_dpi=raster_dpi,
|
||||
# pageno=pageno,
|
||||
# page_dpi=page_dpi,
|
||||
# rotation=rotation,
|
||||
# filter_vector=filter_vector,
|
||||
# stop_on_error=stop_on_soft_error,
|
||||
# )
|
||||
# return output_file
|
||||
@hookimpl
|
||||
def rasterize_pdf_page(
|
||||
input_file,
|
||||
output_file,
|
||||
raster_device,
|
||||
raster_dpi,
|
||||
pageno,
|
||||
page_dpi,
|
||||
rotation,
|
||||
filter_vector,
|
||||
stop_on_soft_error,
|
||||
):
|
||||
"""Rasterize a single page of a PDF file using Ghostscript."""
|
||||
ghostscript.rasterize_pdf(
|
||||
input_file,
|
||||
output_file,
|
||||
raster_device=raster_device,
|
||||
raster_dpi=raster_dpi,
|
||||
pageno=pageno,
|
||||
page_dpi=page_dpi,
|
||||
rotation=rotation,
|
||||
filter_vector=filter_vector,
|
||||
stop_on_error=stop_on_soft_error,
|
||||
)
|
||||
return output_file
|
||||
|
||||
|
||||
@hookimpl
|
||||
|
||||
@@ -13,23 +13,18 @@ except ImportError:
|
||||
pdfium = None
|
||||
|
||||
from ocrmypdf import hookimpl
|
||||
from ocrmypdf.exceptions import MissingDependencyError
|
||||
from ocrmypdf.helpers import Resolution
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@hookimpl
|
||||
def check_options(options):
|
||||
"""Check that pypdfium2 is available."""
|
||||
if pdfium is None:
|
||||
raise MissingDependencyError("pypdfium2 is required for this plugin.")
|
||||
# Note: No check_options hook - pypdfium is optional. If pypdfium2 is not
|
||||
# installed, the rasterize_pdf_page hook returns None and Ghostscript is used.
|
||||
|
||||
|
||||
def _open_pdf_document(input_file: Path):
|
||||
"""Open a PDF document using pypdfium2."""
|
||||
if pdfium is None:
|
||||
raise MissingDependencyError("pypdfium2 is not available")
|
||||
assert pdfium is not None, "pypdfium2 must be available to call this function"
|
||||
return pdfium.PdfDocument(input_file)
|
||||
|
||||
|
||||
@@ -128,8 +123,14 @@ def rasterize_pdf_page(
|
||||
rotation: int | None,
|
||||
filter_vector: bool,
|
||||
stop_on_soft_error: bool,
|
||||
) -> Path:
|
||||
"""Rasterize a single page of a PDF file using pypdfium2."""
|
||||
) -> Path | None:
|
||||
"""Rasterize a single page of a PDF file using pypdfium2.
|
||||
|
||||
Returns None if pypdfium2 is not available, allowing Ghostscript to be used.
|
||||
"""
|
||||
if pdfium is None:
|
||||
return None # Fall back to Ghostscript
|
||||
|
||||
# Open the PDF document
|
||||
pdf = _open_pdf_document(input_file)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user