Opening PDF files in web browser

842 views
Skip to first unread message

José Eloy

unread,
Jul 22, 2014, 2:41:59 PM7/22/14
to web...@googlegroups.com
Hello!

I've developed
an application that allows users to upload document files (doc, pdf, xls) and then download them. The problem is when the user wants download the files the web browser always opens a window to ask where to download them (or the file is downloaded in the download folder). In the case of doc and xls files is fine, but would it be possible that the pdf files will open directly in the web browser?. To upload/download the files I use the standard way of web2py. This problem occurs with Firefox, Safari, Chrome, IE 10. The version of web2py I use with the application is the 1.99.7.

I hope you can understand me, my english is not very good.

Thanks ins advanced.

Regards

José Eloy Torres

Leonel Câmara

unread,
Jul 22, 2014, 3:11:48 PM7/22/14
to
You need to change your download for PDFs controller, this would work if you don't use uploadseparate (which I usually do) :

def download_pdf():
      filename=request.args[0]
      path=os.path.join(request.folder,'uploads', filename)
      response.headers['ContentType'] ="application/pdf"
      response.headers['Content-Disposition']="inline; " + filename
      return response.stream(open(filename), chunk_size=65536)

Michele Comitini

unread,
Jul 22, 2014, 3:30:29 PM7/22/14
to web...@googlegroups.com
If you want to avoid the Acrobat plugin entirely and show pdf inline:
https://github.com/mozilla/pdf.js
It's already included in Firefox, but the other browsers can use it too


2014-07-22 21:11 GMT+02:00 Leonel Câmara <leonel...@gmail.com>:
You need to change your download for PDFs controller, this would work if you don't use uploadseparate (which I usually do) :

def download_pdf():
      filename=request.args[0]
      path=os.path.join(request.folder,'uploads', filename)
      response.headers['ContentType'] ="application/pdf"
      response.headers['Content-Disposition']="inline; filename.pdf"  
+filename
      return response.stream(open(filename), chunk_size=65536)

--
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 web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

José Eloy Torres Monreal

unread,
Oct 23, 2014, 2:32:44 PM10/23/14
to web...@googlegroups.com
Sorry for my delay in answer. I've taken back the project.

The solution of Leonel works well if I want to show an pdf from
another website, but when I try to download a file loaded in my own
web site (Web2py's upload/download method) I get the message that the
file is not found.

I show you the table definition:

db.define_table('enlacesweb_documentos',
Field('categoria', db.categorias_documentos),
Field('nombre'),
Field('documento', 'upload',
uploadfolder=os.path.join(request.folder,'uploads/docs_y_normatividad')),
Field('fecha', 'date'),
Field('descripcion_documento', 'text'),
Field('muestra_en_portada','boolean'),
Field('imagen_portada', 'upload',
uploadfolder=os.path.join(request.folder,'uploads/docs_y_normatividad')),
Field('texto_imagen_portada', label="Texto de la
imagen en portada"),
format='%(categoria)s')


As you see I'm using an subfolder inside uploads folder.

The download function, serving swf, pdf and another type of files:

def download():
"""
allows downloading of uploaded files
http://..../[app]/default/download/[filename]
"""

import os

if (request.args(0) or '').endswith('swf'):

response.headers['Content-Disposition'] = ''
filename = os.path.join(request.folder, 'uploads', request.args(0))

return response.stream(open(filename,'rb'))

elif (request.args(0) or '').endswith('pdf'):

filename=os.path.join(request.folder, 'uploads', request.args(0))
response.headers['ContentType'] ="application/pdf"
response.headers['Content-Disposition']="inline; filename.pdf"
+ filename

return response.stream(open(filename), chunk_size=65536)

else:
return response.download(request,db)


The elif section works well with external pdf to show, but for those
uploaded with Web2py's upload method I get the error the pdf is not
found, if I omit the elif and use the else I can to download the pdf
file, but I want the web browser open it.

I hope you can understand me.

How to fix this?

Thanks in advanced.



2014-07-22 14:11 GMT-05:00 Leonel Câmara <leonel...@gmail.com>:
> You need to change your download for PDFs controller, this would work if you
> don't use uploadseparate (which I usually do) :
>
> def download_pdf():
> filename=request.args[0]
> path=os.path.join(request.folder,'uploads', filename)
> response.headers['ContentType'] ="application/pdf"
> response.headers['Content-Disposition']="inline; filename.pdf"
> +filename
> return response.stream(open(filename), chunk_size=65536)
>
> --
> 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 a topic in the
> Google Groups "web2py-users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/web2py/uu7Bd7aSVLQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

José Eloy

unread,
Oct 23, 2014, 2:34:54 PM10/23/14
to web...@googlegroups.com

José Eloy

unread,
Oct 23, 2014, 3:39:33 PM10/23/14
to web...@googlegroups.com
Hello.

I've discovered if I use

    return response.download(request, db, attachment=False)

in the else statement of the download function of my code, I can do the web browser can open the pdf file, but I miss the original filename. How I can to recover it?

Regards.

José Eloy

unread,
Oct 24, 2014, 1:32:08 PM10/24/14
to web...@googlegroups.com
Any Idea?

Regards

kama

unread,
Jun 17, 2015, 2:57:45 AM6/17/15
to web...@googlegroups.com
But, if I have a PDF stored in a blob field, how could I use PDF.js do display it?


Il giorno venerdì 24 ottobre 2014 19:32:08 UTC+2, José Eloy ha scritto:
Any Idea?

Regards
Reply all
Reply to author
Forward
0 new messages