php 使用php将网页打印为pdf文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5142080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print a webpage to pdf document using php
提问by aHunter
I can see there are a few options for doing this. My question is does any one know of a way that I can simply pass a php web page into a pdf creator and it outputs exactly the same as if it was viewed via a browser? Taking into consideration CSS, images and php functions etc.
我可以看到有几个选项可以做到这一点。我的问题是有人知道一种方法,我可以简单地将 php 网页传递给 pdf 创建者,它的输出与通过浏览器查看时完全相同吗?考虑到 CSS、图像和 php 函数等。
Thanks
谢谢
采纳答案by Treffynnon
Think wkhtmltopdfis the answer you are looking for as it uses WebKitto render the webpage just like it would look in a browser and then converts it to a PDF. This saves you writing a load of PHP code to organise the layout of your PDF.
认为wkhtmltopdf是您正在寻找的答案,因为它使用WebKit呈现网页,就像在浏览器中看起来一样,然后将其转换为 PDF。这可以节省您编写大量 PHP 代码来组织 PDF 的布局。
Details from their site:
来自他们网站的详细信息:
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)
使用 webkit 渲染引擎和 qt 将 html 转换为 pdf 的简单 shell 实用程序。
在网上搜索,我发现了几个命令行工具,可以让您将 HTML 文档转换为 PDF 文档,但是它们似乎都使用自己的且相当不完整的渲染引擎,导致质量很差。最近QT 4.4 发布了一个WebKit 小部件(WebKit 是Apples Safari 的引擎,它是KDE KHtml 的一个分支),制作一个好的工具变得非常容易。
- 使用 webkit 将网页转换为 PDF 文档
- 添加页眉和页脚(仅限静态版本)
- TOC 生成(仅限静态版本)
- 批处理模式转换
- (Linux) 不再需要运行 XServer(但必须安装 X11 客户端库)
For code and more information on wkhtmltopdfintegration with PHP see https://github.com/aur1mas/Wkhtmltopdf
有关wkhtmltopdf与 PHP 集成的代码和更多信息,请参阅https://github.com/aur1mas/Wkhtmltopdf
回答by Pekka
This is a two-part question really.
这真的是一个由两部分组成的问题。
Making a screen shot of a web site into an image has many good answers on SO
To generate a PDF, use e.g. the fpdflibrary.
将网站的屏幕截图制作成图像在 SO 上有很多很好的答案
要生成 PDF,请使用例如fpdf库。
回答by Tokk
You should take a look at thisclass, I really love it. It renders HTML to PDF (also includes CSS)
你应该看看这门课,我真的很喜欢它。它将 HTML 呈现为 PDF(还包括 CSS)
Here are some Examples
这里有一些例子
EDIT
编辑
Since the Links above are broken since 16/03/2016 here is a link to the libs main page: mPDF.
由于上面的链接自2016 年 3 月 16 日起失效,这里是 libs 主页的链接:mPDF。
回答by Borealid
The fact that it's a PHP web page makes no difference. In order to create a PDF that looks like the web page should look, you need to renderthe page - this is what a web browser does when you visit it.
它是一个 PHP 网页这一事实没有任何区别。为了创建看起来像网页应该看起来的 PDF,您需要渲染页面 - 这就是 Web 浏览器在您访问它时所做的。
You'll need to use a web browser engine such as Gecko to produce the page layout, then export that as an image and stick it in a PDF. This can be done programatically.
您需要使用 Gecko 等网络浏览器引擎来生成页面布局,然后将其导出为图像并将其粘贴到 PDF 中。这可以通过编程来完成。
回答by Adam Hopkinson
There are php modules/classes for PDF generation, and there are ways of using CSS to better prepare a page for printing (to pdf).
有用于生成 PDF 的 php 模块/类,并且有一些方法可以使用 CSS 更好地准备打印页面(到 pdf)。
PHP cannot 'see' the page as the browser sees it.
PHP 无法像浏览器看到的那样“看到”页面。