擅长:python、mysql、java
<p>如果您只想将生成的PDF作为http响应返回,那么下面的解决方案适用于我。我不知道你的模板。例如,我使用<code>{{ content.invoid_id }}</code>来访问模板中的上下文值。在</p>
<pre><code>def generate_pdf(request):
# Model data
students = Student.objects.all().order_by('last_name')
context = {
'invoice_id': 18001,
'street_name': 'Rue 76',
'postal_code': '3100',
'city': 'Washington',
'customer_name': 'John Cooper',
'customer_mail': 'customer@customer.at',
'amount': 1339.99,
'today': 'Today',
}
content = Context()
content.update(context)
template = loader.get_template('yourtemplate.html')
# render and return
html = template.render(context={'content':content}, request=request)
response = HttpResponse(content_type='application/pdf')
HTML(string=html, base_url=request.build_absolute_uri()).write_pdf(response)
return response
</code></pre>
<p>不需要临时文件。希望这有帮助!在</p>
<p>编辑:如果需要文件字节,可以这样做:</p>
^{pr2}$