【问题标题】:How to generate a pdf file from a html page in webapp2如何从 webapp2 中的 html 页面生成 pdf 文件
【发布时间】:2012-07-09 15:06:21
【问题描述】:

我需要一个代码 sn-p 从 HTML 页面生成 PDF 文件,该文件可以包含图像和一些 CSS。

我将 Google AppEngine 与 Python 2.7 和 webapp2(不是 Django)一起使用。

我知道这个问题可能值得一票否决,因为我在提问之前似乎没有进行足够的研究,但我实际上做了并且找不到一个具体的例子来说明如何做到这一点。

【问题讨论】:

    标签: google-app-engine pdf webapp2 html2pdf pisa


    【解决方案1】:

    GAE 提供conversion API,它能够将 html 转换为 PDF(以及其他)。有关如何将带有图像的 html 转换为 PDF 的示例,请参阅问题 Incomplete Google App Engine Documentation on Conversion API 的答案。如果需要包含 CSS,则需要将其包含在 html 中。

    【讨论】:

    • 更新:GAE 覆盖 API 是实验性的。现在它已经退役,不能使用了。
    【解决方案2】:

    在将 html 转换为 pdf 时,python 没有原生支持。但是,您可能想要搜索某人编写的具有该功能的库。请参阅http://www.xhtml2pdf.com/ 了解更多信息。

    在您设法将 pdf 数据写入字符串后,只需使用 webapp2 设置 pdf 标题,您就可以将 pdf 文件提供给客户端浏览器。

    # In request handler 
    self.response.headers['Content-Type'] = 'application/pdf'
    self.response.headers['Content-Disposition'] = 'attachment;filename=downloaded.pdf'
    self.response.write(pdfdata)
    

    【讨论】:

    【解决方案3】:

    我制作了 xhtml2pdf 以在 Google AppEngine Python 上工作。

    只需将您的 html 字符串作为 (get/post) 发送到此 url,它就会为您提供 pdf。

    使用 HTTP POST: 将 html 参数中的 html 字符串发布到以下 url。 http://metrocdn.appspot.com/pdf/what_ever_filename_you_want.pdf

    或 使用 HTTP 获取: http://metrocdn.appspot.com/pdf/what_ever_filename_you_want.pdf?html=your-html-string

    问候

    萨法拉兹法鲁基

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-15
      • 2012-12-13
      • 2013-08-10
      • 2015-08-24
      • 2010-12-30
      • 2019-11-07
      • 2011-03-28
      • 2014-03-22
      相关资源
      最近更新 更多