将 HTML 转换为图像的简单 HTML 布局引擎

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/341831/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:50:00  来源:igfitidea点击:

Simple HTML layout engine to convert HTML to an image

htmlimagelayoutimage-processingrendering

提问by gerdemb

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?

我需要一个工具来自动将简单的 HTML 转换为图像。我将控制由简单的文本格式标记和可能的图像链接组成的 HTML 输入——我不需要能够呈现任意 HTML。有没有一种简单的方法可以做到这一点?

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?

我看过像 Gecko 和 Webkit 这样的 HTML 布局引擎,但坦率地说,我对它们拥有的选项数量感到不知所措——我不需要完整的 Web 浏览器!是否可以以这种方式使用这些引擎?有人可以引导我朝着正确的方向前进吗?

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. :)

编辑:抱歉忘记提及我的服务器正在运行 Linux,因此 Windows 解决方案无济于事。:)

采纳答案by gerdemb

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!

在回答我自己的问题时,我发现了这个有用的工具,它使用 WebKit 渲染页面,然后将输出捕获为图像甚至 PDF 格式!

http://cutycapt.sourceforge.net/

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.

这个想法类似于 Jay 提到的 khtml2png,但我更喜欢这个实现。此外,为了将来参考,通过 Xvfb 运行 X 虚拟帧缓冲区并不像我担心的那样占用大量内存。

回答by Joe Kuan

I have used CutyCapt before it's a bit clumsy and bloated - requiring Qt.

在它有点笨拙和臃肿之前我已经使用过 CutyCapt - 需要 Qt。

IMO, PhantomJs (a headless webkit) by far the best solution, fast, easy and clean. You can do a lot of things with it.

IMO,PhantomJs(无头 webkit)是迄今为止最好的解决方案,快速、简单和干净。你可以用它做很多事情。

See http://phantomjs.org

http://phantomjs.org

Or see this

或者看这个

回答by Jay

You may find this useful, if you are running on Linux and have the KDE libs available: khtml2png

如果您在 Linux 上运行并且有可用的 KDE 库,您可能会发现这很有用: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.

khtml2png 是一个命令行程序,用于创建网页截图。它使用 libkhtml(在 KDE 网络浏览器 Konqueror 中使用的库)。在 khtml2png 2.0.5 到 2.5.0 中,ImageMagick 图形转换工具包中的“转换”用于创建各种图像文件格式的输出文件。2.6.0 及以后的开发将使用 Qt 库的内置转换。

Also, to follow up on what Vilx suggested, you could use html2psto 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.htmlfor one approach.

此外,为了跟进 Vilx 的建议,您可以使用html2ps将 HTML 转换为 ps 文件,然后使用gs(Ghostscript) 将 ps 文件转换为 png 或 jpg。有关一种方法,请参阅http://www.karakas-online.de/myLinuxTips/ps2png.html

回答by Rob

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.

视窗?如果是,那么 HTMLayout 可能会有所帮助——它是一个免费的渲染引擎,它有一个简单的 API——从 C/C++ 使用它是轻而易举的——将 HTML 放入 BMP 并不难。

http://www.terrainformatica.com/htmlayout/

http://www.terrainformatica.com/htmlayout/

It's free too.

它也是免费的。

回答by Brandon Rhodes

I have always had very good luck with the wkhtmltoimagecommand (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.

我一直很幸运地使用wkhtmltoimage命令(并且,对于打印输出,wkhtmltopdf),它在 Ubuntu 中本地可用多年,并且,如果我记得,也总是正确且相当快地呈现。

http://wkhtmltopdf.org/

http://wkhtmltopdf.org/

There are several options available, but a basic invocation looks like:

有几个选项可用,但基本调用如下所示:

wkhtmltoimage tmp.html tmp.png

回答by Alex Shnayder

You cn use the PDFCreatorapplication. 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.

您可以使用PDFCreator应用程序。此应用程序允许打印多种格式,包括所有类型的图像。它包括一个 ActiveX / com 服务器,它允许您相当容易地自动化该过程。您几乎可以转换任何可以打印的东西。这种方法的一个缺点是,由于它使用打印框架进行转换,因此一次只能转换一个文档,所以我不知道它对网站是否足够好。

回答by Vilx-

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.

也许您可以将 HTML 转换为另一种更容易转换为图像的格式?在 Google 中,我发现了一种叫做 html2ps 和 html2pdf 的东西。从 PS 到 EPS 仅一步之遥,并且已经可以渲染为图像。或类似的东西。