【问题标题】:Python, JSignature, and ReportLabPython、JSignature 和 ReportLab
【发布时间】: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


    【解决方案1】:

    对于那些对我如何能够实现此功能感兴趣的人。主要问题是将图像拉入 PDF 时将完全变黑。这是必需的:

    在您看来:

    • 使用Jsignature draw_signature 函数获取图像:
    rsr_image = draw_signature(signature)
    
    
    • 将签名保存为PNG,然后存储
    
                # save signature as png to prevent darkening, save to model
                rsr_file_name = str(new_parent.id)+'_rsr.png'
                buffer = BytesIO()
                rsr_image.save(buffer, 'PNG')
                new_parent.rsr_image.save(rsr_file_name, File(buffer))
    
    

    创建以下函数,以便...

    • 打开图像,为图像创建新背景,然后保存。
    
    def get_jpeg_image(new_parent):
        # open png image
        png_image = Image.open(new_parent.rsr_image)
        # create new image with 'RGB' mode which is compatible with jpeg,
        # with same size as old and with white(255,255,255) background
        bg = Image.new("RGB", png_image.size, (255, 255, 255))
        # paste old image pixels in new background
        bg.paste(png_image, png_image)
        # give image file name
        file_name_jpeg = str(new_parent.id)+'.jpg'
        bg.save(file_name_jpeg)
        return file_name_jpeg
    
    

    在创建 PDF 函数中引用该函数以将 PNG 转换为 JPG

    
    jpeg_image = get_jpeg_image(participant)
       
    
    

    希望这可以帮助某人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-18
      • 1970-01-01
      • 1970-01-01
      • 2017-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多