Executing JavaScript from HTML input file through C# code


#1

Hi Team,
Our .Net Web API needs to read one input HTML file with some Javascript contents for Charts/SVG rendering on browser which needs to be converted to PDF further with the help of some third party conversion tool using their APIs.
But the conversion tool does not fully support JavaScript/Dynamic Contents rendering directly so We Are Trying to Use PhantomJS to make the output Supportable with that tool.
But facing issues in implementing PhantomJS for the same. Can anyone help in resolving the issue with Sample Code??

Scenario: We have one HTML file which contains some JavaScript code inside it/external source. We need to get the contents of the HTML file in form of string using C# code.

Note :We are open to buy the PhantomJS Licence if needed.

We are trying it in following way(using NReco.PhantomJS):

public string Conversion()
{

       string script ="var page = require('webpage').create();" +
        "var url = 'Local_File_Path/fileName.html';" +
        "page.open(url, function(status) {" +
         "if (status !== 'success'){" +
         "console.log('Unable to load the address!');" +
        "phantom.exit();" +
        "}else{"+
        "window.setTimeout(function() {"+
         "page.render(MyJavaScriptFuntion);"+
        "phantom.exit();"+
        "}, 1000);"+
        "console.log(status);" +
         "phantom.exit();"+
         ")};";

        string output = string.Empty;

        var phantomJS = new PhantomJS();
        phantomJS.OutputReceived += (sender, e) =>
          {
              output += e.Data;
          };
        phantomJS.RunScript(script, null);
            return output;

}

-> We Also tried : page.Evaluate() method and page.setContent() Methods but it didn’t work.

Thanks,
Ashutosh Shukla