简单的 HTML 布局引擎,可将 HTML 转换为图像
我需要一个工具来自动将简单的 HTML 转换为图像。 我将控制 HTML 输入,其中包含简单的文本格式标记和可能的图像链接——我不需要能够呈现任意 HTML。 有没有一种简单的方法可以做到这一点?
我研究过 Gecko 和 Webkit 等 HTML 布局引擎,但坦率地说,我对它们拥有的选项数量感到不知所措——我不需要一个完整的 Web 浏览器! 是否可以以这种方式使用这些引擎? 有人可以引导我走向正确的方向吗?
其他可能性(例如浏览器截图)依赖于真实浏览器的屏幕截图,但我将在可能有许多用户的 Web 服务器上运行此应用程序,因此性能很重要,而且我担心这种解决方案无法扩展。
有想法吗?
编辑:抱歉忘记提及我的服务器正在运行 Linux,因此 Windows 解决方案无济于事。 :)
I need a tool to automatically convert simple HTML into an image. I will be controlling the HTML input which will consist of simple text formatting tags and possibly image links--I don't need to be able to render arbitrary HTML. Is there a simple way to do this?
I've looked at the HTML layout engines like Gecko and Webkit, but frankly I'm overwhelmed by the number of options they have--I don't need a complete web browser! Is it possible to use these engines in this way? Can someone steer me in the right direction?
Other possibilities like browsershots, rely on screenshots of real browsers, but I'm going to be running this application on a web server with potentially many users so performance is important and I'm afraid this kind of solution won't scale.
Ideas?
EDIT: Sorry forget to mention that my server is running Linux, so Windows solutions won't help. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
回答我自己的问题,我发现这个有用的工具,它使用 WebKit 渲染页面,然后将输出捕获为图像甚至 PDF 格式!
http://cutycapt.sourceforge.net/
这个想法类似于 Jay 提到的 khtml2png,但我喜欢这个实施效果更好。 另外,为了将来的参考,通过 Xvfb 运行 X 虚拟帧缓冲区并不像我担心的那样占用大量内存。
Answering, my own question I found this useful tool which uses WebKit to render a page and then captures the output as an image or even in PDF format!
http://cutycapt.sourceforge.net/
The idea is similar to khtml2png mentioned by Jay, but I liked this implementation better. Also, for future reference, running an X virtual frame-buffer through Xvfb is not nearly as memory intensive as I had feared.
我之前用过CutyCapt,它有点笨拙和臃肿——需要Qt。
IMO,PhantomJs(无头 Webkit)是迄今为止最好的解决方案,快速、简单且干净。 你可以用它做很多事情。
请参阅 http://phantomjs.org
或参阅 这个
I have used CutyCapt before it's a bit clumsy and bloated - requiring Qt.
IMO, PhantomJs (a headless webkit) by far the best solution, fast, easy and clean. You can do a lot of things with it.
See http://phantomjs.org
Or see this
如果您在 Linux 上运行并且有可用的 KDE 库,您可能会发现这很有用:khtml2png< /a>
khtml2png 是一个用于创建网页屏幕截图的命令行程序。 它使用 libkhtml(KDE Web 浏览器 Konqueror 中使用的库)。 在 khtml2png 2.0.5 至 2.5.0 中,ImageMagick 图形转换工具包中的“convert”用于创建各种图像文件格式的输出文件。 2.6.0和未来的开发将使用Qt库的内置转换。
此外,为了跟进Vilx的建议,您可以使用html2ps将HTML转换为ps文件,然后 gs (Ghostscript) 将 ps 文件转换为 png 或 jpg。 请参阅http://www.karakas-online.de/myLinuxTips/ps2png.html 一种方法。
You may find this useful, if you are running on Linux and have the KDE libs available: khtml2png
khtml2png is a command line program to create screenshots of webpages. It uses libkhtml (the library that is used in the KDE webbrowser Konqueror). In khtml2png 2.0.5 to 2.5.0 "convert" from the ImageMagick graphic conversion toolkit is used to create the output files in various image file formats. 2.6.0 and future development will use the built-in conversion of the Qt library.
Also, to follow up on what Vilx suggested, you could use html2ps to convert HTML to a ps file, then gs (Ghostscript) to turn the ps file into a png or jpg. See http://www.karakas-online.de/myLinuxTips/ps2png.html for one approach.
视窗? 如果是,那么 HTMLayout 可能会有所帮助 - 它是一个免费的渲染引擎,并且有一个简单的 API - 从 C/C++ 使用它是轻而易举的 - 将 HTML 转换为 BMP 并不难。
http://www.terrainformatica.com/htmlayout/
它也是免费的。
Windows? If yes, then HTMLayout may be able to help - it's a free rendering engine and it has a simple API - using it from C/C++ is a breeze - getting HTML into a BMP wouldn't be hard.
http://www.terrainformatica.com/htmlayout/
It's free too.
我一直很幸运地使用了
wkhtmltoimage
命令(对于打印输出,wkhtmltopdf
),该命令多年来一直在 Ubuntu 中本地使用,并且,如果我记得的话,总是正确且相当快地渲染。http://wkhtmltopdf.org/
有多个可用选项,但基本调用如下所示:
I have always had very good luck with the
wkhtmltoimage
command (and, for printed output,wkhtmltopdf
) which have for years been available natively in Ubuntu and have, if I recall, always rendered correctly and fairly quickly as well.http://wkhtmltopdf.org/
There are several options available, but a basic invocation looks like:
也许您可以将 HTML 转换为另一种更容易转换为图像的格式? 在谷歌中我发现了一些叫做 html2ps 和 html2pdf 的东西。 从 PS 到 EPS 仅一步之遥,并且已经可以渲染为图像。 或类似的东西。
Perhaps you can convert the HTML to another format which is more readily convertable to an image? In Google I found something called html2ps and html2pdf. From PS it's just one step away to EPS, and that can be rendered as an image already. Or something like that.
您可以使用 PDFCreator 应用程序。 该应用程序允许打印多种格式,包括所有类型的图像。 它包括一个 ActiveX / com 服务器,使您可以相当轻松地自动化该过程。 您几乎可以转换任何可以打印的内容。
这种方法的一个缺点是,由于它使用打印框架进行转换,因此一次只能转换一个文档,所以我不知道它对于网站来说是否足够好。
You cn use the PDFCreator application. This application allows printing to many formats including images of all kind. It includes an ActiveX / com server which allows you to automate the process fairly easily. You can convert pretty much any thing you can print.
One draw back of this method is that since it uses the printing frame work for conversion you can convert only one document at a time, so I don't know if it will good enough for a website.