PDF output using Weasyprint not showing images (Django)

Setup static for the path of your image as:

{% load static %}
<img src="{% static 'images/your_image.png %}" alt="" />

and then you have to pass the base_url in HTML class of Weasyprint as:

HTML(string=html_string, base_url=request.build_absolute_uri())

Fixed by:

Add base_url=request.build_absolute_uri() so that

html = HTML(string=html_string)

becomes

html = HTML(string=html_string, base_url=request.build_absolute_uri())

That will allow for relative URLs in the HTML file.

For the images, only PNG images seems to work for some reason.

For the HTML styles to show on the PDF, add presentational_hints=True as per the Weasyprint docs:

    pdf = html.write_pdf(stylesheets=[CSS(settings.STATIC_ROOT +  '/css/detail_pdf_gen.css')], presentational_hints=True);