Embed pdf and file download

548 views
Skip to first unread message

Gael Princivalle

unread,
Nov 20, 2015, 4:31:27 AM11/20/15
to web2py-users
Hello all.

I'm used to embed pdf files like that:
View:
    type="application/pdf"
    width="100%"
    height="100%">

Pdf file are uploaded by sftp.
It works well.

Now for another application files are uploaded in a table.
Model:
db.define_table('memos',
    Field('code', type='string', requires=IS_NOT_EMPTY()),
    Field('memo_file', 'upload'),
    auth.signature)

View:
<object data="{{=URL('download', args=grid_memos.view_form.record.memo_file, host=True, scheme=True)}}#toolbar=1&amp;navpanes=0&amp;scrollbar=1&amp;page=1&amp;view=FitH"
    type="application/pdf"
    width="100%"
    height="100%">

Pdf file is not displayed. I've tried with relative or absolute (host=True, scheme=True) path without success.
Here is the result in the html page:
Replacing in this new application {{=URL('download',... by a sftp downloaded file path from my other application http://www.domain1.com/pdf/file.pdf pdf file is displayed. And it's the same pdf file.

Someone knows why?

Thanks, regards.



Gael Princivalle

unread,
Nov 20, 2015, 10:26:39 AM11/20/15
to web2py-users
I've tried also with this plugin:

without success.

Gael Princivalle

unread,
Nov 20, 2015, 10:52:10 AM11/20/15
to web2py-users
My doubt was also about the file name, that could give problems perhaps, with all these dots.
But I've uploaded the file memos.memo_file.a1c04cabc73895b5.487964726f7665722d48313630312e706466.pdf on my ftp server and tried to embed it and it works.

It seems that's the fact that the pdf file is downloaded from a record make fails something.

Gael Princivalle

unread,
Nov 24, 2015, 2:53:41 AM11/24/15
to web2py-users
Someone have an idea?

Leonel Câmara

unread,
Nov 24, 2015, 10:35:40 AM11/24/15
to web2py-users
I'm guessing the problem is that response.download sets Content-Disposition as an attachment. Make another controller function in default.py

def stream():
   
import re
   
from pydal.exceptions import NotAuthorizedException, NotFoundException


   
if not request.args:
       
raise HTTP(404)
    name
= request.args[-1]
    items
= re.compile('(?P<table>.*?)\.(?P<field>.*?)\..*').match(name)
   
if not items:
       
raise HTTP(404)
   
(t, f) = (items.group('table'), items.group('field'))
   
try:
        field
= db[t][f]
   
except AttributeError:
       
raise HTTP(404)
   
try:
       
(filename, stream) = field.retrieve(name, nameonly=True)
   
except NotAuthorizedException:
       
raise HTTP(403)
   
except (NotFoundException, IOError):
       
raise HTTP(404)
   
return response.stream(stream)

Note that this function could be made a lot simpler but I wanted it to work with uploadseparate=True and to work for any kind of file you want to stream.

Now replace "download" with "stream" in your URL and you're done.

Gael Princivalle

unread,
Nov 26, 2015, 4:01:50 PM11/26/15
to web2py-users
Dear Leonel thanks a lot.

I've tried also with stream without success.
<object data="/stream/memos.memo_file.ac5f12657d916e96.456c656e636f5f646f63756d656e74695f63616e7469657265312e706466.pdf#toolbar=1&amp;navpanes=0&amp;scrollbar=1&amp;page=1&amp;view=FitH"

       
type="application/pdf"
       
width="100%"
       
height="100%">
</object>

I've tried also in another website that display pdf files with absolute pdf path, to display a downloaded file without success.
Have you already tried to embed a downloaded pdf file?

Mark Graves

unread,
Nov 28, 2015, 1:21:13 PM11/28/15
to web2py-users
I would personally use pdf.js.

Does that meet your needs?
-Mark

Leonel Câmara

unread,
Nov 28, 2015, 1:58:28 PM11/28/15
to web2py-users
Yes Gael I tried and that worked for me. I can provide a sample app if you need.

Gael Princivalle

unread,
Nov 28, 2015, 3:49:48 PM11/28/15
to web2py-users
Thanks Mark I'll try it.

Gael Princivalle

unread,
Nov 28, 2015, 3:50:54 PM11/28/15
to web2py-users
Leonel, if you can send me a sample app it will be really useful, thanks a lot.

Mark Graves

unread,
Nov 28, 2015, 11:03:33 PM11/28/15
to web...@googlegroups.com
https://bitbucket.org/MarkGraves/web2py_pdf_view

Here's a really basic example of pdf.js integration.

Basically, all I did was drop in pdf.js and then return it as a stream via ajax.

-Mark

--
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/EXYT8U_Ws0M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Leonel Câmara

unread,
Nov 30, 2015, 2:15:38 PM11/30/15
to web2py-users
Here's my test application which is working for me. To test I simply introduced a memo pdf file in appamin.
web2py.app.testpdf.w2p

Leonel Câmara

unread,
Nov 30, 2015, 2:15:46 PM11/30/15
to web2py-users
Here's my test application which is working for me. To test I simply introduced a memo pdf file in appadmin.
web2py.app.testpdf.w2p

Gael Princivalle

unread,
Dec 9, 2015, 8:14:43 AM12/9/15
to web2py-users
Thanks a lot Leonel. I have integrated the pdf.js like Mark have done (thanks Mark).
I've got some troubles also with pdf.js. With block center in the layout file pdf was not displayed.
{{block center}}
   
{{include}}
{{end}}

Without, pdf is displayed.
We talk about a custom grid view.
I've to test it but I can imagine that it's the same with your solution, pdf HTML5 object.

One thing that seems pdf.js do better is displaying pdf on mobile devices. Almost with my Android mobile phone pdf are displayed. With pdf HTML5 object no.

Gael Princivalle

unread,
Dec 9, 2015, 3:42:04 PM12/9/15
to web2py-users
I think the best solution is to use the actual version of pdf.js, that offer more functions to the user (zoom...) and perhaps will avoid some incompatibilities. For example pdf files saved by the Windows 10 scan software are not read by the pdf.js version that have used Mark.
Reply all
Reply to author
Forward
0 new messages