使用 php 将网页打印到 pdf 文档

Print a webpage to pdf document using php

我可以看到有几个选项可以做到这一点。我的问题是有人知道我可以简单地将php网页传递给pdf创建者的方法,它的输出与通过浏览器查看时完全相同吗?考虑到 CSS、图像和 php 函数等。

谢谢


认为 wkhtmltopdf 是您正在寻找的答案,因为它使用 WebKit 来呈现网页,就像在浏览器中一样,然后将其转换为 PDF。这样可以节省您编写大量 PHP 代码来组织 PDF 布局的时间。

来自他们网站的详细信息:

Simple shell utility to convert html
to pdf using the webkit rendering
engine, and qt.

Searching the web, I have found
several command line tools that allow
you to convert a HTML-document to a
PDF-document, however they all seem to
use their own, and rather incomplete
rendering engine, resulting in poor
quality. Recently QT 4.4 was released
with a WebKit widget (WebKit is the
engine of Apples Safari, which is a
fork of the KDE KHtml), and making a
good tool became very easy.

  • Convert web pages into PDF documents using webkit
  • Adding headers and footers (static version only)
  • TOC generation (static version only)
  • Batch mode conversions
  • (Linux) No longer requires an XServer to be running (however the X11
    client libs must be installed)

有关 wkhtmltopdf 与 PHP 集成的代码和更多信息,请参阅 https://github.com/aur1mas/Wkhtmltopdf


这真的是一个由两部分组成的问题。

  • 将网站的屏幕截图制作成图像在 SO

    上有很多很好的答案

  • 要生成 PDF,请使用例如fpdf 库。


它是一个 PHP 网页这一事实没有任何区别。为了创建一个看起来像网页应该看起来的 PDF,您需要渲染页面 - 这是您访问它时 Web 浏览器所做的事情。

您需要使用 Gecko 等网络浏览器引擎来生成页面布局,然后将其导出为图像并将其粘贴到 PDF 中。这可以通过编程方式完成。


有用于生成 PDF 的 php 模块/类,并且有一些方法可以使用 CSS 来更好地准备页面以进行打印(到 pdf)。

PHP 无法"看到"浏览器看到的页面。