【问题标题】:Weasyprint django - generates an empty PDFWeasyprint django - 生成一个空的 PDF
【发布时间】:2021-08-23 22:12:32
【问题描述】:

下午好,

我的应用程序出现以下问题:

我正在通过 WEASYPRINT 生成 pdf,以表达我的一些观点。我的应用是一个聚合外部信息的搜索引擎,我正在寻找的是:

  • 用户进行搜索并显示结果。

  • 当他点击报告时,他可以下载带有搜索结果的 pdf。 (通过 AJAX 无需重新加载页面)。

到目前为止,使用 xhtml2pdf 可以完美运行,但我想将其更改为 WEASYPRINT,因为它可以让 pdf 的设计更加灵活。

正如我所说,有一个将数据发送到 pdf 生成视图的 ajax 函数,它接收它们并生成一个 pdf,将 html 转换为 pdf 并发送一个响应,该响应在 javascript 中使用“.done()”函数激活pdf的下载。问题是这个pdf显示为空,因为一定有一些解码问题,或者我认为。

Views.py

def ViewPDF(request, *args, **kwargs):
    response = request.POST.get('nombre', None)
    hits = request.POST.get('hits', None)
    
    response2 = request.POST.get('query1', None)
    
    info = {'searched': str(response2), 'customer': request.user.customer.name, 'type_of_search': '',
            'lists_covered': 'OFAC', 'Date_of_search': str(request.POST.get('date', None)), 'hits': hits}

    if response is not None:
        # json_search = json.loads(response2)

        if hits == 'YES':
            json_data = json.loads(response)

            info['type_of_search'] = 'basic search'

            data = []
            
            for i in range(len(json_data)):
                data.append({'body': {
                    'Name': json_data[i]['name'],
                    'Description': json_data[i]['notes'],
                    'Occupation': json_data[i]['ocupation'],
                    'Place_of_Birth': json_data[i]['POB'],
                    'Date_of_Birth': json_data[i]['DOB'],
                    'Position': json_data[i]['other'],
                    'Citizenship': json_data[i]['nationality']
                }})

            data = {'info': info, 'body': data}

            template = get_template('core/reports/free_search.html')
            html = template.render(data)  
            

            
            pdf = HTML(string=html).write_pdf()
            
            response = HttpResponse(pdf, content_type='application/pdf')
            response['Content-Transfer-Encoding'] = 'utf-8'
               
            
            
        
  
            
            if request.is_ajax():

              
              
                return response

main.js

                var url = '/pdf/' + query['s_pk']
                $report0 = $('#report');
                $report0.on('click', function () {
                    

                    $.ajax({
                        url: url,
                        type: 'POST',
                        data: {
                            query1: query['search'],
                            nombre: JSON.stringify(query['responseData']),
                            date: $time,
                            hits: 'YES',
                            'csrfmiddlewaretoken': $("input[name=csrfmiddlewaretoken]").val()
                        }
                    }).done(function (response) {
                        console.log(response)
                        
                        let blob = new Blob([response]);
                        let link = document.createElement('a');
                        link.href = window.URL.createObjectURL(blob);
                        link.download = query['search'] + '.pdf';
                        link.click();
                    });

对请求的响应

enter image description here

我想这与编码有关,但我想在这里提供一些帮助...您认为问题出在哪里?

提前致谢。

【问题讨论】:

    标签: javascript python django pdf weasyprint


    【解决方案1】:

    我设法通过以下方式做到了

    【讨论】:

      猜你喜欢
      • 2018-03-26
      • 1970-01-01
      • 2016-12-23
      • 2020-02-01
      • 2014-07-15
      • 2021-10-31
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多