1

I am using pdfkit to convert html into pdf. Everything seems to be working perfectly in development, but after building a docker image and running it locally, the pdf is generated but images are not loaded.

Here is my how I am rendering image, which seems to be working fine locally:

<img src="{{ host_url }}/static/tcs.jpg" style="width:100px;display:block;" />

Here's my code in django view function:

template = get_template("tcs_invoice.html")
data = {
  "host_url": settings.HOST
}
html = template.render(data)  # Renders the template with the context data.
pdf = pdfkit.from_string(html, False)
 return HttpResponse(pdf, content_type='application/pdf')

I have installed wkhtmltopdf on my docker container. When I hit the url in the browser the image is shown. why does it not render in production mode.

Osama Eshtiaq
  • 63
  • 2
  • 8

2 Answers2

0

I had the same issue this answer did the trick: wkhtmltopdf, 0.12.6, Warning: Blocked access to file

It ended up being a change of behavior after wkhtmltopdf 0.12.6 where local file access is disabled by default, so you need to wether specify the files you want to allow using the --allow <file> option or set --enable-local-file-access to allow any files.

0

So after trying almost everything, even aws s3(privet bucket) none of them work ! The only way to pass the image with URL ; but we can't always do that in the case of confidential(in my case ) static images like signatures. solution?

If you are hosting it I mean env is Linux add this to settings.py:


STATIC_URL = '/static/'

STATIC_ROOT=(
os.path.join(BASE_DIR,'static')
)
STATICFILES_DIRS =[
     os.path.join(BASE_DIR, 'staticfiles'),
 ]

1.Then your static files will be here ,0.0.0.0/static/img/....

2.You need to pass the image in html page through url only to be converted to PDF

3.From the backend get the domain and pass the url to page

from django.contrib.sites.shortcuts import get_current_site
current_domain = get_current_site(request).domain


template=get_template('template location like template/some.html')
                template_render=template.render({'value':yourquery,"current_domain":current_domain})
                
                options={
                'page-size': 'Letter',
                "enable-local-file-access": None,
                "disable-smart-shrinking": None, 
                "zoom": "0.7",}
                pdf=pdfkit.from_string(template_render,False,options)
                response = HttpResponse(pdf,content_type='application/pdf')
                response['Content-Disposition'] = 'attachment; 
                filename="ourcodeworld.pdf" '
                return HttpResponse(pdf, content_type='application/pdf')

4.In this way you will be able to pass the domain cause it might change. 5.

change the static  :
<img src="{% static 'img/someimage.png' %}> 
  to 
<img src="http://{{current_domain}}/static/img/someimage.png">

It works everywhere! even in EC2. Upvotes are appreciated, Thank you!

Saikat Mukherjee
  • 500
  • 5
  • 11