将HTML渲染成PDF的HTTP服务器
项目描述
名称已经说明了一切。您请求一个包含HTML的POST,然后您将获得响应,其中包括渲染的PDF。
HTTP API
发送带有Content-Type: text/html的POST /请求,例如。
POST / HTTP/1.1
Content-Type: text/html; charset=utf-8
Accept: 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/pdf标题。)
然后您将通过其响应获得PDF文件,例如。
HTTP/1.1 200 OK
Content-Type: application/pdf
Server: html2pdf-server
(...omitted...)
可用的MIME类型
您可以将Accept标题设置为各种MIME类型。
- application/pdf
响应一个PDF文件。这是默认类型。
- image/png
响应一个PNG图像。
- image/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镜像公开8080端口用于HTTP服务器。
$ 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、Homebrew)安装。阅读文档。
运行服务器
使用 html2pdfd 命令
$ html2pdfd --port 8080
Serving 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 或更高。不再支持 Python 3.4 或更低版本。
将 waitress 替换为 aiohttp-wsgi。
Docker 镜像现在使用 Python 3.5 而不是 3.4。
版本 1.1.0
发布于 2017 年 1 月 26 日。
添加了一个选项来启用 pong 端点进行健康检查。
PONG_PATH 环境变量用于 Docker。
--pong-path 选项用于 CLI。
修复了一个在尝试渲染默认值时导致 --help 选项崩溃的 bug。
版本 1.0.0
初始发布。发布于 2017 年 1 月 25 日。
项目详情
下载文件
下载适用于您的平台文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。
源代码发行版
构建发行版
html2pdf-server-1.2.2.tar.gz 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | d72f2e42102b7603233bd5d2f8c48477915a0875ede257cd1bc187d421f5dc07 |
|
MD5 | e7f3f18131900bb132e1b4c5fb5ce167 |
|
BLAKE2b-256 | eb6753dcf60b7f1cabc4c6045df9fdac70fab7cabec12969ba61a6a72021c959 |
html2pdf_server-1.2.2-py3-none-any.whl 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 1b844692841f974ad6e3a51dff3455a97d4a82e62cd917eaf622d76f0dd019ad |
|
MD5 | 3310ab69efd1071fa2bd3098e279151c |
|
BLAKE2b-256 | a0026fe669a4a9f407515d99e443f9670578159ccff9d6d522d4809bd13d4423 |