Display an image on my pdf generate with render_to_pdf
I would like to display an image on my pdf that I generated from information
views.py
class GeneratePdf(View):
def get(self, request, *args, **kwargs):
data={'name':'toto','age':122,'service':'smapa'}
pdf = render_to_pdf('cart/commande.html',data)
return HttpResponse(pdf, content_type='application/pdf')
utils.py
from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa
def render_to_pdf(template_src, context_dict={}):
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), result)
if not pdf.err:
return result.getvalue()
return None
commande.html
{% load static %}
""
<body>
<div class='wrapper'>
<div class='header'>
<p class='title'>Invoice # </p>
<img src="{% static 'images/tof.png' %}" style="width:100px;height: 100px;">
</div>
<div>
<div class='details'>
name: {{name}}<br/>
age: {{age}} <br/>
service: {{service}}
<hr class='hrItem' />
</div>
</div>
</body>
""
I cannot display the image, at the level of the pdf document it is mentioned at the level of the execution console,
Need a valid file name
I managed to display the image on the pdf, by doing:
commande.html
<img src="static/images/tof.png" style="width:100px;height: 100px;">
Merci