How to get my pdf-file to display from webserver

307 views
Skip to first unread message

Karl Florian

unread,
Nov 2, 2015, 10:29:25 AM11/2/15
to web2py-users

Hi,
i am a newby to web2py and need some help dislaying label in my application.
 
I’m using Latex and PSTricks to create my barcode label.pdf files.
The problem is, everthing works ok on local Windows or Ubuntu but not from my Linux-VServer (WEbServer).
The label.pdf is created but not displayed.

This my sourccode after the place where i creating my label.pdf file:

//********************************************************
This does not work on webserver (only in local Systems):
 ...............
        os.system('latex label.tex')
        time.sleep(0.1)
        if sys.platform == "win32":
             os.system('dvips -o label-pics.ps label.dvi')
                os.system('ps2pdf awblabel-pics.ps label.pdf')
               os.startfile('label.pdf')
        else:
                # os.system('dvipdf label.dvi')
                os.system('dvips -o awblabel-pics.ps label.dvi')
                os.system('ps2pdf awblabel-pics.ps label.pdf')
               if sys.platform == "darwin": subprocess.call(['open', 'label.pdf'])
                else: subprocess.call(['xdg-open', 'label.pdf'])
    return dict(redirect(URL('labelapp', args=req_id)))

//********************************************************
This works, however my SUBMIT Button does not get released:
 ...................
 else:
    mypdffile=os.path.join(request.folder, 'pdffiles', 'label.pdf' )
    pdfdata = open(mypdffile,"rb").read()
    os.unlink(mypdffile)
    response.headers['Content-Type']='application/pdf'
    return pdfdata
return dict(redirect(URL('labelapp', args=req_id)))

//********************************************************

Do i have to use something like PDFObject.js or PDF.js or is there a better way to do it?

Where can i find examples that use PDFObject.js or PDF.js?
.........................

I just can not find any good examples.

As i said i am new to web programming and web2py.


I think web2py is a cool product.
Hope someone can help.

Massimiliano

unread,
Nov 3, 2015, 6:02:05 AM11/3/15
to web...@googlegroups.com
Try this way to display your pdf:

def yourcontroller()
    # generate pdf
    . . . 
    import cStringIO
    data = open(filename,"rb").read()
    response.headers['Content-Type']='application/pdf'     
    return response.stream(cStringIO.StringIO(data))  




--
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.



--
Massimiliano

Karl Florian

unread,
Nov 3, 2015, 10:38:06 AM11/3/15
to web2py-users

Hi Massimiliano,
it works thank you!

However there are 2 things i do not like:

1) My PDF File is renamed to the same as my Controller function Name plus a number and without the Extension .pdf.
Example: printlanguages+cd123452

2) After the "return response.stream()" my submit button that started the Output remains pressed until i refresh the url-page manually.

Is there a way to Redirect to the URL after the pdf file was displayed?

Do you or does anybody else have a solution for this?

Karl Florian

unread,
Nov 3, 2015, 4:26:26 PM11/3/15
to web2py-users
I think i found my own solution.
Wo ever has the same Problem, try this!!!!!!!!!!!!!!!

import webbrowser
webbrowser.open_new(r'file://C:\path\to\file.pdf')


It works just the way i Need it.

Dave S

unread,
Nov 3, 2015, 5:31:07 PM11/3/15
to web2py-users


On Tuesday, November 3, 2015 at 1:26:26 PM UTC-8, Karl Florian wrote:
I think i found my own solution.
Wo ever has the same Problem, try this!!!!!!!!!!!!!!!

import webbrowser
webbrowser.open_new(r'file://C:\path\to\file.pdf')


It works just the way i Need it.



I'm glad it works for you, but it won't work on a lot of systems (Linux systems usually don't know what "C:" means, and their slashes are forward leaning).

You might look at using response.stream, which has an option for setting a filename (sets the header field "Content-Disposition"), and the browser should send that to a sensible place, like the Downloads directory.

<URL:http://web2py.com/books/default/chapter/29/04/the-core?search=content-type#response>

/dps

 

Karl Florian

unread,
Nov 3, 2015, 6:30:59 PM11/3/15
to web2py-users
No it didn't work on the web.
local Linux and Windows worked fine.
I am using str(os.path.join(request.folder, 'tex', 'languages.tex')) to set the path to my files.

I am using Massimiliano's code to start my pdf's.
But i still have the 2 Problems i mentioned before.

Dave S

unread,
Nov 3, 2015, 7:38:15 PM11/3/15
to web2py-users


On Tuesday, November 3, 2015 at 3:30:59 PM UTC-8, Karl Florian wrote:
No it didn't work on the web.
local Linux and Windows worked fine.
I am using str(os.path.join(request.folder, 'tex', 'languages.tex')) to set the path to my files.

I am using Massimiliano's code to start my pdf's.
But i still have the 2 Problems i mentioned before.

I'm not sure about the button problem, but response.stream's filename parameter should help with the ugly name. 


 

Am Dienstag, 3. November 2015 23:31:07 UTC+1 schrieb Dave S:


On Tuesday, November 3, 2015 at 1:26:26 PM UTC-8, Karl Florian wrote:
I think i found my own solution.
Wo ever has the same Problem, try this!!!!!!!!!!!!!!!

import webbrowser
webbrowser.open_new(r'file://C:\path\to\file.pdf')


It works just the way i Need it.



I'm glad it works for you, but it won't work on a lot of systems (Linux systems usually don't know what "C:" means, and their slashes are forward leaning).

You might look at using response.stream,

[sorry, careless reading on the first pass]
 
 
/dps

Karl Florian

unread,
Nov 4, 2015, 5:39:49 AM11/4/15
to web2py-users
Thanks!

The first part is working!!! The filename is as it is supposed to be!
This is my code at the Moment, maybe some can use it:

  ltxfile = str(os.path.join(request.folder, 'tex', 'languages.tex'))

  pdffile = str(os.path.join(request.folder, 'tex', 'languages.pdf'))

  #------------------------------------

  #Create and Close the TEX-File .......

  #------------------------------------

  os.system('pdflatex '+ltxfile)  #create the PDF file

  if os.path.isfile(pdffile):        #remove old file from the tex directory

      os.remove(pdffile)

  os.rename('languages.pdf', pdffile)     #move PDF file to the tex directory

  import cStringIO

  pdfdata = open(pdffile,"rb").read()

  response.headers['Content-Type']='application/pdf'

  return response.stream(cStringIO.StringIO(pdfdata), attachment=True, filename='languages.pdf')

  elif form.errors:

      response.flash = 'form has errors'

 

  response.title = 'All Languages'

  return dict(form=form)

 

The second Problem i didn't solve yet, i Need to make a refresh on my sqlform.factory after the PDF file was displayed.

Thanks everybody for helping.
Reply all
Reply to author
Forward
0 new messages