Jump to content
  • Advertisement
Sign in to follow this  

[web] BLOBs, PDFs and Python

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

If you intended to correct an error in the post then please contact us.

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.setHeader('Content-Type', 'application/pdf')
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?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!