3

我在打开 PDF 文件时遇到问题。

(我在 mod_wsgi 下使用 Ladon 和 Python 与 Apache2 一起工作。所以在 ubuntu apache 服务器系统上 - 切换到 Windows 系统)

我正在尝试运行以下 Python 脚本:

(其中,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

我已将 apache2.conf 文件中的标准输入和标准输出限制禁用为

WSGIRestrictStdin Off    
WSGIRestrictStdout Off

现在 error.log 文件显示 ( /var/log/apache2/error.log):

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

我也检查了 os.environ 。它显示如下:

{'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'}

我还尝试使用以下代码更改 PWD:

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)

但是他们的输出没有变化。它仍然显示

'Cannot parse arguments: Cannot open display:'

任何建议可能是什么原因以及如何解决这个问题?

4

3 回答 3

2

evince显然是一个具有 GUI 的工具。您尝试从通常无法与桌面/用户交互的服务器进程内部打开它。在 Linux 上,它可能需要运行 X 服务器,而在您的服务器上可能并非如此。这正是错误消息告诉您的内容。我认为你必须重新考虑你的解决方案。

更新:

由于这并不明显,如果您只是寻找接受的答案而不阅读评论:原始发布者通过使用此处download描述的方法解决了他的问题

于 2012-10-10T08:29:12.857 回答
2

以下链接可能对有同样问题的人有用。

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

上面链接中的下载函数()会有所帮助。

于 2012-10-10T11:48:07.613 回答
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

这对我有用。:-) 希望对某人有所帮助。

于 2012-10-23T07:00:59.203 回答