编程语言
首页 > 编程语言> > python-将html模板转换为带有图像的pdf

python-将html模板转换为带有图像的pdf

作者:互联网

我正在使用django并尝试呈现pdf发票,pdf成功生成,但是图像不在pdf中.

实际上,我想添加条形码图像,因此我动态生成了.svg文件,但不知道如何将其放入pdf.

这是我在做的..

from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template

from xhtml2pdf import pisa

def render_to_pdf(template_src, context_dict={}):
    template = get_template(template_src)
    html  = template.render(context_dict)
    result = BytesIO()
    pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return None

html-

  <body>
    <div class="container-fluid">
      <div class="row">
        <div class="col-6">
          <div class="row">
            <div class="col-12">
              <span>
                {{ package_no_barcode }}
                <br>
                {{ package.package_no }}
                ....

查看-

class GenerateInvoicePdf(View):
    def get(self, request, *args, **kwargs):
        package = Package.objects.get(package_no=request.GET.get('package'))
        address = package.purchase.order_product.delivery_location
        CODE128 = barcode.get_barcode_class('code128')
        package_no_barcode = CODE128(u'{pkg_no}'.format(pkg_no=package.package_no))
        if request.user.display_username == seller.display_username:
            data = {
                 'package':package,
                 'address':address,
                 'package_no_barcode':package_no_barcode,
             }
            pdf = render_to_pdf('pdf/invoice.html', data)
            if pdf:
                response = HttpResponse(pdf, content_type='application/pdf')
                filename = "Invoice_%s.pdf" %(package)
                content = "inline; filename='%s'" %(filename)
                download = request.GET.get("download")
                if download:
                    content = "attachment; filename='%s'" %(filename)
                response['Content-Disposition'] = content
                return response
        return HttpResponse("Not found")

我还尝试了cid-< img src =“ cid:{{package_no_barcode}}”“ alt =” {{package.package_no}}“>

生成的svg的示例

CODE128 = barcode.get_barcode_class('code128')
package_no_barcode = CODE128(u'PLAI73664PJHS')
package_no_barcode.save('PSKK')

生成svg-

<svg xmlns="http://www.w3.org/2000/svg" height="23.000mm" version="1.1" width="40.000mm">
    <!--Autogenerated with python-barcode 0.9.0-->
    <g id="barcode_group">
        <rect height="100%" style="fill:white" width="100%"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="2.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="2.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="3.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.400mm" x="3.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="3.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.800mm" x="3.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="4.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="5.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="5.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="6.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="6.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="6.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="6.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="7.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="7.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="8.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="8.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="8.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="9.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="9.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="9.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="9.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="10.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="10.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="11.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="11.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="12.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="12.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="13.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="13.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="13.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="13.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="13.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="14.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.800mm" x="14.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="15.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="15.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.800mm" x="15.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="16.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="17.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="17.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.400mm" x="17.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="17.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.400mm" x="18.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="18.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.800mm" x="18.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="19.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="19.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="20.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="20.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.800mm" x="20.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="21.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="21.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="22.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="22.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.400mm" x="22.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="23.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.400mm" x="23.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="23.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="24.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="24.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="25.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="25.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="25.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="26.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="26.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="26.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="26.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="27.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="27.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="27.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="28.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="28.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="29.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="29.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="30.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="30.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="30.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="31.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="31.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="31.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="32.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="32.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="32.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="33.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.400mm" x="33.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="34.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="34.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="34.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.400mm" x="35.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="35.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="35.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="36.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="37.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="37.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="37.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="37.740mm" y="1.000mm"/>
        <text style="fill:black;font-size:10pt;text-anchor:middle;" x="20.340mm" y="21.000mm">PLAI73664PJHS</text>
    </g>
</svg>

更新-使用.render,-‘package_no_barcode’:package_no_barcode.render,我可以获取svg代码,但不能获取html-

