【问题标题】:Django JSignature Templated DocsDjango JSignature 模板文档
【发布时间】:2022-12-08 09:17:08
【问题描述】:

我正在使用 Django 4.1.3、templated_docs 和 Jsignature。我希望能够将jsignature生成的签名图片直接放到文档中。

我尝试了几乎所有使用 Draw_signature、templated_docs {% image %} 甚至 {{form.media}} {{form.field}} 的组合,在 Jsignature 中提到。

这里的任何见解都是有帮助的。

from templated_docs import fill_template
from templated_docs.http import FileResponse
from django.http import HttpResponseNotFound
from invoiceManager.models import Invoice
from jsignature.utils import draw_signature
def invoice_view(request, **kwargs):
    pk = kwargs.get('pk', None)
    if pk:
        # try:
        invoice = Invoice.objects.get(pk=pk)
        information = {
            'repairDate': invoice.repair_date,
            'claim': invoice.claim_authorization_number,
            'customerName': invoice.customer.name,
            'customerAddress': invoice.customer.address,
            'customerCityStateZip': f'{invoice.customer.city} {invoice.customer.state.abbreviation}, {invoice.customer.zip_code}',
            'customerPhone': invoice.customer.phone_number,
            'insuranceName': invoice.insurance.name,
            'policyNumber': f'policy Number: {invoice.policy_number}',
            'VIN': f'VIN: {invoice.vehicle_information.vin}',
            'YMM': f'{invoice.vehicle_information.year} {invoice.vehicle_information.make} {invoice.vehicle_information.model}',
            'customerSignature': draw_signature(invoice.signature),
            'technician': invoice.technician.name,
            'location': invoice.business_location.name,
            'submissionDate': invoice.invoice_submission_date,
            'amount':invoice.invoice_amount, 
            }
        repairLocations = {f'RL{x}':"Repair Made" for x in invoice.location_of_repairs.all().values_list('id', flat=True).order_by().distinct()}
        information.update(repairLocations)
        filename = fill_template(
            'docs/base_invoice.odt', information,
            output_format=".pdf")
        visible_filename = 'invoice{}'.format(".pdf")

        return FileResponse(filename, visible_filename)

【问题讨论】:

    标签: django jsignature


    【解决方案1】:

    我重写了图像标签以接受允许使用 jsignature.draw_siganture(, as_file=True) 的路径。创建一个可以被 PIL 引用然后重新绘制的临时文件——不太理想,但允许保持 jsignature 字段的纵横比/大小。

    from PIL import Image
    from django.utils.html import escape
    from django import template
    register = template.Library()
    
    PIXEL_TO_CM = 0.00846666
    
    
    class ImageNode(template.Node):
        def __init__(self, value):
            self.value = template.Variable(value)
    
        def render(self, context):
            self.value = self.value.resolve(context)
            images = context.dicts[0].setdefault('ootemplate_imgs', {})
            id = len(images)
            z_index = id + 3  # Magic
            photo = Image.open(self.value)
            width = photo.size[0] * PIXEL_TO_CM
            height = photo.size[0] * PIXEL_TO_CM
    
            return ('<draw:frame draw:style-name="gr%(z_index)s" '
                    'draw:name="Signature" '
                    'text:anchor-type="char" svg:width="%(width)fcm" '
                    'svg:height="%(height)fcm" draw:z-index="%(z_index)s">'
                    f'<draw:image xlink:href="{self.value}" '
                    'xlink:type="simple" xlink:show="embed" '
                    'xlink:actuate="onLoad"/></draw:frame>') % locals()
    
    
    @register.tag
    def template_image(parser, token):
        _, path = token.split_contents()
        return ImageNode(path)
    
    

    【讨论】:

      猜你喜欢
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      • 1970-01-01
      • 2015-01-14
      • 2011-01-13
      相关资源
      最近更新 更多