Weasyprint pdf generation, too long, makes the download impossible. How to stream its creation?

狂风中的少年 提交于 2020-01-07 06:43:09

问题


I've been using Weasyprint for pdf generation successfully, until I reach a certain size, a common use case of my app, where the pdf generation takes so long (more than 10s) that it breaks the connectivity with the browser, and the download is impossible.

I suppose I must stream the file creation and return a django StreamingHttpResponse (agree ?). I wouldn't pre-process the pdf because it is formed from baskets with items users frequently add or delete.

But how can I stream the file creation with weasyprint ? Even if I cut my sourceHtml string in parts, how to write the pdf step by step ?

I render a django template and generate the pdf from it:

from weasyprint import HTML

sourceHtml = template.render(my-objects)
outhtml = HTML(string=sourceHtml).write_pdf()

response = HttpResponse(outhtml, content_type='application/pdf')
response['Content-Disposition'] = u'attachment; filename="{}.pdf"'.format(name)

Or is there another way to solve this problem ?

Thanks !


回答1:


I asked on the issue tracker: https://github.com/Kozea/WeasyPrint/issues/416

It is not doable and a suggested workaround is to

split the download into two steps: one route asynchronously generates the document and stores it on the filesystem, the second route downloads the generated document. When the document is not generated yet, you can hide the second link and display something like "the document is not generated yet" instead.



来源:https://stackoverflow.com/questions/41723844/weasyprint-pdf-generation-too-long-makes-the-download-impossible-how-to-strea

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