0

我正在处理一项任务,我想使用 GAE 中的转换 API 将文本文件转换为 PDF,我尝试如下:https ://developers.google.com/appengine/docs/python/conversion/overview

这是我使用的代码:

from __future__ import with_statement
from google.appengine.api import files
import cgi, cgitb ; cgitb.enable()
import StringIO
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import mimetypes
from google.appengine.ext import blobstore
from mimetypes import guess_type
from google.appengine.api import conversion




def mime_type(filename):
    return guess_type(filename)[0]
class get(webapp.RequestHandler):
    def post(self):

        form = cgi.FieldStorage() 
        file_upload = form['file']  
        name=file_upload.filename  

        m=mimetypes.guess_type(name)[0]

        data=file_upload.file.read()
        buf = StringIO.StringIO()
        asset = conversion.Asset("text/plain", data, file_upload.filename)
        conversion_obj = conversion.ConversionRequest(asset, "application/pdf")
        result = conversion.convert(conversion_obj)
        if result.assets:
             for asset in result.assets:
                buf.write(asset.data)

        else:
             print "ERROR" 


        u_file = files.blobstore.create(mime_type="application/pdf",_blobinfo_uploaded_filename="test.pdf")
        data=buf.getvalue()
        with files.open(u_file, 'a') as f:
            f.write(data)
        files.finalize(u_file)
        blob_key = files.blobstore.get_blob_key(u_file)
        blob_info = blobstore.get(blob_key)
        name2 =  blob_info.filename   
        self.response.out.write("""<html><br><body style="background-color:#CC9999"><b><font size="5" face="Batang" ><center> <li ><a href="download.py?blob_key=%s" style="color:black">%s

               </center></font><hr></body></html>            
                """ % (str(blob_key),str(name2)))




def main():

    application = webapp.WSGIApplication( [(r'/get.py', get)], debug=True)

    run_wsgi_app(application)


if __name__ == "__main__":
    main()

下载.py:

from __future__ import with_statement
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp.util import run_wsgi_app
from mimetypes import guess_type

def mime_type(filename):
    return guess_type(filename)[0]
class Thumbnailer(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self):
        blob_key = self.request.get("blob_key")
        if blob_key:
            blob_info = blobstore.get(blob_key)


            if blob_info:
                save_as1 =  blob_info.filename
                type2=mime_type(blob_info.filename)
                self.send_blob(blob_info,content_type=type2,save_as=save_as1)

                return


def main():


    application = webapp.WSGIApplication([
            (r'/download.*', Thumbnailer),
        ], debug=True)
    run_wsgi_app(application)



if __name__ == '__main__':
    main()

编辑:我编辑了代码,当我尝试时print buf.getvalue(),我得到:加载pdf文档失败。但是当我尝试打开这个 PDF 时,我无法打开它,并且 adobe reader 立即给出错误,它无法打开文件。对不起,我还是初学者,有什么帮助吗?欢迎任何建议。

4

2 回答 2

1

这是我的工作代码示例(虽然它适用于 Python2.7 运行时)。祝你好运!


import os
import StringIO

from google.appengine.api import conversion
from google.appengine.api import files
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers

import jinja2

template_dir = os.path.join(os.path.dirname(__file__), 'templates')

jinja2_env = jinja2.Environment(
    loader=jinja2.FileSystemLoader(template_dir))


class MainHandler(webapp.RequestHandler):
    def get(self):
        tmpl = jinja2_env.get_template('index.jinja2')
        self.response.out.write(tmpl.render())


class PostHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def post(self):
        data = self.request.get('file')
        asset = conversion.Asset('text/plain', data, 'test.txt')
        conversion_obj = conversion.Conversion(asset, 'application/pdf')
        result = conversion.convert(conversion_obj)
        buf = StringIO.StringIO()
        if result.assets:
            for asset in result.assets:
                buf.write(asset.data)
        else:
            raise Exception('Conversion failed.')
        u_file = files.blobstore.create(mime_type='application/pdf',
                                        _blobinfo_uploaded_filename='test.pdf')
        with files.open(u_file, 'a') as f:
            f.write(buf.getvalue())
        files.finalize(u_file)
        blob_key = files.blobstore.get_blob_key(u_file)
        blob_info = blobstore.get(blob_key)
        self.send_blob(blob_info)


app = webapp.WSGIApplication([
    ('/', MainHandler),
    ('/post_file', PostHandler),
],
debug=True)
于 2012-04-26T17:01:43.010 回答
0

您确定您的“数据”(由 检索data=file_upload.file.read())是正确的吗?

也许您可以停止使用 cgi 模块,而只需使用 webapp 为您创建的请求对象,例如:

data = self.request.get('file')

顺便说一句,请不要使用私有属性,例如asset._data,使用asset.data。此外,StringIO 的用途是什么?您是否考虑过直接写入asset.data打开的文件?

更新:我想我找到了原因。

转换后,请执行以下操作:

if result.assets:
    for asset in result.assets
    buf.write(asset.data)
于 2012-04-26T06:24:01.487 回答