我正在使用 trml2pdf 并且它正在成功运行,我在这里发布我的代码,以便您查看
from django.template.loader import get_template
from django.template.context import Context
import trml2pdf
def invoicepdf(request):
template = get_template('invoice.rml')
context = Context({
'name': "XXXXXXXXXX",
'date':_date,
'subtotal':1909201,
'total': 345789
})
xmlstring = template.render(context)
pdfstr = trml2pdf.parseString(xmlstring)
response = HttpResponse(mimetype='application/pdf')
response.write(pdfstr)
response['Content-Disposition'] = 'attachment; filename=invoice.pdf'
return response
RML 代码
发票.rml
<!-- This is very Invoice RML template for illustrative purposes. -->
<!-- A basic RML template has three sections. The 'template' -->
<!-- section is where you define fixed position elements, along -->
<!-- with 'frames' containing flowing text. The 'stylesheet' -->
<!-- section contains re-useable text style definitions. The -->
<!-- 'story' section contains the text and graphics which fill -->
<!-- up the frames defined in the template section. -->
<!-- For more information, please read the documentation at -->
<!-- http://www.reportlab.com/software/documentation/ -->
<!DOCTYPE document SYSTEM "rml.dtd">
<document filename="invoice.pdf">
<!-- left margin -->
<template title="Invoices" author="Atul jain" allowSplitting="20">
<pageTemplate id="first">
<frame id="first" x1="34.0" y1="28.0" width="530" height="786"/>
<pageGraphics>
<lines>0.3cm 27.0cm 20cm 27.0cm</lines>
</pageGraphics>
</pageTemplate>
</template>
<story>
<para>
<font color="white"> </font>
</para>
<para><b>Name:- {{name}} </b></para>
<para><b>Date:- {{date}} </b></para>
<blockTable colWidths="385.0,60.0,85.0">
<tr>
<td>
<para>
<font color="white"> </font>
</para>
</td>
<td>
<para>Net Total:</para>
</td>
<td>
<para>{{subtotal}} INR;</para>
</td>
</tr>
<tr>
<td>
<para>
<font color="white"> </font>
</para>
</td>
<td>
<para><b>Total:</b></para>
</td>
<td>
<para><b>{{total}} INR;</b></para>
</td>
</tr>
</blockTable>
</story>
</document>