How can I open a temporary PDF in a new tab

712 views
Skip to first unread message

csavorgn

unread,
Apr 23, 2014, 9:55:50 AM4/23/14
to web...@googlegroups.com
Hi everyone,
I'm using a SQLFORM.factory to get the data I need to generate a PDF file using ReportLab.
I'm using a submit  button to check for the data entered and to open a new page using redirect:

redirect(URL("default","result", vars=dict(start=form.vars.date_start,
                                           
end=form.vars.date_end)))

In the controller default.py I defined
def result():
    start
= request.vars['start']
   
end = request.vars['end']
    filename
= os.path.join(request.folder,'private',str(uuid4()))
    create_pdf
(filename, db, start, end)
    response
.headers['Content-Type']='application/pdf'
    data
= open(filename,"rb").read()
    os
.unlink(filename)
   
return data

(The code I'm using is more complex than this.)

Maybe it is not the cleanest way to do this, but when I click on the button the temporary PDF opens on the same tab in the browser.

In my app I would like to get a different behaviour: when I click on the submit button the PDF should open on a new tab. The PDF should be temporary as it is in my current code.
How can I achieve the behaviour I want?

Thanks

Carlo

Oli

unread,
Apr 23, 2014, 10:00:26 AM4/23/14
to web...@googlegroups.com
 
URL('Link for a new tab', _class="btn" , _title=T("in a new tab"), _href=URL("default","yourcontroller",args=[row.id]), _target='_blank')

csavorgn

unread,
Apr 23, 2014, 12:53:19 PM4/23/14
to web...@googlegroups.com
I tried to use your code (with the obvious modifications) but I can't really figure out how to use it... 
Could you better explain your solution?
Thanks
Carlo

csavorgn

unread,
Apr 23, 2014, 5:17:09 PM4/23/14
to web...@googlegroups.com
Would there be a way to do what I explained if I give up using the SQLFORM.factory? How could I validate my form using e.g. JavaScript?

Oli

unread,
Apr 24, 2014, 6:18:54 AM4/24/14
to web...@googlegroups.com
 
example with a button. You can put the button in SQLFORM.factory:
 
def index():
    # in an new tab
    # _target='_blank'
 
    # set start and end for example with SQLFORM.factory
 
    button = A('create a pdf-report in an new tab', _class="btn" , _title=T("create PDF-report"), _href=URL("default","result"), args=[start, end]), _target='_blank')
 
    return dict(button=button)
def result():
    start
= request.vars['start']
   
end = request.vars['end']
    filename
= os.path.join(request.folder,'private',str(uuid4()))
    create_pdf
(filename, db, start, end)
    response
.headers['Content-Type']='application/pdf'
    data
= open(filename,"rb").read()
    os
.unlink(filename)
   
return data
 
 
Am Mittwoch, 23. April 2014 15:55:50 UTC+2 schrieb csavorgn:
Message has been deleted

csavorgn

unread,
Apr 24, 2014, 7:14:04 AM4/24/14
to web...@googlegroups.com
I managed to put the link in the SQLFORM.factory, but I don't manage to use the form validation. The reason is that "start"and "end"must be defined when the html page is created (to put their values in "button"). Is this correct or am I missing something? Thanks
Carlo
Reply all
Reply to author
Forward
0 new messages