Windows 使用Phantom JS将文件夹中的所有HTML文件转换为PNG
我已经开始在Windows上使用Phantom JS,但在查找有关其功能的文档时遇到了一些困难(可能是我问题的根源) 使用Phantom JS,我希望执行以下操作:Windows 使用Phantom JS将文件夹中的所有HTML文件转换为PNG,windows,png,phantomjs,Windows,Png,Phantomjs,我已经开始在Windows上使用Phantom JS,但在查找有关其功能的文档时遇到了一些困难(可能是我问题的根源) 使用Phantom JS,我希望执行以下操作: 给它一个本地计算机文件夹位置 让它导航到该位置并识别HTML文件列表 一旦该列表被标识为HTML文件列表的循环,并将它们全部转换为PNG(类似于rasterize.js示例的工作方式),其中文件名gsubs“HTML”和“PNG” 我相信这是可能的,但我找不到Phantom JS函数调用: 获取文件夹中的文件列表并 Phantom
var page=require('webpage')。create(),loadInProgress=false,fs=require('fs');
var htmlFiles=新数组();
console.log(fs.workingDirectory);
var curdir=fs.list(fs.workingDirectory);
//循环浏览文件和文件夹
对于(变量i=0;i
希望如此。有关文件系统调用的更多信息,请查看以下页面:
此外,我还想补充一点,我相信文件系统功能只在PhantomJS 1.3或更高版本中可用。请确保运行该版本。我在Windows上使用了PyPhantomJS,但我确信它在其他系统上也能正常工作。不知怎的,我在脚本中只得到了空白的
PNG
s。添加console.log(page.content)
在页面中。onLoadFinished
返回
无论想要什么,真正的HTML内容都是…有什么建议吗?Phantomjs v1.9.2在尝试拍摄快照之前,您确定页面已完全加载吗?我还检查了状态。见我的问题:
var page = require('webpage').create(), loadInProgress = false, fs = require('fs');
var htmlFiles = new Array();
console.log(fs.workingDirectory);
var curdir = fs.list(fs.workingDirectory);
// loop through files and folders
for(var i = 0; i< curdir.length; i++)
{
var fullpath = fs.workingDirectory + fs.separator + curdir[i];
// check if item is a file
if(fs.isFile(fullpath))
{
// check that file is html
if(fullpath.indexOf('.html') != -1)
{
// show full path of file
console.log('File path: ' + fullpath);
htmlFiles.push(fullpath);
}
}
}
console.log('Number of Html Files: ' + htmlFiles.length);
// output pages as PNG
var pageindex = 0;
var interval = setInterval(function() {
if (!loadInProgress && pageindex < htmlFiles.length) {
console.log("image " + (pageindex + 1));
page.open(htmlFiles[pageindex]);
}
if (pageindex == htmlFiles.length) {
console.log("image render complete!");
phantom.exit();
}
}, 250);
page.onLoadStarted = function() {
loadInProgress = true;
console.log('page ' + (pageindex + 1) + ' load started');
};
page.onLoadFinished = function() {
loadInProgress = false;
page.render("images/output" + (pageindex + 1) + ".png");
console.log('page ' + (pageindex + 1) + ' load finished');
pageindex++;
}