Thank you for your very good suggestions.
Here is what i tried:
Putting the script into my print_countries view worked good.
The only Thing that bothered me was that i had to click the submit Button
twice.
The Problems i had using your alternative solution are best explained in my
attachment.
Thanks again!
Karl
The problem is that web2py.js automatically disables the submit buttons on
> forms after clicking, but it does not re-enable them unless the form is
> submitted via Ajax (under the assumption that a non-Ajax form submission
> will result in a new page being loaded in the browser, making it
> unnecessary to re-enable the button on the original page). This breaks down
> in your case because the form submission results in a file attachment being
> returned to the browser rather than a new page loading -- so the original
> page remains in the browser window.
>
> We should probably come up with a way to accommodate this case, but for
> now, you can add some JS code to immediately re-enable the button yourself.
> In the view, add something like:
>
> <script>
> jQuery(function() {
> jQuery(document).on('submit', 'form', function() {
> var submit_button = $(this).find(jQuery.web2py.formInputClickSelector
> );
> jQuery.web2py.enableElement(submit_button);
> });
> })
> </script>
>
> Alternatively, you can put the form in a web2py component (i.e., LOAD(...,
> ajax=True)), which will cause the form to be submitted via Ajax. You can
> then return the file via a separate action, which can be called via
> Javascript after the form is submitted:
>
> def print_countries():
> ...
> if form.process().accepted:
> response.js = 'window.location = "%s";' % URL('default', 'get_pdf'
> )
> return dict(form=form)
>
> Then move all the PDF creation code to the get_pdf function. In this case,
> print_countries would be used as an Ajax component within a containing
> page. When the form is submitted, response.js will be returned to the
> browser where it will be executed, resulting in the file being downloaded.
>
> Anthony
>
> On Saturday, November 7, 2015 at 3:45:07 AM UTC-5, Karl Florian wrote:
>>
>> Hi,
>> maybe some Pictures can demonstrate what i mean.The source code is also
>> attached:
>> This excample is of my Countries Report.
>>
>>
>>
>> <https://lh3.googleusercontent.com/-3qSbBrXf50Y/Vj22n9l93iI/AAAAAAAAAAw/siaI3TBfrNA/s1600/CountriesReport1.jpg>
>>
>>
>> <https://lh3.googleusercontent.com/--0nkparVSI0/Vj22ttxTSPI/AAAAAAAAAA4/oyZwenqUf2Y/s1600/CountriesReport2.jpg>
>>
>>
>> <https://lh3.googleusercontent.com/-Ulyv9Uo-U_Y/Vj22zDAUz4I/AAAAAAAAABA/XhiEfVLB8zs/s1600/CountriesReport3.jpg>
>>
>> <https://lh3.googleusercontent.com/-QigPM6MGLS8/Vj223l3AiwI/AAAAAAAAABI/oynrIlh7lcM/s1600/CountriesReport4.jpg>
>> First Picture is start of app
>> Second Picture is after Print Report Button was pressed
>> Third Picture is my pdf-file
>> Fourth Picture is after closing the pdf viewer
>>
>> Any suggestions will be appreciated.
>>
>> Karl:-) :) :] =)
>>
>> Am Freitag, 6. November 2015 20:36:33 UTC+1 schrieb Anthony:
>>
>>> Also, dict() is for creating a dictionary -- there is no reason to do
>>> dict(redirect(...)). Also, you do not return a redirect() -- you simply
>>> call it, and it immediately raises an HTTP exception.
>>>
>>> Anthony
>>>
>>> On Friday, November 6, 2015 at 1:50:15 PM UTC-5, Dave S wrote:
>>>>
>>>>
>>>>
>>>> On Friday, November 6, 2015 at 9:33:47 AM UTC-8, Karl Florian wrote:
>>>>>
>>>>> *Sorry everybody but i need your help again.*
>>>>>
>>>>> Here is my code:
>>>>>
>>>>> def languages_pedit():
>>>>>
>>>>
>>>> [...]
>>>>
>>>>
>>>>>
>>>>>
>>>>> * #I tried this mk_pdffile(pdffile,'languages.pdf') but it did not
>>>>> work*
>>>>>
>>>>> def mk_pdffile(pdffile,pdfname):
>>>>>
>>>>> import cStringIO
>>>>>
>>>>> pdfdata = open(pdffile,"rb").read()
>>>>>
>>>>> response.headers['Content-Type']='application/pdf'
>>>>>
>>>>> return response.stream(cStringIO.StringIO(pdfdata), attachment=True,
>>>>> filename=pdfname)
>>>>>
>>>>>
>>>> Where does the "does not work" part stop and the "this works fine part
>>>> start?
>>>>
>>>>>
>>>>>
>>>>> @auth.requires_login()
>>>>>
>>>>> def print_languages():
>>>>>
>>>> [...] .......................................
>>>>>
>>>>> #Code to make my texfile....................
>>>>>
>>>>> .......................................
>>>>>
>>>>> texfile.write('\\end{document}\n')
>>>>>
>>>>> [...]
>>>>
>>>>> return response.stream(cStringIO.StringIO(pdfdata), attachment=True,
>>>>> filename='languages.pdf')
>>>>>
>>>>>
>>>>>
>>>>> *This works fine but the code below does not get excecuted and my
>>>>> SUBMIT Button stays pressed?*
>>>>>
>>>>>
>>>>>
>>>>> return dict(redirect(URL('languages_pedit')))
>>>>>
>>>>>
>>>>
>>>> Where does this go? If right after the "return response.stream()",
>>>> then it will never get executed.
>>>>
>>>>
>>>>> def example():
>>>> return 1
>>>> return 2
>>>>
>>>>
>>>> will always return 1.
>>>>
>>>> /dps
>>>>
>>>>
>>>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.
### Model File ###
db.define_table(
'alcountries',
Field('ctry_iso', 'string', unique=True, length=2, label='Country-ISO'),
Field('description', 'string', length=80, requires=[IS_NOT_EMPTY()],
label='Description'),
Field('embargo_yn', 'boolean', label='Embargo-Y/N', comment='Test comment'),
Field('lang_iso', 'string',
length=2,requires=IS_IN_DB(db,db.allanguages.lang_iso,'allanguages.description'),
label='Language-ISO'),
Field('date_changed', 'datetime', update = request.now, writable = False,
label='Modified'),
Field('user_id', 'reference auth_user', update=auth.user and auth.user.id,
writable = False, readable = False),
migrate=settings.migrapp,
redefine=settings.redefin,
format='%(ctry_iso)s'
)
### Controller ###
@auth.requires_login()
def print_countries():
btn_list = [TAG.button(SPAN(_class="icon-print glyphicon
glyphicon-print"),' Report', _type='submit',
_id='submit_btn'),TAG.button(SPAN(_class="icon-refresh glyphicon
glyphicon-refresh"),' Refresh',_type='button',_onClick = "parent.location='%s'
" % URL('print_countries'))]
form = SQLFORM.factory(
Field('sortpdf', 'string', label='Sort-by',
requires=IS_IN_SET(['ISO-Code','Description']), default='ISO-Code'),
buttons = btn_list,
formstyle = 'table3cols',
submit_button='Submit')
if form.process().accepted:
response.js = 'window.location = "%s";' % URL('default',
'get_countriespdf', args=request.vars.sortpdf)
### redirect(URL('default',
'get_countriespdf',args=request.vars.sortpdf))
elif form.errors:
response.flash = 'form has errors'
response.title = 'Countries'
return dict(form=form)
def get_countriespdf():
sortpdf=str(request.args(0))
#if len(request.args):
response.flash = 'i am in get_countriespdf'
import datetime
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
#sortpdf = form.vars.sortpdf
if sortpdf=='ISO-Code':
ORDERBY=db.alcountries.ctry_iso
else: ORDERBY=db.alcountries.description
records = db(db.alcountries.id>=0).select(orderby=ORDERBY)
ltxfile = str(os.path.join(request.folder, 'tex', 'countries.tex'))
pdffile = str(os.path.join(request.folder, 'tex', 'countries.pdf'))
with open (ltxfile, 'wb') as texfile:
#...
### this is where i creat my texfile ###
#...
texfile.close()
os.system('pdflatex '+ltxfile)
if os.path.isfile(pdffile):
os.remove(pdffile)
os.rename('countries.pdf', pdffile)
import cStringIO
pdfdata = open(pdffile,"rb").read()
response.headers['Content-Type']='application/pdf'
return response.stream(cStringIO.StringIO(pdfdata), attachment=True,
filename='countries.pdf')
### print_countries view ###
{{extend 'layout.html'}}
{{block center}}
<div class="container-fluid" id="inputall">
<div class="col-md-3"></div>
<div class="col-md-6">
<H3 style="text-align:left;">{{=T(response.title)}}</H3>
{{=LOAD('default','get_countriespdf.load',ajax=True,target='get_countriespdf')}}
{{=form}}
</div>
<div class="col-md-3"></div>
</div>
<div style="clear: both;"></div>
{{end}}
If i do it this way, the pdf data gets mixed into my form view
if i use a additional get_countriespdf view like this, it does not work.
### get_countriespdf view ###
{{extend 'layout.html'}}
{{=LOAD('default','get_countriespdf.load',ajax=True,target='get_countriespdf')}}