return PDF content created from a blob field

189 views
Skip to first unread message

Fred Guedes Pereira

unread,
Nov 14, 2013, 12:21:37 PM11/14/13
to web...@googlegroups.com
Hello,

Is there a way to return PDF content generated from a blob db field? 

My table has a blob field whose content is PDF. I would like to create a 'PDF page' from those bytes and return it.

Thanks,

Sorry for possible duplication.

Fred

Niphlod

unread,
Nov 14, 2013, 3:56:19 PM11/14/13
to web...@googlegroups.com
set the relevant response.headers (such as content-type = 'application/pdf' and content-disposition = attachment; filename="afilename.pdf")

then

response.stream(content)

and you should be good to go (as long as the said "blob" holds the entire content of the pdf)

Fred Guedes Pereira

unread,
Nov 18, 2013, 8:03:45 AM11/18/13
to web...@googlegroups.com
Thanks, Niphlod.

Now it's clear for me, but I cant use response.stream() because it receives a filename, not a content, as parameter. Should I save the content into a local file first? 

Fred

Niphlod

unread,
Nov 18, 2013, 8:56:36 AM11/18/13
to web...@googlegroups.com
taken from the book....

 As noted above, response.download should be used to retrieve files stored via an upload field. response.stream can be used in other cases, such as returning a temporary file or StringIO object created by the controller. If attachment is True, the Content-Disposition header will be set to "attachment", and if filename is also provided, it will be added to the Content-Disposition header as well (but only when attachment is True). If not already included in response.headers, the following response headers will be set automatically: Content-Type, Content-Length, Cache-Control, Pragma, and Last-Modified (the latter three are set to allow browser caching of the file). To override any of these automatic header settings, simply set them in response.headers before calling response.stream.

So, you can use a StringIO instance to stream.... it **should** work with your blob contents.

Fred Guedes Pereira

unread,
Nov 18, 2013, 10:05:49 AM11/18/13
to web...@googlegroups.com
Hi Niphlod,

It worked! No need for StringIO. I just returned the blob field as the http response content. The code:

def pdfdoc():
    evento = session._eventos[int(request.args(0))] or redirect(URL('eventos'), args=request.args(0))
    response.headers['Content-Type'] = 'application/pdf'
    response.headers['Content-Disposition'] = 'attachment; filename="teste.pdf"'
    return evento['lb_documento']  #lb_documento is a blob field in an Oracle DB, mapped with a 'blob' type with DAL

BUT (always have a but!), the blob content (PDF) seems to be encoded. When I open the resulting downloaded file it is not recognized as a PDF file. I put a print just after retrieve the field from DB and it seems enconded (base64?). But it´s another problem.

Fred

Em quinta-feira, 14 de novembro de 2013 14h21min37s UTC-3, Fred Guedes Pereira escreveu:

Niphlod

unread,
Nov 18, 2013, 10:54:20 AM11/18/13
to
you were the one saying that the column hold the content of the pdf file ^_^
DAL uses base64 to put the contents in.....
If you need to convert from base64, use a StringIO and then fill it with the decoded value, and stream that one.

Simone

Fred Guedes Pereira

unread,
Nov 18, 2013, 12:02:47 PM11/18/13
to web...@googlegroups.com
Hi Simone,

It really is a PDF file (I checked)! It´s a legacy database, the blob field was not filled by DAL, but by another system (in Java). I guess a base64 decode is going on incorrectly when the DAL reads the database. I tried to replace the Field type parameter from 'blob' to 'SQLCustomType', but it didnt work. It doenst read the field's value from DB.

Field("lb_documento", "blob")

Field("lb_documento", type=SQLCustomType(type='string', native='blob'))

In the first declaration the field came "base64 decoded". When using the second one, the blob field is not read from DB. 

Do you know any way to read a native blob field using DAL without decode it ?

Thanks a lot,
Fred

Em segunda-feira, 18 de novembro de 2013 12h53min39s UTC-3, Niphlod escreveu:
you were the one saying that the column hold the content of the pdf file ^_^
DAL uses base64 to put the contents in.....
If you need to convert from base64, use a StringIO and then fill it with the decoded value, and stream that one.

Simone

On Monday, November 18, 2013 4:05:49 PM UTC+1, Fred Guedes Pereira wrote:

Fred Guedes Pereira

unread,
Nov 18, 2013, 12:44:08 PM11/18/13
to web...@googlegroups.com
Simone and others,

I solved the DAL base64 encoding "problem". I read the field using cx_Oracle directly, not DAL. I dont know if there is another easier solution for this. Anyway, now I can go foward!

Follows the function to read the blob field:

def readPDFContent(query_parameters_here):
    blobselect = cx_Oracle.connect('usraudien/u$raud1en@SUAP')
    cursor = blobselect.cursor()
    cursor.execute(
                '''select your_blob_field
                    from ...
                ''', {query_parameters_here})
    row = cursor.fetchone()
    blobfield = row[0].read()   #read() must be before the cursor.close()!
    cursor.close()
    return blobfield

Fred
Reply all
Reply to author
Forward
0 new messages