将网页即时转换为图像?
我想知道是否有任何方法可以将整个 HTML 页面转换为 png(或其他类型的图像?) 我正在尝试即时创建 PDF,但它会将我的样式拉为文本,但我想要样式保持与页面相同(cufon 和所有)。任何帮助将不胜感激! :)
I was wondering if there was any way of turning an entire HTML page into a png (or other kind of image?) I'm trying to create PDFs on the fly, but it's pulling across my styles as text, but I want the styles to stay the same as the page (cufon and all). Any help would be appreciated! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这看起来并不简单。后端(PHP等)不进行渲染、布局。它只是生成内容。
网站的布局和视觉方面是由您的客户端(浏览器)完成的,后端无法访问它。
然而,对于给定的 HTML 文件,有一些库可以将其呈现为 PDF,例如 Prince XML似乎有能力做到这一点。
This doesn't look straightforward. The backend (PHP etc.) doesn't do rendering, layout. It merely generates content.
The layout and visual aspects of the website are done by your client (browser) and the backend has no way of accessing this.
However, given an HTML file, there are libraries that can render it into a PDF like Prince XML that seem to be capable of this.
生成与访问者在浏览您的网站时在浏览器中看到的图像相同甚至接近的图像的唯一方法是启动浏览器并截取屏幕截图。您需要浏览器的渲染引擎来渲染页面。您发现的所有无需浏览器即可完成此操作的库都会创建与访问者看到的完全不同的东西,并且根本不会渲染 cufon 或其他奇特的东西。
提供网页屏幕截图预览的公司现在运行许多服务器,每个服务器都运行许多虚拟 PC,每个服务器都运行完整的操作系统和真正的 Web 浏览器。他们拥有所有这些系统来提取作业,在真实浏览器中打开网页,截取屏幕截图并保存图像。您不会用一个小的 PHP 脚本来复制它。
http://ipinfo.info/html/rendering_services.php
The only way to generate an image identical, or even near, what a visitor sees in their browser when viewing your site is to launch a browser and take a screenshot. You need the browser's rendering engine to render the page. All the libraries you find to do it without a browser create something much different than what the visitor sees, and won't render cufon or other fancy things at all.
Companies that offer screenshot previews of a webpage now run many servers, each running many virtual PCs, each running a full operating system and real web browser. They have all those systems pulling jobs, opening the webpages in real browsers, taking screenshots and saving images. You won't replicate that with a little PHP script.
http://ipinfo.info/html/rendering_services.php
使用 PHP 将网页转换为图像和 PDF 是一件非常痛苦的事情。解决方案通常需要操作系统级脚本、伪造的打印机驱动程序或屏幕捕获,这可能会导致设置相当脆弱。几年前,我遇到了同样的问题,并开始研究本机 PHP 扩展,利用 Gecko 引擎将 HTML 渲染为 PDF,但从未完成。
我见过的最佳答案并没有完全将整个网页转换为 PDF,而是将 XML 转换为 PDF。 RenderX 的 XEP 是 Apple 用于生成多种格式(包括 HTML)的开发人员文档的商业工具以及来自 XML 源的精美渲染的 PDF。将 XEP 工具与 PHP 结合使用的好处是 PHP 可以很好地处理 XML,因此您可以将生成的 XML 传递给 XEP 二进制文件,让它转换为 PDF,然后在 PHP 中处理生成的 PDF 文件。
Turning web pages into images and PDFs is a royal pain using PHP. Solutions often require OS level scripting, fake printer drivers, or screen capturing, which can make for a rather fragile setup. I ran into the same issue a few years ago and started working on native PHP extension that leveraged the Gecko engine to render HTML to PDF, but never finished it.
The best answer I've seen doesn't quite turn a full web page into a PDF, but instead does XML to PDF. XEP by RenderX is the commercial tool Apple uses to produce developer documentation in many formats, including HTML and beautifully rendered PDFs, from an XML source. The great thing about using the XEP tool in conjunction with PHP is that PHP deals with XML very well, so you can pass generated XML to the XEP binary, let it do the conversion to PDF, then deal with the resulting PDF file in PHP.
考虑构建一个类似于您的网页的常规 PDF 文件:
PHP::PDF - 使用 php 构建。
PDF 参考 - 文件结构。
consider building a regular PDF file that resembles your web page:
PHP::PDF - constructing using php.
PDF Reference - file structure.