From 0f5af4b9907e5df5a0fc47054d1ef1dd2612b3a6 Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Fri, 1 Jun 2018 13:32:47 +0200 Subject: [PATCH] Automatically shorten event name on invoice --- src/pretix/base/invoice.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/pretix/base/invoice.py b/src/pretix/base/invoice.py index bdc9836de..2a851eda0 100644 --- a/src/pretix/base/invoice.py +++ b/src/pretix/base/invoice.py @@ -282,19 +282,30 @@ class ClassicInvoiceRenderer(BaseReportlabInvoiceRenderer): preserveAspectRatio=True, anchor='n', mask='auto') + def shorten(txt): + txt = str(txt) + p = Paragraph(txt.strip().replace('\n', '
\n'), style=self.stylesheet['Normal']) + p_size = p.wrap(65 * mm, 50 * mm) + + while p_size[1] > 2 * self.stylesheet['Normal'].leading: + txt = ' '.join(txt.replace('…', '').split()[:-1]) + '…' + p = Paragraph(txt.strip().replace('\n', '
\n'), style=self.stylesheet['Normal']) + p_size = p.wrap(65 * mm, 50 * mm) + return txt + if not self.invoice.event.has_subevents: if self.invoice.event.settings.show_date_to: p_str = ( - str(self.invoice.event.name) + '\n' + pgettext('invoice', '{from_date}\nuntil {to_date}').format( + shorten(self.invoice.event.name) + '\n' + pgettext('invoice', '{from_date}\nuntil {to_date}').format( from_date=self.invoice.event.get_date_from_display(), to_date=self.invoice.event.get_date_to_display()) ) else: p_str = ( - str(self.invoice.event.name) + '\n' + self.invoice.event.get_date_from_display() + shorten(self.invoice.event.name) + '\n' + self.invoice.event.get_date_from_display() ) else: - p_str = str(self.invoice.event.name) + p_str = shorten(self.invoice.event.name) p = Paragraph(p_str.strip().replace('\n', '
\n'), style=self.stylesheet['Normal']) p.wrapOn(canvas, 65 * mm, 50 * mm)