import os
import sys
pdf = receipt_PDF()
pdf.add_page()
rcpt_filename = "RCPT_%s_%s.pdf" % (session.rcpt_number, session.rcpt_recipient)
rcpt_filename = rcpt_filename.replace( ' ', '_' )
if sys.platform.startswith( "linux" ) :
os.system( "xdg-open ./%s" %(rcpt_filename) )
else :
os.system( "./%s" %(rcpt_filename) )
#print "<pdf_invoice> inv_filename=%s" %(inv_filename)
response.headers['Content-Type'] = 'application/pdf'
#response.headers['Content-disposition'] = 'attachment; filename=%s' % rcpt_filename
pdf.output( rcpt_filename, dest='F' )
what I would like it to do is...
FPDF is a brilliant contribution to the web2py armoury and again I am
chuffed to have gotten this far with it. The blockage for me is that the
documentation is somewhat disconnected in that none of the examples are in the context of web2py environment e.g.whether and how to use pdf.output vs response.stream
I have been reading as much as I can find but nothing seems to match exactly what I want to do and despite trying dozens of variations nothing gets me closer without breaking something else.
It seems I need to drop the xdg portion, that the header's content type = attachment will force the download dialogue, and, I think, I need to be using file-like object (but is the FPDF object not this?) with Stringio in the mix?
Any assistance with the specifics needed to accomplish what I am looking for above would be appreciated!
Peter
So this is better but I still need some direction...
(thanks to Paul Rykiel's post re labels and responders including one Mariano Reingart)...and hope it helps someone else down the road.
pdf = receipt_PDF()
pdf.add_page()
rcpt_filename = "RCPT_%s_%s.pdf" % (session.rcpt_number, session.rcpt_recipient)
rcpt_filename = rcpt_filename.replace( ' ', '_' )
rcpt_filepath = request.folder + '/static/temp/%s'%(rcpt_filename)
if sys.platform.startswith( "linux" ) :
os.system( "xdg-open %s" %(rcpt_filepath) )
else :
os.system( "%s" %(rcpt_filepath) )
pdf.output(rcpt_filepath,dest='S')
response.headers['Content-Type'] = 'application/pdf'
return response.stream(rcpt_filepath, chunk_size=4096, request=request, attachment=True, filename=rcpt_filename)
(It also creates temp files on the system that may/may not need to be cleaned up - but that's for another day.)
Can I stop it automatically opening the file in Adobe and if so how?
Peter
...
s_io = pdf.output(dest='S')
response.headers['Content-Type']='application/pdf'response.headers['Content-Disposition'] = 'attachment; filename="%s"' % file_name
raise HTTP(200, s_io, **response.headers)
# pdf.output(rcpt_filepath,dest='S')
# response.headers['Content-Type'] = 'application/pdf'
# return response.stream(rcpt_filepath, chunk_size=4096, request=request, attachment=True, filename=rcpt_filename)
s_io = pdf.output(dest='S')
response.headers['Content-Type']='application/pdf'
response.headers['Content-Disposition'] = 'attachment; filename="%s"' % rcpt_filename
raise HTTP(200, s_io, **response.headers
Without setting the 'Content-disposition' the pdf will be opened in the browser.
response.stream (....,attachment=True, filename=rcpt_filename)
response.headers['Content-Disposition'] = 'attachment; filename="%s"' % rcpt_filename
halt. downloading is something the browser can decide. opening the saved file is something your operating system does.The browser can only send the response which gives the user a choice between saving and opening directly the file.If the user chooses "open" instead of "save" ... nothing can prevent that.
import os
import sys
pdf = receipt_PDF()
pdf.add_page()
rcpt_filename = "RCPT_%s_%s.pdf" % (session.rcpt_number, session.rcpt_recipient)
rcpt_filename = rcpt_filename.replace( ' ', '_' )
rcpt_filename = rcpt_filename.replace( '&', 'and' ) # TODO read somewhere there is a 'clean' option in web2py for filepath?
rcpt_filepath = request.folder + 'static/temp/%s'%(rcpt_filename) # Note I have removed leading '/' - long story!
s_io = pdf.output(dest='S')
response.headers['Content-Type']='application/pdf'
response.headers['Content-Disposition'] = 'attachment; filename="%s"' % rcpt_filename
raise HTTP(200, s_io, **response.headers)