如何使用 Django ExtJs 和 ReportLab 打开 pdf

发布于 2024-11-24 12:27:29 字数 1242 浏览 2 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城挽邺 2024-12-01 12:27:29

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文