首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打印到Printing & ReportLab

打印到Printing & ReportLab
EN

Stack Overflow用户
提问于 2019-09-28 20:38:34
回答 1查看 597关注 0票数 1

我目前在Django中有这个视图,它完美地在html页面上呈现了一堆记录。

代码语言:javascript
复制
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
                             }
                  )

我还有这样的代码,它打印得很好,我可以很容易地插入一个变量。

代码语言:javascript
复制
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脚本。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-05 18:24:23

好的,所以我当然是个新手,学习,但是我所做的,我实际上是通过一个url把一个记录号码传递到我的函数中。我确信有一种更好(更安全)的方法,但我现在用超级用户装饰器来保护它。(出发地:django @login_required decorator for a superuser)

代码语言:javascript
复制
{% 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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58150122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档