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

浪尽此生 提交于 2019-12-11 01:37:03

问题


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


回答1:


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'
           }


来源:https://stackoverflow.com/questions/25757503/approach-python-pdfkit-convert-webpagejs-generated-into-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!