3

I have a problem with opening PDF file.

(i am using Ladon and Python under mod_wsgi working with Apache2. so on a ubuntu apache server system- switched to windows system)

I'm trying to run the following Python script:

(where, str_pdf_file_name = '/var/www/Accounting_Engine/pdfDocuments/File_name.pdf')

def preview_pdf(self,str_pdf_file_name):
    try:
                if sys.version_info[:2] > (2,3):
                    import subprocess
                    from subprocess import Popen, PIPE
                    k = subprocess.Popen(['evince', str_pdf_file_name],stdout=PIPE)
                    print k.communicate()
                else:
                    os.spawnlp(os.P_NOWAIT, 'evince', 'evince', str_pdf_file_name)
    except Exception,msg:
        return str(msg)
    return str_pdf_file_name

I have disabled the stdin and stdout restrictions in apache2.conf file as

WSGIRestrictStdin Off    
WSGIRestrictStdout Off

and now the error.log file shows (/var/log/apache2/error.log):

Cannot parse arguments: Cannot open display: [Wed Oct 10 11:50:24 2012] [error] ('', None)

I have checked os.environ also. It shows like:

{'LANG': 'C', 'APACHE_RUN_USER': 'www-data', 'APACHE_PID_FILE': '/var/run/apache2.pid', 'PWD': '/home/user', 'APACHE_RUN_GROUP': 'www-data', 'PATH': '/usr/local/bin:/usr/bin:/bin'}

I have also tried by changing the PWD by using the following codes:

import pwd
os.environ["PWD"] = pwd.getpwuid(os.getuid()).pw_dir    # Because PWD of 'www-data' was '/var/www'
k = subprocess.Popen(['evince', str_pdf_file_name],env=os.environ,stdout=PIPE)

But their is no change in output. It still shows

'Cannot parse arguments: Cannot open display:'

Any suggestions what could be the cause and how to solve this?

Sebass van Boxel
  • 2,514
  • 1
  • 22
  • 37
SKT
  • 83
  • 1
  • 10

3 Answers3

2

evince is obviously a tool having a GUI. You try to open it from inside a server process, which usually cannot interact with the desktop/user. On Linux it probably requires a X server to running, which is likely not the case on your server. And that's exactly what the error message is telling you. I think you have to rethink your solution.

Update:

As it's not obvious, if you just look for the accepted answer and don't read the comments: The original poster solved his problem by using the download method described here

Achim
  • 15,415
  • 15
  • 80
  • 144
  • HI, thanks for trying to help. As I am a beginner with python web wervices, would you please suggest me any another solution to open the pdf file. – SKT Oct 10 '12 at 08:58
  • Can you tell us, what you want to do? Opening a PDF on a server makes usually absolutly no sense at all. So either you are completly on the wrong track or you are building something very strange which I don't get yet?! ;-) – Achim Oct 10 '12 at 09:00
  • :-) Oops.. Yap here it is. I have to generate a pdf report, it should be generated and displayed when clickin on a preview link in my site. I generated it in server, but I have no idea how to show it to the users (client). Is there any other way to open pdf file in web service, – SKT Oct 10 '12 at 09:23
  • You have to generate the report on the server, but then you have to send the generated data to the browser. It then depends on your users browser how the file is processed. A browser having some matching plugins installed (Acrobat Reader for example) will open the PDF and display it. Other browsers will offer the client to save the file. – Achim Oct 10 '12 at 09:32
  • Great!. Thank u. I got a new link, hopes it will help, [link]http://ladonize.org/index.php/Ladon_Attachments – SKT Oct 10 '12 at 09:46
2

The following link could be useful for those who have the same problem.

http://ladonize.org/index.php/Ladon_Attachments

The download function() in the above link will be helpful.

SKT
  • 83
  • 1
  • 10
0
#   Fetch the pdf file from the server,  and keep it in the local matchine
    str_loc_file_path = self.fetch_generated_pdf_file_from_server(str_file_path)

#   Display the fetched file in local matchine
    self.print_pdf(str_loc_file_path)

def fetch_generated_pdf_file_from_server(self, str_file_path,*args):
    import httplib
    import urllib
    conn = httplib.HTTPConnection("192.168.2.10")
    str_loc_file_path = '/home/sreejith/pdfDocuments/__filenew__.pdf'
    locfile = open(str_loc_file_path,'w')
    conn.request("POST",str_file_path)
    response = conn.getresponse()
    locfile.write(response.read())
    locfile.close()
    print "Server Response status is"+str(response.status)+", Because of response "+str(response.reason)
    print response.getheaders()    
    return str_loc_file_path

def print_pdf(self,str_file_name,*args):     # PRINT THE PDF FILE, str_file_name.
    try:
        if sys.platform == "win32":      # IS OS == WINDOWS
            import win32api          # PRINT WITH ADOBE VIEWER. SO IT MUST BE INSTALLED
            win32api.ShellExecute(0, "print", str_file_name, None, ".", 0)
        else:                # CAN'T PRINT IF OS IS LINUX, SO OPEN BY A VIEWER FROM WHICH HE CAN PRINT
            if sys.version_info[:2] > (2,3):
                import subprocess
                from subprocess import PIPE
                k = subprocess.Popen(['evince', str_file_name],stdout=PIPE)
                k.communicate()

            else:
                k = os.spawnlp(os.P_NOWAIT, 'evince', 'evince', str_file_name)
    except Exception,msg:
        print str(msg)
        msgDlg = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK)
        msgDlg.set_markup("<span foreground='blue'>Printer Is Not Connected. And "+str(msg)+".</span>")
        msgDlg.run()
        msgDlg.destroy()      
    pass

This works for me. :-) Hopes helpful for somebody.

SKT
  • 83
  • 1
  • 10