如何将JSignature签名绘制到ReportLab生成的PDF画布中?
如何使用JSignature和ReportLab将签名绘制到PDF画布?
我正在尝试将签名写入PDF文件,使用的工具是JSignature和ReportLab。目前我的代码已成功将数据写入文件和数据库,但始终无法实现将签名绘制到PDF画布中。请问有人成功实现过将签名传入画布的操作吗?
相关代码
pdf.py
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()
Model
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)
内容的提问来源于stack exchange,提问作者cbirch
相关产品推荐
相关产品推荐

