开发者

How to open pdf with Django ExtJs and ReportLab

开发者 https://www.devze.com 2023-03-20 15:07 出处:网络
Hi people I\'m doing an Extjs application and I want to open a pdf with ReportLab when the user click on a button.

Hi people I'm doing an Extjs application and I want to open a pdf with ReportLab when the user click on a button.

My script is:

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'); 
                }
            });
        }
    }

and server side I've a django view:

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

When i click on the print button, Extjs give me this error: syntax error (%PDF-1.3 What I'm doing wrong?


I don't know about about Extjs to give you a full answer, but what I can tell you is that the response text is not JSON, it's a PDF, hence the reason for the error. What you really want to do is just display result in the browser, i.e. display the content of the HTTP request you initiated. If you want to verify success, check the result's HTTP head instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号