微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用cgi python脚本在浏览器中显示pdf文件内容及其全名?

我希望显示pdf文件的完整路径及其在浏览器上显示内容.我的脚本有一个输入html,用户将输入文件名并提交表单.该脚本将搜索文件,如果在子目录中找到该文件,则将文件内容输出到浏览器中并显示名称.我能够显示内容,但也无法同时显示完整的名字.如果我显示文件名,我会看到内容垃圾字符显示.请指导.

enter link description here

脚本a.py:

import os
import cgi
import cgitb 
cgitb.enable()
import sys
import webbrowser

def check_file_extension(display_file):
    input_file = display_file
    nm,file_extension = os.path.splitext(display_file)
    return file_extension

form = cgi.FieldStorage()

type_of_file =''
file_nm = ''
nm =''
not_found = 3

if form.has_key("file1"):
    file_nm = form["file1"].value

type_of_file = check_file_extension(file_nm)

pdf_paths = [ '/home/nancy/Documents/',]

# Change the path while executing on the server,else it will throw error 500
image_paths = [ '/home/nancy/Documents/']


if type_of_file == '.pdf':
    search_paths = pdf_paths
else:
    # .jpg
    search_paths = image_paths
for path in search_paths:
    for root,dirnames,filenames in os.walk(path):
        for f in filenames:
            if f == str(file_nm).strip():
                absolute_path_of_file = os.path.join(root,f)
                # print 'Content-type: text/html\n\n'
                # print 'display.py />
# %s not found' % absolute_path_of_file

html是一个常规的html,文件名只有1个输入字段.

最佳答案
这不可能.至少不那么简单.某些Web浏览器不显示PDF但要求用户下载文件,有些会自己显示文件,有些则嵌入外部PDF查看器组件,有些则启动外部PDF查看器.没有标准的跨浏览器方式将PDF嵌入到HTML中,如果要显示任意文本和PDF内容,则需要这样做.

在每个浏览器上工作的后备解决方案是将服务器上的PDF页面呈现为图像并将其提供给客户端.这给服务器带来了一些压力(处理器,内存/磁盘用于缓存,带宽).

一些支持HTML5的现代浏览器可以在canvas元素上呈现带有Mozilla’s pdf.js的PDF.

对于其他人,您可以尝试使用< embed> /< object>使用Adobe的插件作为described on Adobe’s The PDF Developer Junkie Blog.

在服务器上呈现页面

渲染和提供PDF页面作为图像需要服务器上的一些软件来查询页面数量提取和呈现给定页面作为图像.

可以使用Xpdf或libpoppler命令行实用程序中的pdfinfo程序确定页数.将页面从PDF文件转换为JPG图像可以通过ImageMagick工具转换完成.使用这些程序的一个非常简单的CGI程序:

#!/usr/bin/env python
import cgi
import cgitb; cgitb.enable()
import os
from itertools import imap
from subprocess import check_output

PDFINFO = '/usr/bin/pdfinfo'
CONVERT = '/usr/bin/convert'
DOC_ROOT = '/home/bj/Documents'

BASE_TEMPLATE = (
    'Content-type: text/html\n\n'
    'nes())
    )
    return int(info['Pages'])


def get_page(filename,page_index):
    return check_output(
        [
            CONVERT,'-density','96','{0}[{1}]'.format(filename,page_index),'jpg:-'
        ]
    )


def send_error(message):
    print BASE_TEMPLATE.format(
        title='Error',body='irst('file')
    page_number = int(form.getfirst('page',1))
    type_ = form.getfirst('type','html')

    pdf_path = os.path.abspath(os.path.join(DOC_ROOT,filename))
    if os.path.exists(pdf_path) and pdf_path.startswith(DOC_ROOT):
        page_count = get_page_count(pdf_path)
        page_number = min(max(1,page_number),page_count)
        TYPE2SEND_FUNCTION[type_](pdf_path,page_count)
    else:
        send_error(
            '

libpoppler有Python绑定,因此可以很容易地用该模块替换对外部pdfinfo程序的调用.它还可以用于提取页面的更多信息,例如PDF页面上的链接,以便为它们创建HTML图像映射.安装了libcairo Python绑定后,甚至可以在没有外部进程的情况下进行页面呈现.

原文地址:https://www.jb51.cc/html/426073.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