2016-03-07 113 views
0

我只是在用已經寫好的python應用程序來研究Jinja2。我可能會以錯誤的方式解決這個問題,但這是我想要做的。Jinja2/Python將圖像插入到html中

from jinja2 import Environment, FileSystemLoader 
from weasyprint import HTML 
env = Environment(loader=FileSystemLoader('.')) 
template = env.get_template("really.html") 

template_vars = {"title":"TITLE","graph":'total.png'} 

html_out = template.render(template_vars) 

HTML(string=html_out).write_pdf("report.pdf") 

這幾乎產生我想要什麼,我得到了一個名爲report.pdf PDF格式,但附加的文件,而不是,它是total.png的字符串。這是我第一次使用Jinja,所以希望附上這樣的圖像是可能的。謝謝。

這是模板,沒有多少內容,只是試圖做這件作品。

<!DOCTYPE html> 
<html> 
<head lang="en"> 
<meta charset="UTF-8"> 
<title>{{ title }}</title> 
</head> 
<body> 
<h2>Graph Goes Here</h2> 
{{ graph }} 
</body> 
</html> 
+0

我們展示模板 – yedpodtrzitko

回答

1

我有一個答案,以我自己的問題,我只是能夠將圖像URL添加到模板,而不試圖在把它作爲一個變量。

<!DOCTYPE html> 
<html> 
<head lang="en"> 
<meta charset="UTF-8"> 
<title>{{ title }}</title> 
</head> 
<body> 
<h2>Graph Goes Here</h2> 
<img src="graph.png"> 
</body> 
</html> 

想我是有點過分複雜吧...

+0

您好,我一個試圖做同樣的事情,你。但我沒有獲得PDF格式的圖像。另外我正在使用龍捲風web服務器。我得到的表格不是PDF中的圖像。你能提供任何幫助嗎? –

+0

我唯一的猜測就是確保路徑對文件名是正確的。 – user3582887