PDF не рендеринг - PullRequest
       9

PDF не рендеринг

0 голосов
/ 05 октября 2019

Я уверен, что это просто для кого-то, и любая помощь, когда я узнаю, как развиваться, очень ценится:

Эта функция отлично работает, когда я вызываю ее по динамической ссылке в шаблоне Django.

Однако он не отображается при вызове функции из другого скрипта, например, write_pdf_view(textobject=textobject, exam_record_number='123')

, но терминал сообщает, что через него передаются правильные значения. Где-то PDF не удается отрисовать ... у кого-нибудь есть идея и она будет достаточно любезна, чтобы обучить меня? Ура

def write_pdf_view(textobject, exam_record_number):
    rec = models.record.objects.filter(exam_record_number=exam_record_number)
    rec1 = models.record.objects.all().filter(exam_record_number=exam_record_number).values('exam_record')
    pat = models.record.objects.all().filter(exam_record_number=exam_record_number).values('patient_assign')
    print(rec)
    print(rec1)
    print(pat)
filename = "Blink Eye Care Record Number:"+exam_record_number
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
    # response['Content-Disposition'] = 'inline; filename="{}"'.format(filename)
    buffer = BytesIO()
    my_canvas = canvas.Canvas(buffer)
    # Below are f strings, with lists of dictionaries from the query set.

    # Create textobject 1
    textobject = my_canvas.beginText(30 * mm, 65 * mm)
    textobject.setFont('Times-Roman', 8)
    textobject.textLine(text="+ This text is written 30mm in and 65mm up from the mark")
    my_canvas.drawText(textobject)
    print(textobject.textLine())

    # Create textobject 2
    textobject = my_canvas.beginText(50 * mm, 100 * mm)
    textobject.textLine(text="+ This text is written 50mm in and 100mm up from the mark")
    textobject.textLine(text=f"Patient Number: {pat[0]['patient_assign']}")
    my_canvas.drawText(textobject)
    print(pat[0])


    # Create textobject 3
    textobject = my_canvas.beginText(100 * mm, 200 * mm)
    textobject.textLine(text="+ This text is written")
    textobject.textLine(text="100mm in and 200mm up from the mark")
    textobject.textLine(text=f"Exam Record Number: {rec[0]}")
    my_canvas.drawText(textobject)
    print(rec[0])

    # Create textobject 4
    textobject = my_canvas.beginText(140 * mm, 270 * mm)
    textobject.textLine(text="+ This text is written")
    textobject.textLine(text="+ 140mm in and 270mm up from the mark")
    textobject.textLine(text=f"Exam Record: {rec1[0]['exam_record']}")
    print(rec1[0])

    my_canvas.drawText(textobject)
    title = filename
    my_canvas.setTitle(title)
    print(title)

    my_canvas.showPage()
    my_canvas.save()
    pdf = buffer.getvalue()
    buffer.close()
    response.write(pdf)
    return response

...