Nancy
Nancy

Reputation: 1097

Approach: python-pdfkit convert webpage(JS generated) into PDF

views.py

def download_as_pdf(request):
    # some stuff/function call to get updated(with data and JS) template and render it

    return render(request, temp)

def download(request):
    import pdfkit
    pdfkit.from_url('/download/', 'out.pdf', options={'ignore-load-errors': None})
    return HttpResponse('DONE')

urls.py

url(r'^download/', views.download_as_pdf, name="download_pdf")

I want to print all content(some graphs (JS generated - flotcharts)) of /download/ url in pdf. If I put these two lines

import pdfkit
pdfkit.from_url('/download/', 'out.pdf', options={'ignore-load-errors': None})

in download_as_pdf view it prints nothing in pdf (pdf gets download though but empty) (I think because template rendering happening after these statements thats why ??)

how should I proceed to prit graph in pdf (can I solve this by threading? how?) or any other approach

Upvotes: 3

Views: 5481

Answers (1)

Arun24
Arun24

Reputation: 51

This could be because pdf is rendered before the entire javascript is loaded. If you introduce a javascript delay it will work.

 options = {
    'javascript-delay':'5000'
           }

Upvotes: 5

Related Questions