def generate(self, op: OrderPosition, override_layout=None, override_background=None): Renderer._register_fonts() buffer = BytesIO() if override_background: bgf = default_storage.open(override_background.name, "rb") elif isinstance(self.layout.background, File) and self.layout.background.name: bgf = default_storage.open(self.layout.background.name, "rb") else: bgf = open(finders.find('pretixpresale/pdf/ticket_default_a4.pdf'), "rb") r = Renderer( self.request.event, override_layout or self.get_current_layout(), bgf, ) p = canvas.Canvas(buffer, pagesize=pagesizes.A4) r.draw_page(p, op.order, op) p.save() outbuffer = r.render_background(buffer, 'Ticket') return 'ticket.pdf', 'application/pdf', outbuffer.read()
def render_pdf(event, positions): Renderer._register_fonts() renderermap = { bi.item_id: _renderer(event, bi.layout) for bi in BadgeItem.objects.select_related('layout').filter( item__event=event) } try: default_renderer = _renderer(event, event.badge_layouts.get(default=True)) except BadgeLayout.DoesNotExist: default_renderer = None merger = PdfFileMerger() for op in positions: r = renderermap.get(op.item_id, default_renderer) if not r: continue with language(op.order.locale): buffer = BytesIO() p = canvas.Canvas(buffer, pagesize=pagesizes.A4) r.draw_page(p, op.order, op) p.save() outbuffer = r.render_background(buffer, 'Badge') merger.append(ContentFile(outbuffer.read())) outbuffer = BytesIO() merger.write(outbuffer) merger.close() outbuffer.seek(0) return outbuffer
def render_pdf(event, positions): Renderer._register_fonts() renderermap = { bi.item_id: _renderer(event, bi.layout) for bi in BadgeItem.objects.select_related('layout').filter(item__event=event) } try: default_renderer = _renderer(event, event.badge_layouts.get(default=True)) except BadgeLayout.DoesNotExist: default_renderer = None merger = PdfFileMerger() for op in positions: r = renderermap.get(op.item_id, default_renderer) if not r: continue with language(op.order.locale): buffer = BytesIO() p = canvas.Canvas(buffer, pagesize=pagesizes.A4) r.draw_page(p, op.order, op) p.save() outbuffer = r.render_background(buffer, 'Badge') merger.append(ContentFile(outbuffer.read())) outbuffer = BytesIO() merger.write(outbuffer) merger.close() outbuffer.seek(0) return outbuffer
def generate(self, op: OrderPosition, override_layout=None, override_background=None): Renderer._register_fonts() buffer = BytesIO() if override_background: bgf = default_storage.open(override_background.name, "rb") elif isinstance(self.layout.background, File) and self.layout.background.name: bgf = default_storage.open(self.layout.background.name, "rb") else: bgf = open(finders.find('pretixplugins/badges/badge_default_a6l.pdf'), "rb") r = Renderer( self.request.event, override_layout or self.get_current_layout(), bgf, ) p = canvas.Canvas(buffer, pagesize=pagesizes.A4) r.draw_page(p, op.order, op) p.save() outbuffer = r.render_background(buffer, 'Badge') return 'badge.pdf', 'application/pdf', outbuffer.read()
def _register_fonts(self): Renderer._register_fonts()
def render_pdf(event, positions, opt): from PyPDF2 import PdfFileReader, PdfFileWriter Renderer._register_fonts() renderermap = { bi.item_id: _renderer(event, bi.layout) for bi in BadgeItem.objects.select_related('layout').filter( item__event=event) } try: default_renderer = _renderer(event, event.badge_layouts.get(default=True)) except BadgeLayout.DoesNotExist: default_renderer = None output_pdf_writer = PdfFileWriter() any = False npp = opt['cols'] * opt['rows'] def render_page(positions): buffer = BytesIO() p = canvas.Canvas(buffer, pagesize=pagesizes.A4) for i, (op, r) in enumerate(positions): offsetx = opt['margins'][3] + (i % opt['cols']) * opt['offsets'][0] offsety = opt['margins'][2] + ( opt['rows'] - 1 - i // opt['cols']) * opt['offsets'][1] p.translate(offsetx, offsety) with language(op.order.locale, op.order.event.settings.region): r.draw_page(p, op.order, op, show_page=False) p.translate(-offsetx, -offsety) if opt['pagesize']: p.setPageSize(opt['pagesize']) p.showPage() p.save() buffer.seek(0) canvas_pdf_reader = PdfFileReader(buffer) empty_pdf_page = output_pdf_writer.addBlankPage( width=opt['pagesize'][0] if opt['pagesize'] else positions[0][1].bg_pdf.getPage(0).mediaBox[2], height=opt['pagesize'][1] if opt['pagesize'] else positions[0][1].bg_pdf.getPage(0).mediaBox[3], ) for i, (op, r) in enumerate(positions): bg_page = copy.copy(r.bg_pdf.getPage(0)) offsetx = opt['margins'][3] + (i % opt['cols']) * opt['offsets'][0] offsety = opt['margins'][2] + ( opt['rows'] - 1 - i // opt['cols']) * opt['offsets'][1] empty_pdf_page.mergeTranslatedPage(bg_page, tx=offsetx, ty=offsety) empty_pdf_page.mergePage(canvas_pdf_reader.getPage(0)) pagebuffer = [] outbuffer = BytesIO() for op in positions: r = renderermap.get(op.item_id, default_renderer) if not r: continue any = True pagebuffer.append((op, r)) if len(pagebuffer) == npp: render_page(pagebuffer) pagebuffer.clear() if pagebuffer: render_page(pagebuffer) output_pdf_writer.addMetadata({ '/Title': 'Badges', '/Creator': 'pretix', }) output_pdf_writer.write(outbuffer) outbuffer.seek(0) if not any: raise OrderError( _("None of the selected products is configured to print badges.")) return outbuffer
def _register_fonts(self): Renderer._register_fonts()