jdum / odfdo

python library for OpenDocument format (ODF)
Apache License 2.0
48 stars 11 forks source link

Render ODT without saving #13

Closed rodolfolotte closed 6 months ago

rodolfolotte commented 3 years ago

Is there any feature that would avoid the use of doc.save(target=PATH, pretty=True) in order to write the produced Document() in a response object?

jdum commented 3 years ago

The doc.save() actually creates the "zip" document.

In [10]: d=Document() In [11]: d Out[11]: <odfdo.document.Document at 0x1119878b0> In [12]: with BytesIO() as output: ...: d.save(output) ...: In [13]: output Out[13]: <_io.BytesIO at 0x111a65e50>

regards,

jdum commented 3 years ago

Fix: I think I need to not close the bytesIO too early, so: d=Document() b=BytesIO() d.save(b)

use the bytesio in some way: with open("test.zip", "wb") as f: f.write(b.getvalue())

only close now b.close()

rodolfolotte commented 3 years ago

Fast reply! Thank you so much!

I will try right now and will post the solution here!

I have a django project which will build an odt file and delivery it as a response ('application/vnd.oasis.opendocument.text'') to download. I will try your suggestion in a response.write

regards,

rodolfolotte commented 3 years ago

@jdum that works fine! Thank you!

filename = "filename.odt"
response = HttpResponse(content_type='application/vnd.oasis.opendocument.text')
response['Content-Disposition'] = 'attachment; filename=' + filename

b = BytesIO()
doc.save(b)
response.write(b.getvalue())
b.close()
jdum commented 6 months ago

Added 2 recipes for save/read from io.BytesIO