def royalty_statement_print(request, id): """ same as royalty statement detail, will output a PDF instead of html """ royalty = get_object_or_404(Royalty, id=id) # get the list of payables payables = royalty.get_payables(request.user) # let us prepare the pdf, set the mime type, filename response = HttpResponse(mimetype='application/pdf') filename = 'Royalty_Statement_%s_%s_%05d.pdf' % ( royalty.account.slug, royalty.statement_date.strftime('%b-%Y'), int(id)) response['Content-Disposition'] = 'attachment; filename=%s' % (filename) # we use reportlab to generate the pdf, we will use SimpleDocTemplate and # keep adding elements to it doc = SimpleDocTemplate(response) # basic container for the file elements = [] # container for the objects styles = getSampleStyleSheet() # get the sample styles width, height = A4 # default size is A4 # start adding elements in the following order: # the title: Athena Information Solutions Pvt. Ltd. # the royalty title # the royalty statement line level details and the adjustment (as a table) # Notes, if any # statement sent to and royalty details elements.append( Paragraph("Athena Information Solutions Pvt Ltd", styles['Title'])) elements.append( flowables.HRFlowable(width=width, spaceAfter=0.4 * inch, color=colors.black)) elements.append(Paragraph(royalty.__unicode__(), styles['Heading1'])) elements.append( Paragraph( "Please find below your statement showing the royalties due to you for the period shown", styles['Heading3'])) # for pdf - data needs to be list of lists - let us extract details from payables data = [] data.append([ 'Publication', 'Client', 'For period', 'Received On', 'Revenue', 'Exchange', 'Royalty', 'Royalty Due' ]) for i in payables: data.append([ i.publication, i.receivable.received_from, i.receivable.title, i.receivable.received_on.strftime('%b %d, %Y'), '%s %s' % (i.receivable.currency, intcomma(i.revenue)), i.get_exchange_rate(), '%d%s' % (i.revenue_share, u' %'), 'Rs %s' % intcomma(i.payable) ]) # add the adjustments etc to the same data list extra_text = "" if royalty.notes: extra_text = " (see notes below)" data.append([ 'Amount to be paid', '', '', '', '', '', '', 'Rs %s' % (intcomma(royalty.revenue)) ]) data.append([ 'Adjustment%s' % (extra_text), '', '', '', '', '', '', 'Rs %s' % (intcomma(royalty.adjustment)) ]) data.append([ 'Amount Payable', '', '', '', '', '', '', 'Rs %s' % (intcomma(royalty.amount_payable())) ]) line_items_table = Table(data) line_len = len(data) line_items_table.setStyle( TableStyle([ # header row - keep it in bold ('ALIGN', (1, 1), (-1, -1), 'CENTER'), ('LINEABOVE', (0, 0), (-1, 0), 1, colors.black), ('LINEBELOW', (0, 0), (-1, 0), 1, colors.black), ('FONT', (0, 0), (-1, 0), 'Times-Bold'), # make all numerical fields right aligned, exclude header row ('ALIGN', (4, 1), (-1, -1), 'RIGHT'), # draw the grid ('INNERGRID', (0, 0), (-1, line_len - 4), 0.25, colors.gray), ('BACKGROUND', (0, 0), (8, 0), colors.lavender), # make sure the last 3 rows span all the columns ('SPAN', (0, line_len - 3), (-2, line_len - 3)), ('SPAN', (0, line_len - 2), (-2, line_len - 2)), ('SPAN', (0, line_len - 1), (-2, line_len - 1)), # keep the Amount to be paid and Amount payable in bold ('FONT', (0, -3), (-1, -3), 'Times-Bold'), ('LINEABOVE', (0, -3), (-1, -3), 1, colors.black), ('FONT', (0, -1), (-1, -1), 'Times-Bold'), ('LINEABOVE', (0, -1), (-1, -1), 1, colors.black), ('LINEBELOW', (0, -1), (-1, -1), 1, colors.black), ])) # add table to the doc elements.append(line_items_table) elements.append(Spacer(0, 0.1 * inch)) # add the notes - if they exist if royalty.notes: elements.append(Paragraph("Notes: ", styles['Heading2'])) elements.append(Paragraph(royalty.notes, styles['Normal'])) elements.append(Spacer(0, 0.1 * inch)) # add the sent to details elements.append(Paragraph("Statement sent to: ", styles['Heading2'])) elements.append(Paragraph(royalty.get_sent_to(), styles['Normal'])) elements.append( Paragraph(royalty.get_sent_to_street_address(), styles['Normal'])) elements.append( Paragraph(royalty.get_sent_to_city_state(), styles['Normal'])) elements.append(Paragraph(royalty.get_sent_to_country(), styles['Normal'])) elements.append(Paragraph(royalty.get_sent_to_email(), styles['Normal'])) elements.append(Spacer(0, 0.1 * inch)) # add royalty department details elements.append(Paragraph("For any queries, contact: ", styles['Heading2'])) elements.append(Paragraph("Account Department", styles['Normal'])) elements.append(Paragraph("*****@*****.**", styles['Normal'])) elements.append(Paragraph("011-4057-6200 / 5200", styles['Normal'])) # all done - let us build the doc and finish! doc.build(elements) return response
if span_start < span_end: spans.append(("SPAN", (span_start, i), (span_end, i))) span_start = j span_end = j if span_start < span_end: spans.append(("SPAN", (span_start, i), (span_end, i))) params["style"] += spans return Table(table_list, **params) black_line = flowables.HRFlowable( width="92%", color="black", thickness=1, lineCap="round", spaceBefore=0, spaceAfter=1, hAlign="CENTER", vAlign="BOTTOM", dash=None, ) grey_line = flowables.HRFlowable( width="92%", thickness=1, lineCap="round", spaceBefore=1, spaceAfter=1, hAlign="CENTER", vAlign="BOTTOM", dash=None, )