Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions sbc_compassion/models/correspondence_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,41 @@ def fetch_image(self):
image = self.template_id.background or self._fetch_sponsor_pdf_image()
return image

def fetch_report_images(self):
"""
Used by the PDF report: images of the page, one per printed sheet.
GMC gives a sent S2B letter as one image of all its A4 pages stacked,
whatever the number of pages we hold: it is cut back into its pages.
"""
self.ensure_one()
image = self.fetch_image()
if not image or self.correspondence_id.direction != "Supporter To Beneficiary":
return [image]
try:
stacked = Image.open(BytesIO(base64.b64decode(image)))
stacked.load()
width, height = stacked.size
nb_pages = round(height / width * PAGE_WIDTH_PX / PAGE_HEIGHT_PX)
if nb_pages < 2:
return [image]
page_height = height // nb_pages
# The pages of our letters all have the same size: an even cut is
# exact, no need to look for the borders in the picture.
bounds = [(index * page_height, page_height) for index in range(nb_pages)]
images = []
for y_start, slice_height in bounds:
output = BytesIO()
stacked.crop((0, y_start, width, y_start + slice_height)).convert(
"RGB"
).save(output, "JPEG", quality=90)
images.append(base64.b64encode(output.getvalue()))
return images
except Exception:
_logger.warning(
"Could not cut the image of page %s", self.id, exc_info=True
)
return [image]

@api.model
def fetch_image_width(self, image):
"""
Expand Down
14 changes: 9 additions & 5 deletions sbc_compassion/reports/correspondence_report.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@
</style>
<t t-foreach="docs" t-as="letter">
<t t-foreach="letter.page_ids" t-as="page">
<!-- One sheet per image: a stacked image of GMC is cut into its pages -->
<t
t-foreach="page.fetch_report_images()"
t-as="background"
>
<div class="page" style="page-break-after:always;">
<t
t-set="background"
t-value="page.fetch_image()"
/>
<!-- Pages higher than A4 are shrunk to fit on the page,
the text boxes follow as they are in % of the image -->
<div
Expand All @@ -51,7 +52,9 @@
t-attf-src="data:image/jpeg;base64,{{background}}"
style="display: block; width: 100%;"
/>
<t t-if="letter.report_needs_overlay">
<t
t-if="letter.report_needs_overlay and background_first"
>
<div
class="overlay w-100 p-4"
style="position: absolute; top:0; left:0"
Expand Down Expand Up @@ -105,6 +108,7 @@
</t>
</div>
</div>
</t>
</t>
<t t-if="len(letter.page_ids) % 2 == 1">
<div class="page" style="page-break-after: always">
Expand Down
Loading