芹菜+Django pdf gen

2024-06-02 06:58:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些问题:如何捕捉我生成的pdf文件任务.py并在重定向到主页并将下载链接放入此页面后自动开始下载?我的任务.py因为我在终端看到了结果。在

我的任务.py公司名称:

@shared_task
def html_to_pdf(ctx):
    html_string = render_to_string('pdf_template.html', ctx)
    html = HTML(string=html_string)
    html.write_pdf('/tmp/report.pdf')

    fs = FileSystemStorage('/tmp')
    with fs.open('report.pdf') as pdf:
        response = HttpResponse(pdf, content_type='application/pdf')
        response['Content-Disposition'] \
            = 'attachment; filename="report.pdf"'
        return response

我的视图.py公司名称:

^{pr2}$

Tags: 文件topyreport名称stringpdfresponse
1条回答
网友
1楼 · 发布于 2024-06-02 06:58:43

您的芹菜任务执行超时,并返回响应(因此任务.延迟()). 当他们到达该页面时,任务很可能仍在生成PDF。如果你想运行这个任务然后等待,你可以这样做

    ctx = {'name': name,
           'date_of_generation': date_of_generation,
           'pdf_text_content': pdf_text_content,
           'pdf_numbers_content': pdf_numbers_content}

    tsk = html_to_pdf.apply_async(ctx)
    ret = tsk.get()

    url = reverse('home_page')
    return HttpResponseRedirect(url)

然后,我要做的是找出一种方法来创建另一个端点来提供该文件,而不是将其作为响应对象提供。在

相关问题 更多 >