[Python] Render image in template.html with Jinja2 and weasyprint in a PDF

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have a python file that generates an HTML page (and a PDF) using jinja2 like this.

    import pandas as pd
    import requests
    from jinja2 import Environment, FileSystemLoader
    from weasyprint import HTML

    # Stuff to get dataframe...

    # Generate PDF and csv
    env = Environment(loader=FileSystemLoader('.'))
    template = env.get_template("template.html")

    template_vars = {"title" : "Something...",
    "vulnerabilities_table": df.to_html(index=False)}

    html_out = template.render(template_vars)
    HTML(string=html_out).write_pdf("report_styled.pdf", stylesheets=["style.css"])


    It uses the following template.html

    <!DOCTYPE html>
    {% block layout_style %}
    <style> @page { size: letter landscape; margin: 2cm; } </style>

    {% endblock %}
    <html>
    <head lang="en">
    <meta charset="UTF-8">
    <title>{{ title }}</title>
    </head>
    <body>
    <img src="static/images/Logo.png" style="height: 100; width: 300; float:inline-start">
    <h2>{{ title }}</h2>
    {{ vulnerabilities_table }}
    </body>
    </html>


    The problem is that the image is not rendering at all (but the rest does). I guess it's because I'm not using Flask

    <img src="static/images/Logo.png" style="height: 100; width: 300; float:inline-start">


    My image is in the folder static/images/Logo.png. What did I do wrong ?

    Continue reading...

Compartilhe esta Página