将svg存储在jinja2中的变量中 [英] Getting svg stored in a variable in jinja2

查看:80
本文介绍了将svg存储在jinja2中的变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 viivakoodi获取字母数字编码的条形码.我得到了我需要的条形码,但无法使用Jinja2在我的HTML页面中显示它.这是我的代码:

I'm trying to get an alphanumeric-encoded barcode using the viivakoodi library. I got the barcode which I needed but I'm not able to display it in my HTML page using Jinja2. Here's my code:

import barcode
from StringIO import StringIO

encoding_std = barcode.get_barcode_class('code128')
ean = encoding_std('Test123')
fp = StringIO()
ean.write(fp)
encoded_output = fp.getvalue()
fp.close()

我当前将条形码存储为SVG,但无法在HTML页面中显示.在HTML页面中使用{{ encoded_ouput | safe }}来显示条形码不会向我显示结果:

I'm currently storing the barcode as SVG and I'm not able to display that in the HTML page. Using {{ encoded_ouput | safe }} in the HTML page to display the barcode does not show me the results:

<div class="row border-bottom no-margin" style="height: 15%">
    <div class="float-left border-right"
         style="width: 100%; padding: 5px; height: 100%;font-family:arial">
        <p style="font-weight:bold; margin-bottom: 0; font-family:arial; font-size:13px">
            <b>Generated Barcode Space:</b></p><b>
            {{ encoded_output | safe }}

    </b></div>
</div>

或者那里有任何Jinja2过滤器来获取SVG?

Or any Jinja2 filters are there to get the SVG?

推荐答案

生成的SVG输出包括XML标头和doctype:

The SVG output produced includes an XML header and doctype:

>>> print '\n'.join(encoded_output.splitlines()[:4])
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg
  PUBLIC '-//W3C//DTD SVG 1.1//EN'
  'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>

尽管您的代码可在Chrome中运行,但其他浏览器可能无法宽恕包含的额外标头.我从生成的输出中拆分了这些:

Although your code works in Chrome, other browsers may not be so forgiving of the extra headers being included. I'd split those from the output generated:

encoded_output = encoded_output[encoded_output.find('<svg'):]

这篇关于将svg存储在jinja2中的变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