Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
domdinicola committed May 2, 2024
2 parents f2e56bf + ca65631 commit a7e9684
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/hope_country_report/apps/power_query/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,16 @@ def insert_external_image(self, document: fitz.Document, field_name: str, image_
Loads, resizes, and inserts an external image into the specified field.
"""
rect, page_index = self.get_field_rect(document, field_name)
if rect and page_index is not None:
if rect is None or page_index is None:
logger.error(f"No valid rectangle or page index found for field {field_name}. Cannot insert image.")
return
page = document[page_index]
try:
image_stream = self.load_image_from_blob_storage(image_path)
image = Image.open(image_stream)
output_stream = io.BytesIO()
image.save(output_stream, format="JPEG", quality=75)
output_stream.seek(0)
page = document[page_index]
for widget in page.widgets():
if widget.field_name == field_name:
page.delete_widget(widget)
Expand All @@ -297,6 +300,12 @@ def insert_external_image(self, document: fitz.Document, field_name: str, image_
if image.height < image.width:
rotate = 270
page.insert_image(rect, stream=output_stream, keep_proportion=False, rotate=rotate)
except Exception as e:
logger.exception(e)
capture_exception(e)
page.insert_textbox(
rect, "Image unreadable", color=(1, 0, 0), fontsize=11, fontname="helv", align=fitz.TEXT_ALIGN_CENTER
)

def is_image_field(self, annot: ArrayObject) -> bool:
"""
Expand Down

0 comments on commit a7e9684

Please sign in to comment.