Django ReportLab:使用Drawing对象创建PDF并通过Httpresponse返回

时间:2022-06-12 09:46:26

In ReportLab, Drawing object can be written into different renderers, e.g

在ReportLab中,可以将Drawing对象写入不同的渲染器,例如

d = shapes.Drawing(400, 400)
renderPDF.drawToFile(d, 'test.pdf')

and in Django, Canvas object can be sent via httpresponse, e.g.:

在Django中,Canvas对象可以通过httpresponse发送,例如:

response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'filename=test.pdf'
c = canvas.Canvas(response)

in my case, my problem is that I have a reportLab script using Drawing object which saves to local file system. I now put it in Django views, and wondering whether there is a way to not save to local file system but instead sent back to client.

在我的情况下,我的问题是我有一个使用Drawing对象的reportLab脚本,它保存到本地文件系统。我现在把它放在Django视图中,并想知道是否有一种方法可以不保存到本地文件系统,而是发送回客户端。

I hope I describe this question clearly.

我希望我能清楚地描述这个问题。

Thanks for any advice!

谢谢你的建议!

updates

it turns out there is a function in renderPDF:

事实证明renderPDF中有一个函数:

renderPDF.draw(drawing, canvas, x, y)

which can render drawing() object in the given canvas.

它可以在给定的画布中渲染drawing()对象。

3 个解决方案

#1


6  

Using ReportLab in Django without saving to disk is actually pretty easy. There are even examples in the DjangoDocs (https://docs.djangoproject.com/en/dev/howto/outputting-pdf)

在不保存到磁盘的情况下在Django中使用ReportLab实际上非常简单。 DjangoDocs中甚至有一些例子(https://docs.djangoproject.com/en/dev/howto/outputting-pdf)

The trick basically boils down to using a "file like object" instead of an actual file. Most people use StringIO for this.

这个技巧基本上归结为使用“像对象一样的文件”而不是实际的文件。大多数人都使用StringIO。

You could do it pretty simply with

你可以很简单地做到这一点

from cStringIO import StringIO

def some_view(request):
    filename = 'test.pdf'

    # Make your response and prep to attach
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=%s.pdf' % (filename)
    tmp = StringIO()

    # Create a canvas to write on
    p = canvas.Canvas(tmp)
    # With someone on
    p.drawString(100, 100, "Hello world")

    # Close the PDF object cleanly.
    p.showPage()
    p.save()

    # Get the data out and close the buffer cleanly
    pdf = tmp.getvalue()
    tmp.close()

    # Get StringIO's body and write it out to the response.
    response.write(pdf)
    return response

#2


2  

it turns out there is a function in renderPDF:

事实证明renderPDF中有一个函数:

renderPDF.draw(drawing, canvas, x, y) which can render drawing() object in the given canvas.

renderPDF.draw(drawing,canvas,x,y),它可以在给定画布中渲染drawing()对象。

#3


0  

Drawing has a method called asString with a one required attribute that represents the required drawing format such as 'png', 'gif' or 'jpg'. so instead of calling

Drawing有一个名为asString的方法,它有一个必需属性,表示所需的绘图格式,如'png','gif'或'jpg'。所以不要打电话

renderPDF.drawToFile(d, 'test.pdf')

You could call

你可以打电话

binaryStuff = d.asString('gif')
return HttpResponse(binaryStuff, 'image/gif')

Without the need to save your drawing to the disc.

无需将绘图保存到光盘。

Check https://code.djangoproject.com/wiki/Charts for full example.

有关完整示例,请访问https://code.djangoproject.com/wiki/Charts。

#1


6  

Using ReportLab in Django without saving to disk is actually pretty easy. There are even examples in the DjangoDocs (https://docs.djangoproject.com/en/dev/howto/outputting-pdf)

在不保存到磁盘的情况下在Django中使用ReportLab实际上非常简单。 DjangoDocs中甚至有一些例子(https://docs.djangoproject.com/en/dev/howto/outputting-pdf)

The trick basically boils down to using a "file like object" instead of an actual file. Most people use StringIO for this.

这个技巧基本上归结为使用“像对象一样的文件”而不是实际的文件。大多数人都使用StringIO。

You could do it pretty simply with

你可以很简单地做到这一点

from cStringIO import StringIO

def some_view(request):
    filename = 'test.pdf'

    # Make your response and prep to attach
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=%s.pdf' % (filename)
    tmp = StringIO()

    # Create a canvas to write on
    p = canvas.Canvas(tmp)
    # With someone on
    p.drawString(100, 100, "Hello world")

    # Close the PDF object cleanly.
    p.showPage()
    p.save()

    # Get the data out and close the buffer cleanly
    pdf = tmp.getvalue()
    tmp.close()

    # Get StringIO's body and write it out to the response.
    response.write(pdf)
    return response

#2


2  

it turns out there is a function in renderPDF:

事实证明renderPDF中有一个函数:

renderPDF.draw(drawing, canvas, x, y) which can render drawing() object in the given canvas.

renderPDF.draw(drawing,canvas,x,y),它可以在给定画布中渲染drawing()对象。

#3


0  

Drawing has a method called asString with a one required attribute that represents the required drawing format such as 'png', 'gif' or 'jpg'. so instead of calling

Drawing有一个名为asString的方法,它有一个必需属性,表示所需的绘图格式,如'png','gif'或'jpg'。所以不要打电话

renderPDF.drawToFile(d, 'test.pdf')

You could call

你可以打电话

binaryStuff = d.asString('gif')
return HttpResponse(binaryStuff, 'image/gif')

Without the need to save your drawing to the disc.

无需将绘图保存到光盘。

Check https://code.djangoproject.com/wiki/Charts for full example.

有关完整示例,请访问https://code.djangoproject.com/wiki/Charts。