微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

django 中的 reportlab,返回一个字符串而不是 pdf

如何解决django 中的 reportlab,返回一个字符串而不是 pdf

我在一个应用程序中工作,我需要根据从服务器获得的响应创建 pdf 文件。我在 django 1.8 中使用 reportlab,响应如下所示:

%PDF-1.3

%���� ReportLab Generated PDF document http://www.reportlab.com

1 0 obj

<< /F1 2 0 R >>

endobj

2 0 obj

<< /BaseFont /Helvetica /Encoding /WinAnsiEncoding /Name /F1 /Subtype /Type1 /Type /Font >>

endobj

3 0 obj

<< /BitsPerComponent 8 /ColorSpace /DeviceRGB /Filter [ /ASCII85Decode /DCTDecode ] /Height 1250 /Length 1206832 /Subtype /Image 

  /Type /XObject /Width 1667 >>

stream

[...]

这是我的代码

def generate_pdf(request,course_id):

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

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

    # # change the page size
    p.setPageSize((500,500))

    # # Draw the image at x,y. I positioned the x,y to be where i like here
    p.drawImage('fond.jpg',width=500,height=500)



    # 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

有什么想法吗?谢谢回复

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。