Forums

Need help with "render_to_pdf" and image into it

I've got this function

image = settings.STATIC_ROOT+'/img/logo_facture.png'
context = {
   [...]
    'image': image,
}
html = template.render(context)
pdf = render_to_pdf(path_pdf, context)
if pdf:
    response = HttpResponse(pdf, content_type='application/pdf')
    filename = type_pdf + "_%s_%s.pdf" % (commande.id, commande.date.strftime('%Y'))
    content = "inline; filename=%s" % filename
    download = request.GET.get("download")
    if download:
        content = "attachment; filename=%s" % filename
    response['Content-Disposition'] = content
    return response
return HttpResponse("Not found")

on my pdf.html template somewhere on this webpage i've got this img tag

<img src="{% static '/img/logo_facture.png' %}" height="116" width="330" alt="image non chargée" />

but nothing appear on my PDF downloaded ... I tried with {{ STATIC_ROOT }} ... with "/home/Pseudo/project/static/img/logo_facture.png, I tried with https://Pseudo.pythonanywhere.com... (absolute path) ... I tried to with image var in context .. Still no image on my PDF downloaded :( ... On local it's works ^^

I applied all settings into web settings on pythonanywhere ...

If someone has a solution ... another PDF converter library to use ?

Thanks for your help ;)

Use the developer tools in your browser to see what might be happening to your pdf and you can also add logging to your web app code (prints to stderr will appear in your error log) so you can see what it's doing and perhaps work out what's going wrong.

Ok ... thanks for your help ... I modified my code to user generate_pdf instead render_to_pdf but what I didn't understand is when I show the result in a new page (PDF format) logo is shown ... but when I download it directly (content = "attechment; filename=%s" ...) image disappear ... Don't know why :(

My guess would be that the first instance is pre-generated so that the image can be retrieved from you web app. In the second case, the request to get the image fails. It fails because you have a free account and free accounts only have one worker. That worker is busy with the request that is generating the PDF, so it cannot serve the image, so the request to get the image fails.

Oh I understood :) Thanks again for your reply ... I know now why it's only work on local and not on pythonanywhere ;)