跳转到主要内容

将HTML渲染成PDF的HTTP服务器

项目描述

https://img.shields.io/pypi/v/html2pdf-server.svg https://img.shields.io/docker/automated/spoqa/html2pdf-server.svg

名称已经说明了一切。您请求一个包含HTML的POST,然后您将获得响应,其中包括渲染的PDF。

HTTP API

发送带有Content-Type: text/htmlPOST /请求,例如。

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

结果截图

Result screenshot

使用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 (5.1 kB 查看哈希)

上传时间 源代码

构建发行版

html2pdf_server-1.2.2-py3-none-any.whl (9.3 kB 查看哈希)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面