首页 > 解决方案 > 在pdf视图中执行js

问题描述

我已将 html 呈现为字符串,该字符串将作为 PDF 响应返回

class ReportFileResponse(FileResponse):

    def __init__(self, report, output_format):
        ....... # queries
        if output_format == 'pdf':
            super(ReportFileResponse, self).__init__(streaming_content=my_pdf_str,
                                                 content_type='application/pdf')

问题是字符串包含一些肯定需要执行的js(修改一些块)。它在浏览器中工作(作为 html),但不在 PDF 响应中执行。有没有办法执行JS代码?

我想使用 Selenium 来执行 JS 代码。但是当您需要转到该页面时,我该怎么做。但就我而言,我不能那样做。我无法访问该页面,因为我已经渲染了 html。

至少我需要进行查询getElementsByClassName并有权访问属性

PS 对于渲染我们使用 django-weasyprint

标签: pythondjango

解决方案


推荐阅读