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.
- Capture screenshots of web pages.
- Support for mobile and desktop viewports.
- Customizable viewport dimensions.
- Full-page screenshots.
- Image compression using
sharp
.
- Node.js (v16 or higher recommended)
- npm (v7 or higher recommended)
- Docker (optional, for containerized deployment)
-
Clone the repository:
git clone https://github.com/yourusername/puppeteer-screenshot-service.git cd puppeteer-screenshot-service
-
Install the dependencies:
npm install
-
Start the server:
node index.js
-
The service will be available at
http://localhost:3000
.
-
Build the Docker image:
docker build -t puppeteer-screenshot-service .
-
Run the Docker container:
docker run -d -p 3000:3000 puppeteer-screenshot-service
-
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
orfalse
).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
Method | Endpoint | Description |
---|---|---|
GET | /screenshot |
Capture a screenshot of a webpage. |
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). |
-
Default (1920x1080) Screenshot:
curl "http://localhost:3000/screenshot?url=https://example.com" --output screenshot_default.jpg
-
Mobile (iPhone 15) Screenshot:
curl "http://localhost:3000/screenshot?url=https://example.com&type=mobile" --output screenshot_mobile.jpg
-
Custom Dimensions (1024x768) Screenshot:
curl "http://localhost:3000/screenshot?url=https://example.com&width=1024&height=768" --output screenshot_custom.jpg
-
Full Page Screenshot:
curl "http://localhost:3000/screenshot?url=https://example.com&fullPage=true" --output screenshot_fullpage.jpg
-
Custom Quality (e.g., quality=50):
curl "http://localhost:3000/screenshot?url=https://example.com&quality=50" --output screenshot_compressed.jpg
index.js
: This is the main file that sets up the Express server, Puppeteer, andsharp
for image processing.package.json
: Contains project metadata and dependencies.
If a screenshot capture fails, the server will respond with a 500 status code and an error message.
Feel free to submit issues or pull requests if you find any bugs or have suggestions for improvements.
This project is licensed under the GNU GENERAL PUBLIC LICENSE. See the LICENSE
file for details.