打印到 PDF Django 和 ReportLab
Printing to PDF Django & ReportLab
我目前在 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 脚本.
好的,所以我当然是新手,正在学习,但我所做的是,我基本上通过 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>
我目前在 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 脚本.
好的,所以我当然是新手,正在学习,但我所做的是,我基本上通过 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>