Server-side HTML + Javascript rendering

I need to render the server page of an HTML page and "extract" the raw bytes of the canvas element so that I can save it in PNG. The problem is that the canvas element is created from javascript (I use jquery Flot to generate the chart mostly). Therefore, I assume that I need a way to "host" the DOM + Javascript functionality from a browser without actually using the browser. I settled in mshtml (but open to all and all suggestions), because it seems that he should be able to do it for sure. This is an ASP.NET MVC project.

I searched everywhere and did not see anything convincing.

So, I have this simple HTML example, as simple as possible, to demonstrate the problem -

<!DOCTYPE html> <html> <head> <title>Wow</title> <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js" type="text/javascript"></script> </head> <body> <div id="hello"> </div> <script type="text/javascript"> function simple() { $("#hello").append("<p>Hello</p>"); } </script> </body> </html> 

which gives the expected result when launched from the browser.

I want to be able to load the original HTML into memory, execute a javascript function, and then manipulate the final DOM tree. I cannot use any System.Windows.WebBrowser class, since my code must run in a service environment.

So here is my code:

 IHTMLDocument2 domRoot = (IHTMLDocument2)new HTMLDocument(); using (WebClient wc = new WebClient()) { using (var stream = new StreamReader(wc.OpenRead((string)url))) { string html = stream.ReadToEnd(); domRoot.write(html); domRoot.close(); } } while (domRoot.readyState != "complete") Thread.Sleep(SleepTime); string beforeScript = domRoot.body.outerHTML; IHTMLWindow2 parentWin = domRoot.parentWindow; parentWin.execScript("simple"); while (domRoot.readyState != "complete") Thread.Sleep(SleepTime); string afterScript = domRoot.body.outerHTML; System.Runtime.InteropServices.Marshal.FinalReleaseComObject(domRoot); domRoot = null; 

The problem is that "beforeScript" and "afterScript" are exactly the same. The IHTMLDocument2 instance goes through the usual "uninitialized", "loadable", "full" cycle, no errors occur, nothing.

Anyone have any ideas on what I'm doing wrong? Totally lost here.

+6
source share
4 answers

I found Awesomium exactly what I need! "Windowless web browser frame." Brilliant.

+1
source

Basically you are trying to do things that are not meant for this.

You create HTML + Javascript so that the browser can draw it. You write C # to activate any server-side actions.

Creating HTML + Javascript on the server to upload it to the browser on the server in order to be able to save PNG sounds.

Have you considered other approaches, such as creating an image using the server-side C # component? Basically, why do you really need to save it on the server? Maybe someone can suggest a better solution?

+1
source

You can use Watin. Create your page, then use the Watin api to capture the generated page.

http://fwdnug.com/blogs/ddodgen/archive/2008/06/19/watin-api-capturewebpagetofile.aspx

+1
source

See Creating image data on an HTML canvas on the server side for a PhantomJs solution (similar to Node.js, but another, one file, without installation)

0
source

Source: https://habr.com/ru/post/903139/


All Articles