0

我需要一个代码片段来从可以包含图像和一些 CSS 的 HTML 页面生成 PDF 文件。

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

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

4

3 回答 3

2

GAE provides the conversion API, which has the capability to convert html to PDF (among some others). For an example on how to convert an html with images to PDF see the answer to the question Incomplete Google App Engine Documentation on Conversion API. If you need to include a CSS, then you need to include it in the html.

于 2012-07-09T16:11:48.233 回答
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)
于 2012-07-09T15:21:16.093 回答
1

我制作了 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 GET: http ://metrocdn.appspot.com/pdf/what_ever_filename_you_want.pdf?html=your-html-string

问候

萨法拉兹·法鲁基

于 2014-05-28T14:26:42.367 回答