用javascript或python将Html转换成图像

时间:2022-10-20 09:01:26

Just a dumb question... Is there any way you can create an image, say png, with the content of an html page, with a simple click of a button?

只是一个愚蠢的问题…有什么方法可以创建一个图像,比如png,和一个html页面的内容,只需点击一个按钮?

Thanks to anyone who cares to answers.

感谢所有关心答案的人。


Thanks for you answers. But what I wanted was some code in javascript or python that would allow me to create an image from a table or div.

谢谢你的答案。但我想要的是javascript或python中的一些代码,这些代码可以让我从表或div中创建图像。

1 个解决方案

#1


3  

You'll be much better off just subprocessing off to phantomjs. On Ubuntu you can install it via:

如果你只是对phantomjs进行子处理,那就更好了。在Ubuntu上,你可以通过以下方式安装:

sudo apt-get install phantomjs

Then do something like:

然后做一些类似:

generate_image.py:

generate_image.py:

import subprocess
subprocess.check_call(['phantomjs', 'generate_image.js'])

generate_image.js:

generate_image.js:

var page = require('webpage').create();
page.content = "<b>Hello World!</b>";
page.render('generated_image.png')
phantom.exit()

#1


3  

You'll be much better off just subprocessing off to phantomjs. On Ubuntu you can install it via:

如果你只是对phantomjs进行子处理,那就更好了。在Ubuntu上,你可以通过以下方式安装:

sudo apt-get install phantomjs

Then do something like:

然后做一些类似:

generate_image.py:

generate_image.py:

import subprocess
subprocess.check_call(['phantomjs', 'generate_image.js'])

generate_image.js:

generate_image.js:

var page = require('webpage').create();
page.content = "<b>Hello World!</b>";
page.render('generated_image.png')
phantom.exit()