使用xhtml2pdf在Django中生成PDF文件
我试图根据来自客户端的信息生成一个PDF文件。 我使用request.POST获取数据使用xhtml2pdf在Django中生成PDF文件,django,xhtml2pdf,Django,Xhtml2pdf,我试图根据来自客户端的信息生成一个PDF文件。 我使用request.POST获取数据 @csrf_exempt def create_pdf(request): if request.POST: context = {} data = json.loads(request.POST.get("users")) // I didn't use it. context["
@csrf_exempt
def create_pdf(request):
if request.POST:
context = {}
data = json.loads(request.POST.get("users")) // I didn't use it.
context["title"] = "AMIR"
render_to_pdf(
template_path="useres/users_template.html", context=context)
return HttpResponse(response)
为了呈现pdf文件,我在下面写下了代码:
def render_to_pdf(template_path, context={}):
# Create a Django response object, and specify content_type as pdf
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="report.pdf"'
# find the template and render it.
template = get_template(template_path)
html = template.render(context)
# create a pdf
pisa_status = pisa.CreatePDF(
html, dest=response)
# if error then show some funy view
if pisa_status.err:
return HttpResponse('We had some errors <pre>' + html + '</pre>')
return response
def render_to_pdf(模板路径,上下文={}):
#创建Django响应对象,并将内容类型指定为pdf
response=HttpResponse(content\u type='application/pdf')
响应['Content-Disposition']='附件;filename=“report.pdf”'
#找到模板并渲染它。
模板=获取模板(模板路径)
html=template.render(上下文)
#创建pdf
pisa_status=pisa.CreatePDF(
html,dest=响应)
#如果出现错误,则显示一些有趣的视图
如果pisa_status.err:
返回HttpResponse('我们有一些错误'+html+'')
返回响应
当我在创建\u pdf视图中调用渲染到\u pdf时,它不会返回pdf文件!
我该如何归还它