def create_pdf_response(document): # Create Buffer buff = BytesIO() # Build template template = BriefTemplate(buff, document) template.build(document.content) # Create Response and close Buffer response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = \ 'attachment; filename="uebermittlungssperre.pdf"' pdf_response = buff.getvalue() buff.close() response.write(pdf_response) return response
def pdf_response(self): # create document structure document = Document( sender = self.sender_address, recipient = self.recipient_address, date = datetime.datetime.now().strftime("%d.%m.%Y"), content = self._content() ) # create response instance response = HttpResponse(content_type='application/pdf') filename = "dsg-2000-auskunft-" + self.sender_name.replace(' ','-').lower() + ".pdf" response['Content-Disposition'] = \ 'attachment; filename="' + filename + '"' # build letter template buff = BytesIO() template = BriefTemplate(buff, document) template.build(document.content) pdf_response = buff.getvalue() buff.close() response.write(pdf_response) return response
def invoice_pdf(request, number, correction=False): invoice = get_object_or_404(Invoice, number=number) if correction: invoice = invoice.correction from reportlab.lib.units import mm from reportlab.platypus import Paragraph from reportlab.platypus.flowables import Spacer from reportlab.platypus.flowables import KeepTogether from dinbrief.document import Document from dinbrief.invoice import ItemTable, TotalTable from dinbrief.styles import styles from dinbrief.template import BriefTemplate with trans_override(invoice.language): response = HttpResponse(content_type='application/pdf') if 'download' in request.GET: filename = '%s.pdf' % invoice.number response[ 'Content-Disposition'] = 'attachment; filename=%s' % filename if invoice.type == Invoice.TYPE_INVOICE: if callable(INVOICE_TERMS): terms = INVOICE_TERMS(invoice) else: terms = [ Paragraph(term, styles['Terms']) for term in INVOICE_TERMS ] else: terms = [] template = BriefTemplate() document = Document( sender=invoice.sender_lines, recipient=invoice.recipient_lines, date=date_format(invoice.created, 'SHORT_DATE_FORMAT'), content=[ Paragraph( '%s %s' % (invoice.get_type_display() if not correction else gettext(u'Correction of invoice'), invoice.number), styles['Subject']), Spacer(template.CONTENT_WIDTH, 2 * mm), ItemTable(template, invoice), KeepTogether(TotalTable(template, invoice)), Spacer(template.CONTENT_WIDTH, 10 * mm), ] + terms) if settings.SHARK['INVOICE']['BACKGROUND']: with tempfile.TemporaryFile() as tmp: # Create content in a temporary file template.render(document, tmp) # Combine background with the content writer = PdfFileWriter() content = PdfFileReader(tmp) info_dict = writer._info.getObject() info_dict.update(content.getDocumentInfo()) first_bg = PdfFileReader( open(settings.SHARK['INVOICE']['BACKGROUND']['FIRST_PAGE'], 'rb')) later_bg = PdfFileReader( open(settings.SHARK['INVOICE']['BACKGROUND']['LATER_PAGE'], 'rb')) bg = [first_bg.getPage(0), later_bg.getPage(0)] for i, page in enumerate(content.pages): page.mergePage(bg[min(i, 1)]) page.compressContentStreams() writer.addPage(page) writer.write(response) else: # Render content directly to the HTTP response object if no # background images are configured. template.render(document, response) return response