【问题标题】:How to open pdf with Django ExtJs and ReportLab如何使用 Django ExtJs 和 ReportLab 打开 pdf
【发布时间】:2011-10-05 15:00:29
【问题描述】:

大家好,我正在做一个 Extjs 应用程序,当用户点击一个按钮时,我想用 ReportLab 打开一个 pdf。

我的脚本是:

xtype: 'button',
text: 'Print',
listeners: {
    click: function(evt, comp) {
        Ext.Ajax.request({
            url : 'get_pdf',
            method: 'GET',
            success: function ( result, request ) {
                var pdf = Ext.util.JSON.decode(result.responseText);
                if (pdf.success) {
                    console.log('It's ok'); 
                }
            });
        }
    }

服务器端我有一个 django 视图:

def get_pdf(request):

    # Create the HttpResponse object with the appropriate PDF headers.
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=somefilename.pdf'

    # Create the PDF object, using the response object as its "file."
    p = canvas.Canvas(response)

    # Draw things on the PDF. Here's where the PDF generation happens.
    # See the ReportLab documentation for the full list of functionality.
    p.drawString(100, 100, "Hello world.")

    # Close the PDF object cleanly, and we're done.
    p.showPage()
    p.save()
    return response

当我点击打印按钮时,Extjs 给我这个错误:语法错误(%PDF-1.3 我做错了什么?

【问题讨论】:

  • 您希望 Extjs 对该 PDF 做什么?看起来您正在尝试将其解析为 JSON,我无法想象为什么。

标签: django extjs pdf-generation reportlab


【解决方案1】:

我不知道 Extjs 能给你一个完整的答案,但我可以告诉你的是响应文本不是 JSON,它是 PDF,因此是错误的原因。你真正想做的只是在浏览器中显示result,即显示你发起的HTTP请求的内容。如果要验证成功,请改为检查结果的 HTTP 头。

【讨论】:

  • 是的,我不必使用 Ext.decode 对其进行解码...我使用 iframe 并在 "src" 参数中指定 url "get_pdf" 解决了
猜你喜欢
  • 2020-01-28
  • 1970-01-01
  • 2015-10-11
  • 1970-01-01
  • 2014-08-06
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
  • 2014-08-16
相关资源
最近更新 更多