Appengine - Reportlab PDF生成

1 投票
1 回答
896 浏览
提问于 2025-04-16 03:36

我正在使用谷歌的应用引擎,想用reportlab生成一个PDF文件。这个应用运行得不错,可以生成像“你好,世界”这样的简单PDF,但我想要的是从用户填写的表单中获取数据,然后动态生成PDF。

有没有人能分享一段代码?我会非常感激。

1 个回答

2

我猜您是在使用网页应用框架。

import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.out.write("""
          <html>
            <body>
              <form action="/makepdf" method="post">
                <div><textarea name="content" rows="3" cols="60"></textarea></div>
                <div><input type="submit" value="Make a PDF for me"></div>
              </form>
            </body>
          </html>""")


class MakePDF(webapp.RequestHandler):
    def post(self):
        # now here you can make your PDF like you did for the "Hello world" one
        # and you can access the entered data like this: self.request.get('content')

application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/makepdf', MakePDF)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

撰写回答