有没有办法提供 Phantom.js HTML 代码而不是 URL 来渲染?
HTML URL 示例
var page = require('webpage').create();
page.open('http://github.com/', function() {
page.render('github.png');
phantom.exit();
});
所需的 HTML 标记示例
var page = require('webpage').create();
page.open('<html><head><style>SOME CSS</style></head><body><div>SOME TEXT AND IMAGES</div></body></html>',
function() {
page.render('github.png');
phantom.exit();
}
);
最佳答案
是的,有一种方法可以做到这一点:
var page = require('webpage').create();
page.viewportSize = { width: 800, height : 600 };
page.content = '<html><head><style>SOME CSS</style></head><body><div>SOME TEXT AND IMAGES</div></body></html>';
page.evaluate(function() {
// your logic here
});
page.render('github.png');
phantom.exit();
关于phantomjs - 使用 Phantom.js 将 HTML 代码转为 PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23795913/