【发布时间】:2022-08-04 12:43:38
【问题描述】:
我正在寻找为 PDF 写签名。我正在使用 JSignature 和 Reportlab。我的代码成功地将数据写入文件和数据库。我只是不知道如何将签名写入画布。有没有人成功地将签名传递到画布中?
先感谢您。
这是我的代码:
pdf文件
import io
from django.core.files.base import ContentFile
from reportlab.lib.units import inch
from reportlab.pdfgen import canvas
from reportlab.lib.utils import ImageReader
def create_pdf(parent):
# create a file-like buffer to receive PDF data
buffer = io.BytesIO()
# create the pdf object, using the buffer as its \"file\"
p = canvas.Canvas(buffer)
# create text
textobject = p.beginText()
# start text at top left of page
textobject.setTextOrigin(inch, 11*inch)
# set font and size
textobject.setFont(\"Helvetica-Bold\", 18)
textobject.textLine(\"My Document\")
textobject.textLine(\"\")
# write page 1
textobject.setFont(\"Helvetica\", 12)
p_name = f\'Name: {participant.first_name} {participant.middle_initial} {participant.last_name}\'
textobject.textLine(p_name)
sig = f\'Signature:\'
textobject.textLine(sig)
----insert signature here----
# write created text to canvas
p.drawText(textobject)
# close the pdf canvas
p.showPage()
p.save()
buffer.seek(0)
# get content of buffer
pdf_data = buffer.getvalue()
# save to django File object
file_data = ContentFile(pdf_data)
# name the file
file_data.name = f\'{participant.last_name}.pdf\'
#
participant.pdf = file_data
participant.save()
模型:
class Participant(models.Model):
first_name = models.CharField(max_length=50)
middle_initial = models.CharField(max_length=50, blank=True)
last_name = models.CharField(max_length=50, blank=True)
signature = JSignatureField()
pdf = models.FileField(blank=True, null=True)
标签: python django pdf reportlab jsignature