html2canvas vs puppeteer vs screenshot-desktop
Web Page Screenshot Tools
html2canvaspuppeteerscreenshot-desktopSimilar Packages:
Web Page Screenshot Tools

Web Page Screenshot Tools are libraries or tools that allow developers to capture images of web pages or specific elements within them. These tools can be used for various purposes, including creating visual documentation, capturing content for testing, generating thumbnails, or allowing users to take screenshots of their screens. They can operate in different environments, such as the browser, server-side, or desktop applications, and may offer features like capturing full-page screenshots, selecting specific areas, adding annotations, and supporting various image formats.

Npm Package Weekly Downloads Trend
3 Years
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
html2canvas7,814,93831,7693.38 MB1,046-MIT
puppeteer7,263,77293,49063 kB2858 days agoApache-2.0
screenshot-desktop50,74848841.9 kB243 months agoMIT
Feature Comparison: html2canvas vs puppeteer vs screenshot-desktop

Capture Method

  • html2canvas:

    html2canvas captures screenshots by rendering the DOM elements directly in the browser. It creates a canvas element and draws the content of the selected elements onto it, simulating the appearance of the page. This method is client-side and does not require any external services.

  • puppeteer:

    puppeteer uses a headless Chrome browser to capture screenshots. It can render pages just like a regular browser, allowing for accurate capture of complex layouts, animations, and dynamic content. Puppeteer provides a programmatic interface to control the browser and take screenshots of any part of the page.

  • screenshot-desktop:

    screenshot-desktop captures screenshots of the user's desktop or specific application windows using native OS APIs. It is a desktop-focused solution that provides quick and simple screen capture functionality, but it does not interact with web pages or DOM elements.

Complexity

  • html2canvas:

    html2canvas is relatively easy to use and integrate into web applications. It requires minimal setup and can be used with just a few lines of JavaScript code. However, its limitations with complex layouts and cross-origin content may require additional handling.

  • puppeteer:

    puppeteer has a steeper learning curve due to its extensive API and headless browser setup. It is more complex to configure, especially for automated tasks, but it offers greater flexibility and control for advanced users and applications.

  • screenshot-desktop:

    screenshot-desktop is straightforward and simple to use, especially for capturing desktop screens. It requires minimal configuration and is easy to integrate into Node.js applications. However, it is limited to desktop environments and does not provide advanced features.

Cross-Origin Support

  • html2canvas:

    html2canvas has limitations with cross-origin content due to browser security restrictions. It cannot render images or elements from different domains unless they have the appropriate CORS headers set. This can result in incomplete or distorted screenshots when capturing content from multiple sources.

  • puppeteer:

    puppeteer handles cross-origin content more effectively since it operates in a headless browser environment. It can render pages as a regular browser would, including cross-origin images and elements. Puppeteer also allows for more control over network requests, making it easier to manage CORS issues if needed.

  • screenshot-desktop:

    screenshot-desktop does not deal with cross-origin content as it captures the screen directly from the user's desktop. It is not affected by web security policies since it operates outside the browser environment.

Use Case

  • html2canvas:

    Use html2canvas for client-side applications where you need to capture screenshots of specific web page elements or entire pages. It is suitable for creating visual content, generating images for social media sharing, or capturing data for reports directly from the browser.

  • puppeteer:

    Use puppeteer for automated screenshot generation, web scraping, testing, and capturing high-quality images of web pages. It is ideal for server-side applications, automated testing frameworks, and scenarios where you need precise control over the rendering process.

  • screenshot-desktop:

    Use screenshot-desktop for desktop applications or Node.js projects that require quick screen captures. It is useful for creating tools that allow users to capture their screens, generate thumbnails, or integrate screen capture functionality into desktop software.

Ease of Use: Code Examples

  • html2canvas:

    Capture a screenshot of a DOM element using html2canvas

    html2canvas(document.getElementById('elementId')).then(canvas => {
        document.body.appendChild(canvas);
        const img = canvas.toDataURL('image/png');
        console.log(img);
    });
    
  • puppeteer:

    Capture a screenshot of a web page using puppeteer

    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: 'screenshot.png' });
        await browser.close();
    })();
    
  • screenshot-desktop:

    Capture a screenshot of the desktop using screenshot-desktop

    const screenshot = require('screenshot-desktop');
    screenshot()
        .then((img) => {
            // img is a Buffer containing the screenshot
            // You can save it to a file or process it further
        })
        .catch((err) => {
            console.error(err);
        });
    
How to Choose: html2canvas vs puppeteer vs screenshot-desktop
  • html2canvas:

    Choose html2canvas if you need to capture screenshots directly from the browser without any server-side processing. It is ideal for capturing specific DOM elements or entire pages as images. However, it may struggle with complex layouts, cross-origin images, and does not support capturing content rendered by canvas elements or videos.

  • puppeteer:

    Select puppeteer if you require a headless browser solution for automated screenshot generation. It provides more control over the rendering process, supports capturing full-page screenshots, and can handle dynamic content, animations, and complex layouts. Puppeteer is suitable for server-side applications and automated testing environments.

  • screenshot-desktop:

    Use screenshot-desktop if you want to capture screenshots of the user's desktop or specific application windows. It is a good choice for desktop applications or Node.js projects where you need to capture the screen directly. This package is not suitable for capturing web pages or DOM elements.

README for html2canvas

html2canvas

Homepage | Downloads | Questions

Gitter CI NPM Downloads NPM Version

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.

How does it work?

The script renders the current page as a canvas image, by reading the DOM and the different styles applied to the elements.

It does not require any rendering from the server, as the whole image is created on the client's browser. However, as it is heavily dependent on the browser, this library is not suitable to be used in nodejs. It doesn't magically circumvent any browser content policy restrictions either, so rendering cross-origin content will require a proxy to get the content to the same origin.

The script is still in a very experimental state, so I don't recommend using it in a production environment nor start building applications with it yet, as there will be still major changes made.

Browser compatibility

The library should work fine on the following browsers (with Promise polyfill):

  • Firefox 3.5+
  • Google Chrome
  • Opera 12+
  • IE9+
  • Safari 6+

As each CSS property needs to be manually built to be supported, there are a number of properties that are not yet supported.

Usage

The html2canvas library utilizes Promises and expects them to be available in the global context. If you wish to support older browsers that do not natively support Promises, please include a polyfill such as es6-promise before including html2canvas.

To render an element with html2canvas, simply call: html2canvas(element[, options]);

The function returns a Promise containing the <canvas> element. Simply add a promise fulfillment handler to the promise using then:

html2canvas(document.body).then(function(canvas) {
    document.body.appendChild(canvas);
});

Building

You can download ready builds here.

Clone git repository:

$ git clone git://github.com/niklasvh/html2canvas.git

Install dependencies:

$ npm install

Build browser bundle

$ npm run build

Examples

For more information and examples, please visit the homepage or try the test console.

Contributing

If you wish to contribute to the project, please send the pull requests to the develop branch. Before submitting any changes, try and test that the changes work with all the support browsers. If some CSS property isn't supported or is incomplete, please create appropriate tests for it as well before submitting any code changes.