CentOS 5.4 上的无头 HTML 渲染到图像
我有一个项目要求渲染 HTML 并将渲染的图像捕获为无头 CentOS 5.4 服务器上的文件。我的具体要求是:
1) 输入将是要渲染的页面的 URL(或 file:// URL 到本地 HTML 文件),输出将是包含页面渲染图像的图像文件
2) 渲染器必须支持 CSS level-1
3) 页面中的静态图像必须在渲染图像中正确显示,包括 .GIF 或 .PNG 文件中的任何透明颜色
4) 必须能够在无头 CentOS 5.4 服务器上运行。
5)解决方案不得依赖于任何不可免费用于商业用途的产品或组件,
除了获取屏幕截图并将其保存到图像之外,我不需要对页面执行任何操作。困难的部分似乎是在无头环境中执行此操作。我见过的一些解决方案需要显示器才能工作。
有什么建议吗?
提前致谢, 吉姆
解决方案: 我最终使用了 Standard Widget Toolkit 库 (www.eclipse.org/swt) 来实现其嵌入式浏览器功能。这使我能够以编程方式打开一个带有浏览器控件的窗口,渲染页面,然后将窗口的内容捕获到图像文件(通常是 PNG)。这种方法唯一无法避免的缺点是,当窗口必须暂时可见才能进行屏幕捕获时,会出现“闪烁”。我可以忍受它。其余的只是用于初始化获取系统资源的 SWT 对象的代码、用于检查页面加载是否成功完成的侦听器以及一些用于清理挂起连接的代码(当页面加载由于某种原因从未完成时)。
我让一个队友在 CentOS Linux 上玩 xvfb(X 虚拟帧缓冲区)。初始测试似乎有效,因此看起来下面发帖人的建议(他说尝试 xvfb)可能是解决我的问题的无头服务器部分的可行解决方案。
I have a project requirement to render HTML and capture the rendered image as a file on a headless CentOS 5.4 server. My specific requirements are:
1) Input will be a URL to the page to render (or file:// URL to a local HTML file), output will be an image file containing the rendered image of the page
2) CSS must be supported by the renderer up to CSS level-1
3) Static images in the page must be displayed properly in the rendered image, including any transparent color in .GIF or .PNG files
4) Must be able to run on a headless CentOS 5.4 server.
5) Solution must not depend on any product or component that is not free for commercial use
I do not need to do anything with the page other than get the screen capture and save it to an image. The hard part seems to be doing this in a headless environment. Some of the solutions that I've seen discussed need a display in order to work.
Any suggestions?
Thanks in advance,
Jim
SOLUTION:
I ended up using the Standard Widget Toolkit library (www.eclipse.org/swt) for its embedded browser capabilities. This allowed me to programmatically open a window with a browser control in it, render the page, then capture the content of the window to an image file (usually a PNG). The only downside to this approach that cannot be avoided is a "flicker" when the window must be made visible for a moment in order to do the screen capture. I can live with it. The rest was just code to initialize the SWT objects that obtain system resources, a listener to check for a successful completion of the page load, and some code to clean up a hung connection (when the page load never completes for whatever reason).
I got a teammate to play around with xvfb (X virtual framebuffer) on CentOS Linux. The initial tests appear to work, so it looks like the advice from the poster below (who said to try xvfb) may be a viable solution for the headless server part of my issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 xvfb 启动虚拟 X-windows 环境。您现在可以启动常规浏览器来呈现页面并使用屏幕捕获实用程序来捕获窗口的内容。它并不漂亮,但做起来很简单。
WebKit 是开源且可嵌入的,也许您可以在小型本机应用程序中使用它在画布上渲染并将其保存到磁盘?
You can start a virtual X-windows environment using xvfb . You can now start a regular browser to render the page and use a screencapture utility to capture the content of the window. It is not pretty, but straghtforward to do.
WebKit is open-source and embeddable, maybe you can use this in a small native app to render on a canvas and save it to disk?