3

I am just looking into using Jinja2 with a python application I have already written. I may be going about this in the wrong way, but here is what I would like to do.

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")

This nearly produces what I want, I get a pdf called report.pdf, but instead of the attached file, it is a string of total.png. This is my first run at using Jinja, so hopefully attaching an image like this is possible. Thanks.

This is the template, not much built, just trying to do this piece at first.

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>{{ title }}</title>
</head>
<body>
<h2>Graph Goes Here</h2>
 {{ graph }}
</body>
</html>
Dark Knight
  • 869
  • 1
  • 9
  • 18
user3582887
  • 403
  • 3
  • 6
  • 19

1 Answers1

2

I have an answer to my own question, I was simply able to add the image url into the template, without trying to pass it in as a variable.

<!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>

Guess I was over complicating it a bit...

user3582887
  • 403
  • 3
  • 6
  • 19
  • Hi, I a trying to do the exact same thing as you. But I am not getting the image in PDF. Also I am using tornado webserver. I am getting the table not the image in PDF. Could you suggest any help? – Dark Knight Jun 30 '16 at 18:36
  • My only guess would be to make sure the path is correct for the file name. – user3582887 Jun 30 '16 at 20:56