# [web] BLOBs, PDFs and Python

This topic is 4888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am working on a Python script that will run under the Zope server. It is supposed to obtain a BLOB from a MySQL database and return it to the client as a PDF file. The BLOBs themselves already contain the PDF file itself so returning the stream and telling the client that it is a PDF should be enough. So far, my script successfully retrieves the BLOB and presents it in an array object. The Zope server is configured in such a way that I am unable to access the methods that are part of the array object, so that makes things a little more difficult. When I just write the contents of the array into the response stream, the following appears as the contents of the file when it is saved:
array('c', '%PDF-1.2\n%\xc7 ... \x9f\xbb\x')
This seems like it's just writing the array object as a whole and not just its contents. To get around this, I used the following hack:
pdf_string = (str(pdf_array)[12:len(pdf_array) - 2])
This effectively stripped the unnecessary content and only left the PDF data. I took the remainder and wrote it into the stream, so far so good. This is the code I use to write the response:
RESPONSE.setHeader('Content-Disposition', 'attachment; filename="abstract.pdf"')
RESPONSE.write(pdf_string)
The problem is, when the client tries to open the PDF, Acrobat says that it is damaged and is unable to be repaired. I'm figuring that there is some mangling going on with the array contents. My question is, what could be wrong with what I'm doing? Is there a certain encoding that I should be using while writing the stream, and if so, how can I do this in Python? Also, is it worth trying to obtain permission from the admin in order to modify the server and allow access to some of these objects such as array?

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633674
• Total Posts
3013275
×