gpt4 book ai didi

python - 打印为 PDF Django 和 ReportLab

转载 作者:太空宇宙 更新时间:2023-11-03 20:21:12 25 4
gpt4 key购买 nike

我目前在 Django 中有这个 View ,它可以完美地在我的 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 将记录号传递到我的函数中。我确信有更好(更安全)的方法,但我现在用 super 用户装饰器来保护它。 (从这里:django @login_required decorator for a superuser)

{% for rec in rec %}
<ul>
<b>Created: </b>{{ rec.date }} <b>Record:</b> &nbsp <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>

关于python - 打印为 PDF Django 和 ReportLab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58150122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com