我目前在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脚本。
发布于 2019-10-05 18:24:23
好的,所以我当然是个新手,学习,但是我所做的,我实际上是通过一个url把一个记录号码传递到我的函数中。我确信有一种更好(更安全)的方法,但我现在用超级用户装饰器来保护它。(出发地:django @login_required decorator for a superuser)
{% for rec in rec %}
<ul>
<b>Created: </b>{{ rec.date }} <b>Record:</b>   <a href="{% url 'main:record_specific' rec.exam_record_number %}">{{ rec.exam_record_number }}</a>
<a href="{% url 'main:print_record' rec.exam_record_number %}" download style="float: right;">Print</a>
<br>
<p>{{ rec.exam_record |safe}}</p>
<hr>
</ul>https://stackoverflow.com/questions/58150122
复制相似问题