问题
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