adding image in pdf using Django pisa: standard {% static 'path/to/image' %} do not working

377 Views Asked by At

I try to implement pdf output using pisa following this tutorial: https://www.codingforentrepreneurs.com/blog/html-template-to-pdf-in-django/

and it works except for images that are not displayed

I understand the problem deal with relative/absolute path between pisa and Django system but do not manage to resolve. I read the solution in the xhtml2pdf document using link_callback method but it dosen work and have no error

If I pass the absolute url of images in context to my html template it works :

<img class="logo" src="{{ url }}" alt="logo alima">
1

There are 1 best solutions below

0
Mohamed ElKalioby On

You are right, the problem that {% static %} returns the path under the domain and when xhtml2pdf runs it doesn't know which domain to contact so this problem has several solutions

  1. Add request.META['HOST'] to your image url
  2. Adding a settings variable called 'PDF_BASE_URL' and it to your image URL.

I like the second solution as you can set the value to localhost with the need to resolve the domain which is used by the users.