b'<?xml version="1.0" encoding="UTF-8"?>\r\n<!DOCTYPE svg\r\n PUBLIC \'-//W3C//DTD SVG 1.1//EN\'\r\n
\'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\'>\r\n<svg height="23.000mm" version="1.1" width="38.000mm" xmlns="http://www.w3.org/2000/svg">\r\n
<!--Autogenerated with python-barcode 0.9.0-->\r\n <g id="barcode_group">\r\n <rect height="100%" style="fill:white" width="100%"/>\r\n <rect
height="15.000mm" style="fill:black;" width="0.400mm" x="2.540mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm"
x="2.940mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.200mm" x="3.140mm" y="1.000mm"/>\r\n <rect height="15.000mm"
style="fill:white;" width="0.400mm" x="3.340mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.200mm" x="3.740mm"
y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.800mm" x="3.940mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;"
width="0.400mm" x="4.740mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm" x="5.140mm" y="1.000mm"/>\r\n <rect
height="15.000mm" style="fill:black;" width="0.600mm" x="5.340mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm"
x="5.940mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.600mm" x="6.140mm" y="1.000mm"/>\r\n <rect height="15.000mm"
style="fill:white;" width="0.200mm" x="6.740mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.600mm" x="6.940mm"
y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.600mm" x="7.540mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;"
width="0.200mm" x="8.140mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm" x="8.340mm" y="1.000mm"/>\r\n <rect
height="15.000mm" style="fill:black;" width="0.400mm" x="8.540mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm"
x="8.940mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.600mm" x="9.140mm" y="1.000mm"/>\r\n <rect height="15.000mm"
style="fill:white;" width="0.600mm" x="9.740mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.200mm" x="10.340mm"
y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm" x="10.540mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;"
width="0.400mm" x="10.740mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm" x="11.140mm" y="1.000mm"/>\r\n <rect
height="15.000mm" style="fill:black;" width="0.600mm" x="11.340mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm"
x="11.940mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.200mm" x="12.140mm" y="1.000mm"/>\r\n <rect height="15.000mm"
style="fill:white;" width="0.600mm" x="12.340mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.400mm" x="12.940mm"
y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm" x="13.340mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;"
width="0.200mm" x="13.540mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.600mm" x="13.740mm" y="1.000mm"/>\r\n <rect
height="15.000mm" style="fill:black;" width="0.400mm" x="14.340mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm"
x="14.740mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.600mm" x="14.940mm" y="1.000mm"/>\r\n <rect height="15.000mm"
style="fill:white;" width="0.200mm" x="15.540mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.600mm" x="15.740mm"
y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.400mm" x="16.340mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;"
width="0.200mm" x="16.740mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm" x="16.940mm" y="1.000mm"/>\r\n <rect
height="15.000mm" style="fill:black;" width="0.400mm" x="17.140mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.400mm"
x="17.540mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.400mm" x="17.940mm" y="1.000mm"/>\r\n <rect height="15.000mm"
style="fill:white;" width="0.400mm" x="18.340mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.200mm" x="18.740mm"
y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.400mm" x="18.940mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;"
width="0.600mm" x="19.340mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm" x="19.940mm" y="1.000mm"/>\r\n <rect
height="15.000mm" style="fill:black;" width="0.400mm" x="20.140mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.600mm"
x="20.540mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.200mm" x="21.140mm" y="1.000mm"/>\r\n <rect height="15.000mm"
style="fill:white;" width="0.200mm" x="21.340mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.200mm" x="21.540mm"
y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.600mm" x="21.740mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;"
width="0.600mm" x="22.340mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.600mm" x="22.940mm" y="1.000mm"/>\r\n <rect
height="15.000mm" style="fill:black;" width="0.200mm" x="23.540mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm"
x="23.740mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.400mm" x="23.940mm" y="1.000mm"/>\r\n <rect height="15.000mm"
style="fill:white;" width="0.200mm" x="24.340mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.200mm" x="24.540mm"
y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.600mm" x="24.740mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;"
width="0.600mm" x="25.340mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm" x="25.940mm" y="1.000mm"/>\r\n <rect
height="15.000mm" style="fill:black;" width="0.400mm" x="26.140mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm"
x="26.540mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.200mm" x="26.740mm" y="1.000mm"/>\r\n <rect height="15.000mm"
style="fill:white;" width="0.400mm" x="26.940mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.600mm" x="27.340mm"
y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm" x="27.940mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;"
width="0.400mm" x="28.140mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.400mm" x="28.540mm" y="1.000mm"/>\r\n <rect
height="15.000mm" style="fill:black;" width="0.600mm" x="28.940mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm"
x="29.540mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.200mm" x="29.740mm" y="1.000mm"/>\r\n <rect height="15.000mm"
style="fill:white;" width="0.400mm" x="29.940mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.400mm" x="30.340mm"
y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.400mm" x="30.740mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;"
width="0.400mm" x="31.140mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.600mm" x="31.540mm" y="1.000mm"/>\r\n <rect
height="15.000mm" style="fill:black;" width="0.200mm" x="32.140mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.600mm"
x="32.340mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.200mm" x="32.940mm" y="1.000mm"/>\r\n <rect height="15.000mm"
style="fill:white;" width="0.200mm" x="33.140mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.400mm" x="33.340mm"
y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.600mm" x="33.740mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;"
width="0.600mm" x="34.340mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm" x="34.940mm" y="1.000mm"/>\r\n <rect
height="15.000mm" style="fill:black;" width="0.200mm" x="35.140mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:white;" width="0.200mm"
x="35.340mm" y="1.000mm"/>\r\n <rect height="15.000mm" style="fill:black;" width="0.400mm" x="35.540mm" y="1.000mm"/>\r\n <text
style="fill:black;font-size:10pt;text-anchor:middle;" x="19.240mm" y="21.000mm">UXXWL94HXO08</text>\r\n </g>\r\n</svg>\r\n'

