Reputation: 79
I'm trying to generate bills in pdf with Weasyprint and Jinja2 I have the following file structure:
project
scripts
pdf_builder.py
templates
bill_template.html
bill_style.css
However, whatever I try I get the following error:
jinja2.exceptions.TemplateNotFound: ../templates/bill_template.html
My relevant code for pdf_builder.py
is:
from weasyprint import HTML
from jinja2 import Environment, FileSystemLoader
file_html = "../templates/bill_template.html"
# loading the jinja2 environment
env = Environment(loader=FileSystemLoader('../templates'))
# Render and build
template = env.get_template(file_html)
html_out = template.render()
HTML(string=html_out).write_pdf(pdf_name, stylesheets=[file_css])
Upvotes: 1
Views: 2929
Reputation: 79
Found it.
For jinja, you need to specify in the FileSystemLoader
where the templates folder is, and then give the relative paths to the template file from that.
But, for the css (that you give to Weasyprint, you have to to specify the relative path from the working directory).
That gives:
file_html = f"{document_type}_template.html"
file_css = f"templates/{document_type}_style.css"
env = Environment(loader=FileSystemLoader('templates'))
template = env.get_template(file_html)
html_out = template.render(template_vars)
HTML(string=html_out).write_pdf(pdf_name, stylesheets=[file_css])
Upvotes: 1