[英]jinja2 and weasyprint path to image
My code convert the jinja2 template into PDF using weasyprint.我的代码使用 weasyprint 将 jinja2 模板转换为 PDF。 Everything work fine except I can't get the image to appear in the PDF.
一切正常,除了我无法让图像出现在 PDF 中。
My files structure:我的文件结构:
dist
src
img
image.png
templates
statement.html.j2
build.py
"build.py" “构建.py”
import jinja2
import json
import os
import weasyprint
from weasyprint.fonts import FontConfiguration
def main():
if not os.path.exists('dist'):
os.makedirs('dist')
jinja_env = jinja2.Environment(
loader = jinja2.FileSystemLoader('src/templates')
)
css_dir = 'src/css'
html_tpl = jinja_env.get_template('statement.html.j2')
with open('sample/json/statement.json', 'r') as f:
statement = json.load(f)
statement = statement['statement']
font_config = FontConfiguration()
html = html_tpl.render(statement=statement)
css_files = [weasyprint.CSS(os.path.join(css_dir, filename))
for filename in os.listdir(css_dir)]
rendered_html = weasyprint.HTML(
string=html
)
rendered_html.write_pdf(
'dist/statement.pdf',
stylesheets=css_files,
font_config=font_config
)
if __name__ == '__main__':
main()
"statement.html.j2" “statement.html.j2”
<img class="img-responsive" src="../img/image.png">
Is the image path correct?图片路径是否正确? How I can reference the image in the template?
如何在模板中引用图像? What will be the path then?
那么路径是什么呢? Or the issue is not related to the image reference?
或者问题与图像参考无关?
This question has been answered here : Image ignored in PDF rendering with jinja2 and weasyprint ,此问题已在此处得到解答: 使用 jinja2 和 weasyprint 在 PDF 渲染中忽略图像,
you need to add base_url='.'
你需要添加
base_url='.'
here :这里 :
HTML(string=html_out, base_url='.').write_pdf(outfile)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.