Take a screenshot of a webpage with JavaScript?
Asked Answered
P

15

153

Is it possible to to take a screenshot of a webpage with JavaScript and then submit that back to the server?

I'm not so concerned with browser security issues. etc. as the implementation would be for HTA. But is it possible?

Paedo answered 13/9, 2008 at 10:21 Comment(4)
Can you clarify why you want to do this? Perhaps there are alternative solutions to taking screenshots.Accroach
I'm looking at having the user roughly design what they want, a bit of a sketch and a bit of a drag 'n drop of objects. I then want this "design" to be used as some part of the instructions in a production process. It is definitely a user-involved step, nothing clandestine here :)Paedo
Possible duplicate of How to take page screen shot of a visitor?Hypoblast
Does this answer your question? Using HTML5/Canvas/JavaScript to take in-browser screenshotsSoftcover
T
44

I have done this for an HTA by using an ActiveX control. It was pretty easy to build the control in VB6 to take the screenshot. I had to use the keybd_event API call because SendKeys can't do PrintScreen. Here's the code for that:

Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Public Const CaptWindow = 2

Public Sub ScreenGrab()
   keybd_event &H12, 0, 0, 0
   keybd_event &H2C, CaptWindow, 0, 0
   keybd_event &H2C, CaptWindow, &H2, 0
   keybd_event &H12, 0, &H2, 0
End Sub

That only gets you as far as getting the window to the clipboard.

Another option, if the window you want a screenshot of is an HTA would be to just use an XMLHTTPRequest to send the DOM nodes to the server, then create the screenshots server-side.

Trumpeter answered 15/9, 2008 at 14:40 Comment(3)
I agree with Allen, that's an ingenious way to potentially screenshot a page!Wiretap
What an "HTA" please?Goodfellowship
@PeteAlvin an HTA is a Hypertext application, which was a way to run privileged JS applications in Internet Explorer. Not super relevant today.Trumpeter
L
144

Google is doing this in Google+ and a talented developer reverse engineered it and produced http://html2canvas.hertzen.com/ . To work in IE you'll need a canvas support library such as http://excanvas.sourceforge.net/

Lucienlucienne answered 2/8, 2011 at 22:53 Comment(1)
Thanks, the links in your post explains everyting wellLussier
T
44

I have done this for an HTA by using an ActiveX control. It was pretty easy to build the control in VB6 to take the screenshot. I had to use the keybd_event API call because SendKeys can't do PrintScreen. Here's the code for that:

Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Public Const CaptWindow = 2

Public Sub ScreenGrab()
   keybd_event &H12, 0, 0, 0
   keybd_event &H2C, CaptWindow, 0, 0
   keybd_event &H2C, CaptWindow, &H2, 0
   keybd_event &H12, 0, &H2, 0
End Sub

That only gets you as far as getting the window to the clipboard.

Another option, if the window you want a screenshot of is an HTA would be to just use an XMLHTTPRequest to send the DOM nodes to the server, then create the screenshots server-side.

Trumpeter answered 15/9, 2008 at 14:40 Comment(3)
I agree with Allen, that's an ingenious way to potentially screenshot a page!Wiretap
What an "HTA" please?Goodfellowship
@PeteAlvin an HTA is a Hypertext application, which was a way to run privileged JS applications in Internet Explorer. Not super relevant today.Trumpeter
P
15

Another possible solution that I've discovered is http://www.phantomjs.org/ which allows one to very easily take screenshots of pages and a whole lot more. Whilst my original requirements for this question aren't valid any more (different job), I will likely integrate PhantomJS into future projects.

Paedo answered 13/2, 2012 at 23:25 Comment(2)
Do you know if phantomjs can generate an image of an element on a page (not the whole page)?Gearbox
Yes you can. Either use this snippet with PhantomJS or use CasperJS.Nab
R
13

Pounder's if this is possible to do by setting the whole body elements into a canvase then using canvas2image ?

http://www.nihilogic.dk/labs/canvas2image/

Ramekin answered 25/10, 2010 at 8:29 Comment(2)
wonder if SVG would help, might have to look at Google's sourceVander
You can use Niklas's html2canvas code for rendering html in canvas, then use this to save an image.Gearbox
R
10

A possible way to do this, if running on windows and have .NET installed you can do:

public Bitmap GenerateScreenshot(string url)
{
    // This method gets a screenshot of the webpage
    // rendered at its full size (height and width)
    return GenerateScreenshot(url, -1, -1);
}

public Bitmap GenerateScreenshot(string url, int width, int height)
{
    // Load the webpage into a WebBrowser control
    WebBrowser wb = new WebBrowser();
    wb.ScrollBarsEnabled = false;
    wb.ScriptErrorsSuppressed = true;
    wb.Navigate(url);
    while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }


    // Set the size of the WebBrowser control
    wb.Width = width;
    wb.Height = height;

    if (width == -1)
    {
        // Take Screenshot of the web pages full width
        wb.Width = wb.Document.Body.ScrollRectangle.Width;
    }

    if (height == -1)
    {
        // Take Screenshot of the web pages full height
        wb.Height = wb.Document.Body.ScrollRectangle.Height;
    }

    // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
    Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
    wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
    wb.Dispose();

    return bitmap;
}

And then via PHP you can do:

exec("CreateScreenShot.exe -url http://.... -save C:/shots domain_page.png");

