3

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

Nancy
  • 997
  • 1
  • 8
  • 12
  • Pdfkit/wkhtmltopdf won't handle JS rendering without some changes. Have you tried looking at this? http://stackoverflow.com/questions/6949685/create-pdf-with-wkhtmltopdf-and-rendering-javascript – Charlie Mar 12 '15 at 18:18

1 Answers1

5

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'
           }
Marlon Abeykoon
  • 11,927
  • 4
  • 54
  • 75
Arun24
  • 51
  • 1
  • 3
  • still printing a blank page for me. the page I am trying to load is a plotly dash website with graphs etc. – Zaffer Dec 13 '22 at 22:04