【问题标题】:how to read a pdf in view.html without downloading the file如何在不下载文件的情况下阅读 view.html 中的 pdf
【发布时间】:2015-10-18 14:53:36
【问题描述】:

我正在尝试在不下载 pdf 的情况下读取 html 格式的 pdf 文件。向下代码适用于图像。我想代替图像阅读pdf。有可能吗。

db.define_table('mytable',
          Field('image', type='upload'))

控制器

def tables(): 
   return dict(tables=db().select(db.mytable.ALL))

查看

{{for table in tables:}}
    <img src="{{=URL('default', 'download', args=table.image)}}" /> <br /> 
{{pass}}

【问题讨论】:

  • stackoverflow.com/questions/11779246/… 有关于如何使用 Django 执行此操作的建议
  • 下载只是意味着“把东西送到电脑上”。您无法阅读不在计算机上的内容。还是我误解了你的问题?
  • 我有一张桌子,我正在播放 pdf 文件。我正在从表格中获取 pdf 并显示在 html 中

标签: python web2py


【解决方案1】:

您可以从 PDF 创建一个 png。 PDF 不是浏览器可以在 HTML 中呈现的东西(当然 Chrome 和 Firefox 可以打开 PDF;但这是全屏的,只是 pdf 本身;例如另一个 URL)。可能超出范围:在某些项目中,我使用 crontab 将上传的 PDF 转换为 jpg 以提供预览:

#Render all PDF's to thumb when no thumb is found
#convert -thumbnail x600 -background white "1.pdf"[0] "1.jpg"
import os,sys

OVERWRITE_EXISTING=False
PDFDIR='uploads'
THUMBDIR=os.path.join('static','thumbs')

import glob
import os

def insensitive_glob(pattern):
    def either(c):
        return '[%s%s]'%(c.lower(),c.upper()) if c.isalpha() else c
    return glob.glob(''.join(map(either,pattern)))

#get all the pdf file names in the current folder
os.chdir('uploads')
files = insensitive_glob("*.pdf")
# and convert each file if needed
for f in files:
    convert_needed=True
    newFile=os.path.join('..',THUMBDIR,'%s.jpg' % f[:-4])
    if not OVERWRITE_EXISTING:
        if os.path.exists(newFile):
            #print 'Warning:: .jpg already exists; skipping %s' % f 
            convert_needed=False
    if convert_needed:
        cmd='convert -density 144 %s[0] %s' % (f,newFile)
        print "Converting with: %s" % cmd
        os.system(cmd)

【讨论】:

猜你喜欢
  • 2016-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-25
  • 2011-12-31
相关资源
最近更新 更多