Hacker News 中文摘要

RSS订阅

Show HN: PlutoPrint – 使用Python从HTML生成PDF和PNG -- Show HN: PlutoPrint – Generate PDFs and PNGs from HTML with Python

文章摘要

Plutoprint 是一个基于 PlutoBook 的 Python 库,用于从 HTML 生成 PDF 和图像。该项目在 GitHub 上开源,提供了详细的文档和持续集成的构建状态,支持通过 PyPI 安装,并遵循特定的开源许可证。

文章总结

PlutoPrint:基于PlutoBook的HTML转PDF与图像生成库

PlutoPrint 是一个轻量级且易于使用的 Python 库,能够直接从 HTML 或 XML 内容生成高质量的 PDF 和图像文件。该库基于 PlutoBook 的强大渲染引擎,提供了简洁的 API,方便用户将 HTML 转换为清晰的 PDF 文档或生动的图像文件。PlutoPrint 非常适合用于生成报告、发票或视觉快照等场景。

安装

通过以下命令安装 PlutoPrint:

bash pip install plutoprint

PlutoPrint 依赖于 PlutoBook。为了加快安装速度,建议先手动安装 PlutoBook 及其依赖项。否则,Meson 会在安装过程中从源代码构建这些依赖项,耗时较长。

对于 Windows 和 Linux 64 位用户,PlutoPrint 提供了预编译的二进制文件,因此无需额外设置。

快速使用

从命令行生成 PDF

使用已安装的 plutoprint 脚本从命令行生成 PDF:

bash plutoprint input.html output.pdf --size=A4

使用 Python 生成 PDF

```python import plutoprint

book = plutoprint.Book(plutoprint.PAGESIZEA4) book.loadurl("input.html") book.writeto_pdf("output.pdf") ```

使用 Python 生成 PNG

```python import plutoprint import math

book = plutoprint.Book(media=plutoprint.MEDIATYPESCREEN) book.loadhtml("Hello World", userstyle="body { text-align: center }")

width = math.ceil(book.getdocumentwidth()) height = math.ceil(book.getdocumentheight())

with plutoprint.ImageCanvas(width, height) as canvas: canvas.clearsurface(1, 1, 1) book.renderdocument(canvas) canvas.writetopng("hello.png") ```

使用 Matplotlib 生成图表

```python import plutoprint import matplotlib.pyplot as plt import urllib.parse import io

class CustomResourceFetcher(plutoprint.ResourceFetcher): def fetchurl(self, url): if not url.startswith('chart:'): return super().fetchurl(url) values = [float(v) for v in urllib.parse.unquote(url[6:]).split(',')] labels = [chr(65 + i) for i in range(len(values))]

    plt.bar(labels, values)
    plt.title('Bar Chart')
    plt.xlabel('Labels')
    plt.ylabel('Values')

    buffer = io.BytesIO()
    plt.savefig(buffer, format='svg', transparent=True)

    return plutoprint.ResourceData(buffer.getvalue(), "image/svg+xml", "utf-8")

book = plutoprint.Book(plutoprint.PAGESIZEA4.landscape(), plutoprint.PAGEMARGINSNONE)

book.customresourcefetcher = CustomResourceFetcher()

HTML_CONTENT = """

"""

USER_STYLE = """ div { display: flex; flex-wrap: wrap; justify-content: center; height: 98vh } img { flex: 0 0 45%; height: 50%; background: #fff; border: 1px solid #ccc; } body { background: #f7f7f7 } """

book.loadhtml(HTMLCONTENT, USERSTYLE) book.writetopng("charts.png") book.writeto_pdf("charts.pdf") ```

示例与资源

许可证

PlutoPrint 采用 MIT 许可证,允许个人和商业用途。

评论总结

评论内容主要围绕以下几个方面:

  1. 与其他工具的比较

    • 评论1和评论10分别提到了与WeasyPrint和Typst的比较,询问PlutoPrint与这些工具的区别。
      • "How does it differ from https://weasyprint.org?" (评论1)
      • "Comparing it to typst?" (评论10)
    • 评论5则对PlutoPrint表示高度认可,认为其效率远超WeasyPrint。
      • "This is so efficient, i just tested it ,far better than weasyprint" (评论5)
  2. 功能与支持的询问

    • 评论9和评论11分别询问了PlutoPrint对Flexbox的支持和是否支持生成可链接的目录。
      • "Does this support full flexbox styling?" (评论9)
      • "Also, is there support for creating a linkable table of contents?" (评论11)
    • 评论4建议通过测试来评估PlutoPrint的复杂打印能力。
      • "It would be great if you could run it against the tests at https://www.print-css.rocks/" (评论4)
  3. 实际应用中的问题

    • 评论12详细列举了在实际生产环境中使用类似工具时遇到的多种问题,如页面分割、打印精度等,并建议使用绝对坐标系统来避免这些问题。
      • "In my 20 years in retail and logistics, I've seen these libraries repeatedly fail in production." (评论12)
      • "The only truly reliable solution is to bypass the HTML/CSS rendering model entirely and build the document on a canvas with an absolute coordinate system." (评论12)
  4. 替代方案与建议

    • 评论3提出了使用Puppeteer作为替代方案,认为其支持所有新特性,且代码量较少。
      • "it's a relatively few lines of code to use puppeteer to use an actual browser to render pages to PDFs/PNGs." (评论3)
    • 评论6建议PlutoPrint直接支持Markdown输入,而不需要先转换为HTML。
      • "I think that it would be great if this could take markdown as input, without having to convert to HTML first" (评论6)

总结:评论中对PlutoPrint的功能、与其他工具的比较、实际应用中的问题以及替代方案进行了广泛的讨论。部分评论对其效率表示认可,但也有评论指出了实际应用中可能遇到的问题,并提出了替代方案和改进建议。