Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use asp.net and phantomjs to take a screen shot of a page and return it to the client

I want to be able to read a screen shot of a web site, and am attempting to use phantomjs and ASP.NET.

I have tried using page.render which would save the screen shot to a file. It works as a console application, but not when I call it from an asp.net handler. It is probably due to file permissions, since simple applications (like hello.js) work fine.

That is okay, my preference would be not to write to a file, but to deal with the bytes and return an image directly from the handler.

I am a bit lost as to how to do that. I noticed a method called page.renderBase64, but do not know how to use it.

Currently I am using an IHttpHandler.

There is a similar question here, but that person eventualy dropped phantomjs. I like the look of it and want to continue using it if possible. Running Phantomjs using C# to grab snapshot of webpage

like image 565
zod Avatar asked Dec 17 '25 19:12

zod


1 Answers

According to your last comment you can do the following in phantom js file:

var base64image = page.renderBase64('PNG');
system.stdout.write(base64image);

in C#:

    var startInfo = new ProcessStartInfo {
    //some other parameters here
    ...
    FileName = pathToExe,
    Arguments = String.Format("{0}",someParameters),
    UseShellExecute = false,
    CreateNoWindow = true,
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    RedirectStandardInput = true,
    WorkingDirectory = pdfToolPath
    };
    var p = new Process();
    p.StartInfo = startInfo;
    p.Start();
    p.WaitForExit(timeToExit);
    //Read the Error:
    string error = p.StandardError.ReadToEnd();
    //Read the Output:
    string output = p.StandardOutput.ReadToEnd();

In your output variable you can read base64 returned from phantomJS and then do what you have planned with it.

like image 74
Andrey Borisko Avatar answered Dec 20 '25 09:12

Andrey Borisko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!