English 中文(简体)
Using Phantom JS to convert all HTML files in a folder to PNG
原标题:

I ve started using Phantom JS on Windows, but I m having a bit of difficulty finding documentation on its capability (probably the root of my problem).

Using Phantom JS I would like to do the following:

  1. Give it a local machine folder location,
  2. Have it navigate to that location and identify the list of HTML files,
  3. Once that list is identified to loop of the the list of HTML files and convert them all to PNG (similar to the way the rasterize.js example works), where the filename gsubs "HTML" with "PNG".

I m sure this is probably possible, but I wasn t able to find the Phantom JS function call for:

  1. getting the list of files in a folder and
  2. the format for gsub and grep in Phantom JS.
最佳回答
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++;
}

Hope this helps. For more information about the FileSystem calls, check this page out: http://phantomjs.org/api/fs/

Also, I wanted to add, that I believe the FileSystem functions are only available in PhantomJS 1.3 or later. Please make sure to run the latest version. I used PyPhantomJS for Windows but I m sure this will work without a hitch on other systems as well.

问题回答

暂无回答




相关问题
Why running a service as Local System is bad on windows?

I am trying to find out the difference between difference service account types. I tumbled upon this question. The answer was because it has powerful access to local resources, and Network Service ...

Programmatically detect Windows cluster configuration?

Does anyone know how to programatically detect that a Windows server is part of a cluster? Further, is it possible to detect that the server is the active or passive node? [Edit] And detect it from ...

get file icon for Outlook appointment (.msg)

I ve read Get File Icon used by Shell and the other similar posts - and already use SHFileInfo to get the associated icon for any given extension, and that works great. However, Outlook uses ".msg" ...

Identifying idle state on a windows machine

I know about the GetLastInputInfo method but that would only give me the duration since last user input - keyboard or mouse. If a user input was last received 10 minutes ago, that wouldn t mean the ...

Terminating a thread gracefully not using TerminateThread()

My application creates a thread and that runs in the background all the time. I can only terminate the thread manually, not from within the thread callback function. At the moment I am using ...

热门标签