Then you have the screenshot in the server side.

Ramekin answered 2/12, 2010 at 21:47 Comment(2)
Although slightly unrelated to the questions, this is an awesome suggestion ! Thanks !Playmate
why it doesn't load some styles that even i can see them loaded in IE9?!Gluck
H
8

This might not be the ideal solution for you, but it might still be worth mentioning.

Snapsie is an open source, ActiveX object that enables Internet Explorer screenshots to be captured and saved. Once the DLL file is registered on the client, you should be able to capture the screenshot and upload the file to the server withing JavaScript. Drawbacks: it needs to register the DLL file at the client and works only with Internet Explorer.

Horrorstruck answered 15/9, 2008 at 14:58 Comment(0)
S
7

We had a similar requirement for reporting bugs. Since it was for an intranet scenario, we were able to use browser addons (like Fireshot for Firefox and IE Screenshot for Internet Explorer).

Stylite answered 13/9, 2008 at 15:28 Comment(0)
K
5

This question is old but maybe there's still someone interested in a state-of-the-art answer:

You can use getDisplayMedia:

https://github.com/ondras/browsershot

Kelantan answered 26/11, 2019 at 18:6 Comment(1)
I really like getDisplayMedia but the default usage of browsershot is somewhat over the top as it takes a picture of the entire window which may not be desirable in most cases. The developer can control this (setting displaySurface to application, browser, monitor, or window), and the user has control over whether to allow it, but I think that most developers just want the contents of the browser tab and not the actual browser window.Ofris
W
4

The SnapEngage uses a Java applet (1.5+) to make a browser screenshot. AFAIK, java.awt.Robot should do the job - the user has just to permit the applet to do it (once).

And I have just found a post about it:

Wort answered 14/10, 2010 at 19:1 Comment(0)
M
3

I found that dom-to-image did a good job (much better than html2canvas). See the following question & answer: https://mcmap.net/q/98890/-render-html-to-an-image

This question asks about submitting this back to the server, which should be possible, but if you're looking to download the image(s) you'll want to combine it with FileSaver.js, and if you want to download a zip with multiple image files all generated client-side take a look at jszip.

Maricruzmaridel answered 26/3, 2018 at 15:38 Comment(0)
M
2

You can achieve that using HTA and VBScript. Just call an external tool to do the screenshotting. I forgot what the name is, but on Windows Vista there is a tool to do screenshots. You don't even need an extra install for it.

As for as automatic - it totally depends on the tool you use. If it has an API, I am sure you can trigger the screenshot and saving process through a couple of Visual Basic calls without the user knowing that you did what you did.

Since you mentioned HTA, I am assuming you are on Windows and (probably) know your environment (e.g. OS and version) very well.

Muticous answered 13/9, 2008 at 10:52 Comment(1)
He's trying to do it as a part of a web app across clients' computes not for personal useHeaume
P
2

If you are willing to do it on the server side, there are options like PhantomJS, which is now deprecated. The best way to go would be Headless Chrome with something like Puppeteer on Node.JS. Capturing a web page using Puppeteer would be as simple as follows:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.screenshot({path: 'example.png'});

  await browser.close();
})();

However it requires headless chrome to be able to run on your servers, which has some dependencies and might not be suitable on restricted environments. (Also, if you are not using Node.JS, you might need to handle installation / launching of browsers yourself.)

If you are willing to use a SaaS service, there are many options such as

Phyletic answered 16/11, 2018 at 10:38 Comment(0)
A
1

A great solution for screenshot taking in Javascript is the one by https://grabz.it.

They have a flexible and simple-to-use screenshot API which can be used by any type of JS application.

If you want to try it, at first you should get the authorization app key + secret and the free SDK

Then, in your app, the implementation steps would be:

// include the grabzit.min.js library in the web page you want the capture to appear
<script src="grabzit.min.js"></script>

//use the key and the secret to login, capture the url
<script>
GrabzIt("KEY", "SECRET").ConvertURL("http://www.google.com").Create();
</script>

Screenshot could be customized with different parameters. For example:

GrabzIt("KEY", "SECRET").ConvertURL("http://www.google.com", 
{"width": 400, "height": 400, "format": "png", "delay", 10000}).Create();
</script>

That's all. Then simply wait a short while and the image will automatically appear at the bottom of the page, without you needing to reload the page.

There are other functionalities to the screenshot mechanism which you can explore here.

It's also possible to save the screenshot locally. For that you will need to utilize GrabzIt server side API. For more info check the detailed guide here.

Anemo answered 1/7, 2017 at 10:30 Comment(0)
L
0

As of today Apr 2020 GitHub library html2Canvas https://github.com/niklasvh/html2canvas

GitHub 20K stars | Azure pipeles : Succeeded | Downloads 1.3M/mo |

quote : " JavaScript HTML renderer The script allows you to take "screenshots" of webpages or parts of it, directly on the users browser. The screenshot is based on the DOM and as such may not be 100% accurate to the real representation as it does not make an actual screenshot, but builds the screenshot based on the information available on the page.

Lelandleler answered 16/4, 2020 at 16:59 Comment(0)
M
0

I made a simple function that uses rasterizeHTML to build a svg and/or an image with page contents.

Check it out :

https://github.com/orisha/tdg-screen-shooter-pure-js

Manicurist answered 17/6, 2020 at 16:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.