【发布时间】:2020-01-28 16:47:10
【问题描述】:
我目前在 Django 中有这个视图,它可以完美地在我的 html 页面上呈现一堆记录
def patient_page(request, id):
pat = models.patient.objects.get(pk=id) # Goes to patient models returns pk according to page
rec = models.record.objects.all().filter(patient_assign__pk=id).order_by('-date')
return render(request=request,
template_name = 'main/patient_page.html',
context = {"pats":pat,
"rec":rec
}
)
我也有这个打印完美的代码,我可以很容易地插入一个变量。
def write_pdf_view(textobject):
#Need to play with filename.
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'inline; filename="txt_obj.pdf"'
buffer = BytesIO()
my_canvas = canvas.Canvas(buffer)
# Create textobject(s)
textobject = my_canvas.beginText(30 * mm, 65 * mm)
textobject.setFont('Times-Roman', 8)
textobject.textLine(text="+ Hello This text is written 30mm in and 65mm up from the mark")
my_canvas.drawText(textobject)
title = "this is my title"
my_canvas.setTitle(title)
my_canvas.showPage()
my_canvas.save()
pdf = buffer.getvalue()
buffer.close()
response.write(pdf)
return response
我的问题,有没有人知道我如何渲染为 pdf 并打印为 PDF,即在 html 上的记录旁边我有一个打印按钮,该按钮当前运行我的打印到 pdf 脚本。
【问题讨论】: