简体   繁体   中英

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

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">

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM