我在应用程序中具有用于pdf呈现的简单代码,我尝试添加pdf附件。所以我读了docs and find solution。
附件(列表)–生成的PDF的其他文件附件的列表 文档或
x-axis
。列表的元素是None
对象,文件名, URL 或类似文件的对象。
所以我将附件添加到Attachment
方法中:
write_pdf
我尝试2种变体:将URL添加到附件中或使用URL添加 def render_to_pdf(self, request):
context_dict = {
'application': self,
}
context_dict['pagesize'] = 'A4'
context_dict['documents'] = dict()
urls = []
documents = self.user.documents.all()
for document in documents:
context_dict['documents'][str(document.id)] = dict()
context_dict['documents'][str(document.id)]['signed_name'] = document.signed_file_name
context_dict['documents'][str(document.id)]['name'] = document.file_name
attachment = Attachment(url=BotoGateway().generate_cloudfront_url(document.signed_file_name))
urls.append(attachment)
html_template = get_template('application_form_pdf.html')
rendered_html = html_template.render(context_dict).encode(encoding="UTF-8")
pdf_file = HTML(string=rendered_html, base_url=request.build_absolute_uri()).write_pdf(attachments=urls)
http_response = HttpResponse(pdf_file, content_type='application/pdf')
http_response['Content-Disposition'] = 'filename="report.pdf"'
return http_response
对象。但是没有任何效果。我得到正确的文件名,但是当我尝试打开附件时,我得到了0个字节的文件。我调试了我的代码,并且网址正确。