将html呈现为pdf的http服务器
html2pdf-server的Python项目详细描述
名字说明一切你用HTML请求一个POST,然后 将使用呈现的pdf获取响应。
http api
使用Content-Type: text/html发送POST /请求,例如:
POST/HTTP/1.1Content-Type:text/html; charset=utf-8Accept:application/pdf<!DOCTYPE><html><body><h1>HTML to be rendered to PDF</h1><p>This document will be rendered to PDF.</p></body></html>
(注意,您必须设置Accept: application/pdfheader。)
然后您将通过它的响应获得一个PDF文档,例如:
HTTP/1.1200OKContent-Type:application/pdfServer:html2pdf-server (...omitted...)
可用的mime类型
可以将Accept头设置为各种mime类型。
- 应用程序/pdf
- 回复pdf文档。此类型是默认类型。
- 图像/png
- 响应PNG图像。
- 图像/jpeg
- 响应JPEG图像。
会话在curl
$ cat input.html <!DOCTYPE> <html> <body> <h1>HTML to be rendered to PDF</h1> <p>This document will be rendered to PDF.</p> </body> </html> $ curl --header 'Content-Type: text/html'\ --data "`cat input.html`"\ --output output.pdf \ http://localhost:8080/ $ open output.pdf # Use xdg-open on Linux
结果截图:
开始使用Docker
由于它的非Python依赖性,最简单的使用方法是使用Docker。 官方的Docker镜像为HTTP服务器公开了8080端口
$ docker run -p 8080:8080 spoqa/html2pdf-server
如果需要用于运行状况检查的pong端点,请指定PONG_PATH环境 变量:
$ docker run -e PONG_PATH=/ping/ -p 8080:8080 spoqa/html2pdf-server Serving on http://0.0.0.0:8080
无Docker入门
安装
您可以使用pip:
$ pip install --user git+git://github.com/spoqa/html2pdf-server.git
注意WeasyPrint有几个依赖项需要安装 使用系统包管理器,如apt、自制。Read the docs.
运行服务器
使用html2pdfd命令:
$ html2pdfd --port 8080Serving on http://0.0.0.0:8080
或者也可以使用首选的wsgi服务器(wsgi端点是 html2pdfd:app):
$ aiohttp-wsgi-serve html2pdfd:app Serving on http://:::8080 http://0.0.0.0:8080
许可证
分布在AGPLv3或更高版本下
变更日志
版本1.2.2
2017年1月31日发布。
- 修正了服务器打印第一个日志时的崩溃
版本1.2.1
2017年1月31日发布。
- Docker图片现在包含了中文/日文/韩文的内置字体。
版本1.2.0
2017年1月27日发布。
- 先决条件python版本变为3.5或更高版本。 不再支持Python3.4或更低版本。
- 将waitress替换为aiohttp-wsgi。
- Docker映像现在使用Python3.5而不是3.4。
版本1.1.0
2017年1月26日发布。
- 添加了一个选项以启用Pong Endpoint进行运行状况检查。
- PONG_PATHdocker的环境变量。
- --pong-path用于cli的选项。
- 修复了--help选项在尝试呈现时崩溃的错误 默认值。
版本1.0.0
初次发布。2017年1月25日发布。