From 045edc7cecb1bbe915fac5b9a741fe100d4935d2 Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Tue, 21 Jan 2025 15:03:43 +0100 Subject: [PATCH] Invoice rendering: Remove transparency from logos (Z#23179391) (#4762) * Invoice rendering: Remove logos from transparency * Add comment --- src/pretix/base/invoice.py | 9 +++++++++ src/pretix/helpers/reportlab.py | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/src/pretix/base/invoice.py b/src/pretix/base/invoice.py index 5a25a3e23..2dbd6c65a 100644 --- a/src/pretix/base/invoice.py +++ b/src/pretix/base/invoice.py @@ -388,6 +388,15 @@ class ClassicInvoiceRenderer(BaseReportlabInvoiceRenderer): except: logger.exception("Can not resize image") pass + try: + # Valid ZUGFeRD invoices must be compliant with PDF/A-3. pretix-zugferd ensures this by passing them + # through ghost script. Unfortunately, if the logo contains transparency, this will still fail. + # I was unable to figure out a way to fix this in GhostScript, so the easy fix is to remove the + # transparency, as our invoices always have a white background anyways. + ir.remove_transparency() + except: + logger.exception("Can not remove transparency from logo") + pass canvas.drawImage(ir, self.logo_left, self.pagesize[1] - self.logo_height - self.logo_top, diff --git a/src/pretix/helpers/reportlab.py b/src/pretix/helpers/reportlab.py index b2af459c6..275d0205c 100644 --- a/src/pretix/helpers/reportlab.py +++ b/src/pretix/helpers/reportlab.py @@ -39,6 +39,12 @@ class ThumbnailingImageReader(ImageReader): self._data = None return width, height + def remove_transparency(self, background_color="WHITE"): + if "A" in self._image.mode: + new_image = Image.new("RGBA", self._image.size, background_color) + new_image.paste(self._image, mask=self._image) + self._image = new_image.convert("RGB") + def _jpeg_fh(self): # Bypass a reportlab-internal optimization that falls back to the original # file handle if the file is a JPEG, and therefore does not respect the