开发者

turn web page into an image on the fly?

开发者 https://www.devze.com 2023-01-29 19:47 出处:网络
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开发者_如何学C it\'s pulling across my styles as text

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开发者_如何学C 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! :)


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.


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


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.


consider building a regular PDF file that resembles your web page:

PHP::PDF - constructing using php.
PDF Reference - file structure.

0

精彩评论

暂无评论...
验证码 换一张
取 消