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 (将#修改为@)