如何使用Django ExtJs和ReportLab打开pdf

时间:2011-07-14 18:59:38

标签: django extjs pdf-generation reportlab

大家好我正在做一个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 我做错了什么?

1 个答案:

答案 0 :(得分:0)

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