如何用此代码制作svg图像?

解决方法:

要获取在html中呈现的svg,您必须按原样获取svg文本(文本输出)并将其放入模板中,而无需使用< img>标签.

<body>
    <div class="container-fluid">
      <div class="row">
        <div class="col-6">
          <div class="row">
            <div class="col-12">
            <svg xmlns="http://www.w3.org/2000/svg" height="23.000mm" version="1.1" width="40.000mm">
    <!--Autogenerated with python-barcode 0.9.0-->
    <g id="barcode_group">
        <rect height="100%" style="fill:white" width="100%"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="2.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="2.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="3.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.400mm" x="3.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="3.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.800mm" x="3.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="4.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="5.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="5.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="6.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="6.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="6.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="6.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="7.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="7.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="8.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="8.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="8.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="9.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="9.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="9.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="9.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="10.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="10.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="11.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="11.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="12.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="12.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="13.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="13.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="13.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="13.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="13.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="14.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.800mm" x="14.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="15.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="15.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.800mm" x="15.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="16.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="17.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="17.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.400mm" x="17.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="17.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.400mm" x="18.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="18.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.800mm" x="18.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="19.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="19.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="20.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="20.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.800mm" x="20.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="21.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="21.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="22.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="22.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.400mm" x="22.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="23.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.400mm" x="23.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="23.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="24.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="24.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="25.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="25.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="25.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="26.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="26.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="26.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="26.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="27.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="27.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="27.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="28.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="28.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="29.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="29.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="30.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="30.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="30.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="31.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="31.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="31.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="32.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="32.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="32.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="33.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.400mm" x="33.740mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="34.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="34.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="34.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.400mm" x="35.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="35.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.600mm" x="35.940mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.600mm" x="36.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="37.140mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.200mm" x="37.340mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:white;" width="0.200mm" x="37.540mm" y="1.000mm"/>
        <rect height="15.000mm" style="fill:black;" width="0.400mm" x="37.740mm" y="1.000mm"/>
        <text style="fill:black;font-size:10pt;text-anchor:middle;" x="20.340mm" y="21.000mm">PLAI73664PJHS</text>
    </g>
</svg>
            <span></span>
            </div>
          </div>
        </div>
      </div>
    </div>
 </body>

在代码段中,您看到的是已经渲染的html,但是您需要找到获取文本的方式,并将其作为上下文变量传递给模板,就像使用package_no_barcode一样.

编辑

我已经使用jinja2和flask进行了测试,但是解决方案应该与django及其模板引擎相似.

首先,请确保在服务器代码中以字符串形式获取svg代码.然后,将其作为上下文变量传递到模板,就像我之前提到的那样.

在模板中,您需要“评估”之前传递的变量中的html或本例中的svg代码.为了使用jinja2做到这一点,我使用了安全过滤器.

<body>
    <div class="container-fluid">
      <div class="row">
        <div class="col-6">
          <div class="row">
            <div class="col-12">
            {{ barcode | safe }}
            <span></span>
            </div>
          </div>
        </div>
      </div>
    </div>
 </body>

结果,我得到了:

enter image description here

但是我想使用django的默认模板引擎,您需要使用内置的escape过滤器来获得类似的东西.

类似的问题,但使用flask和jinja2:Passing HTML to template using Flask/Jinja2

标签:python,python-3-x,django,django-templates
来源: https://codeday.me/bug/20191010/1883473.html