Linux 将html转换为图像的程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10042388/
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
Program that convert html to image
提问by Pydev UA
Is there a ready-to-use program, that I can run from my web application, to convert an HTML file to an image? Preferably using Webkit, or another mature browser engine?
是否有现成的程序,我可以从我的 Web 应用程序运行,将 HTML 文件转换为图像?最好使用 Webkit,还是其他成熟的浏览器引擎?
I tried wkhtmltoimage, but it doesn't render backgrounds correctly (while wkhtmltopd works fine). Is there one that works?
我试过wkhtmltoimage,但它没有正确呈现背景(而 wkhtmltopd 工作正常)。有没有一个有效的?
回答by kev
You can try webkit2png
:
你可以试试webkit2png
:
Tested in Ubuntu
:
测试Ubuntu
:
$ sudo apt-get install python-qt4 libqt4-webkit python-pip xvfb
$ wget https://raw.github.com/millisami/python-webkit2png/master/webkit2png.py
$ chmod +x webkit2png.py
$ sudo xvfb-run --server-args="-screen 0, 1024x768x24" ./webkit2png.py -o google.png http://www.google.com
This will create an image named google.png
这将创建一个名为 google.png 的图像
UPDATE
更新
Another easy way is using phantomjs
:
另一种简单的方法是使用phantomjs
:
$ phantom rasterize.js http://www.google.com google.png
Download and unzip the binary archive. rasterize.js
is in the example
directory.
下载并解压缩二进制存档。rasterize.js
是在example
目录中。