Giter Club home page Giter Club logo

puppeteer-screenshot-service's Introduction

Puppeteer Screenshot Service

This project provides a web service to capture screenshots of web pages using Puppeteer. It supports various features such as custom viewport sizes, full-page screenshots, and image compression using the sharp library. Additionally, it can be run as a Docker container for easy deployment.

中文说明

中文说明

Features

  • Capture screenshots of web pages.
  • Support for mobile and desktop viewports.
  • Customizable viewport dimensions.
  • Full-page screenshots.
  • Image compression using sharp.

Prerequisites

  • Node.js (v16 or higher recommended)
  • npm (v7 or higher recommended)
  • Docker (optional, for containerized deployment)

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/puppeteer-screenshot-service.git
    cd puppeteer-screenshot-service
  2. Install the dependencies:

    npm install

Usage

Running as a Node.js Application

  1. Start the server:

    node index.js
  2. The service will be available at http://localhost:3000.

Running as a Docker Container

  1. Build the Docker image:

    docker build -t puppeteer-screenshot-service .
  2. Run the Docker container:

    docker run -d -p 3000:3000 puppeteer-screenshot-service

API Endpoints

Capture Screenshot

  • URL: /screenshot

  • Method: GET

  • Query Parameters:

    • url (required): The URL of the web page to capture.
    • type (optional): The type of device to emulate (mobile for iPhone 15).
    • width (optional): The viewport width in pixels.
    • height (optional): The viewport height in pixels.
    • fullPage (optional): Whether to capture a full-page screenshot (true or false).
    • quality (optional): The quality of the output image (1-100, default is 80).
  • Response:

    • Content-Disposition: attachment; filename=screenshot_<random_string>.jpg
    • Content-Type: image/jpeg

API Request Methods

Method Endpoint Description
GET /screenshot Capture a screenshot of a webpage.

Query Parameters

Parameter Type Required Description
url String Yes The URL of the webpage to capture.
type String No The type of device to emulate (mobile for iPhone 15).
width Integer No The viewport width in pixels.
height Integer No The viewport height in pixels.
fullPage Boolean No Whether to capture a full-page screenshot (true or false).
quality Integer No The quality of the output image (1-100, default is 80).

Examples

  1. Default (1920x1080) Screenshot:

    curl "http://localhost:3000/screenshot?url=https://example.com" --output screenshot_default.jpg
  2. Mobile (iPhone 15) Screenshot:

    curl "http://localhost:3000/screenshot?url=https://example.com&type=mobile" --output screenshot_mobile.jpg
  3. Custom Dimensions (1024x768) Screenshot:

    curl "http://localhost:3000/screenshot?url=https://example.com&width=1024&height=768" --output screenshot_custom.jpg
  4. Full Page Screenshot:

    curl "http://localhost:3000/screenshot?url=https://example.com&fullPage=true" --output screenshot_fullpage.jpg
  5. Custom Quality (e.g., quality=50):

    curl "http://localhost:3000/screenshot?url=https://example.com&quality=50" --output screenshot_compressed.jpg

Configuration

  • index.js: This is the main file that sets up the Express server, Puppeteer, and sharp for image processing.
  • package.json: Contains project metadata and dependencies.

Error Handling

If a screenshot capture fails, the server will respond with a 500 status code and an error message.

Contributing

Feel free to submit issues or pull requests if you find any bugs or have suggestions for improvements.

License

This project is licensed under the GNU GENERAL PUBLIC LICENSE. See the LICENSE file for details.

puppeteer-screenshot-service's People

Contributors

easychris avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.