Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pdsuwwz/puppeteer-server
🦩 Puppeteer + Node.js 后端服务, 基于 ESM + Koa + Rollup + TypeScript + ESLint (v9), 快速将任意网页转换为 PDF / 图像快照, 支持爬取合并多个网页自动生成为 PDF 文件,支持 Cookie 注入、PDF 水印和页眉页脚插入
https://github.com/pdsuwwz/puppeteer-server
babel combine es es6 eslint esm esmodule generator hmr injection merge module multiple node nodemon pdf pm2 rollup watermark
Last synced: 9 days ago
JSON representation
🦩 Puppeteer + Node.js 后端服务, 基于 ESM + Koa + Rollup + TypeScript + ESLint (v9), 快速将任意网页转换为 PDF / 图像快照, 支持爬取合并多个网页自动生成为 PDF 文件,支持 Cookie 注入、PDF 水印和页眉页脚插入
- Host: GitHub
- URL: https://github.com/pdsuwwz/puppeteer-server
- Owner: pdsuwwz
- License: mit
- Created: 2021-06-24T04:17:30.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-07-18T08:16:37.000Z (4 months ago)
- Last Synced: 2024-07-19T12:07:22.321Z (4 months ago)
- Topics: babel, combine, es, es6, eslint, esm, esmodule, generator, hmr, injection, merge, module, multiple, node, nodemon, pdf, pm2, rollup, watermark
- Language: TypeScript
- Homepage:
- Size: 935 KB
- Stars: 24
- Watchers: 3
- Forks: 11
- Open Issues: 7
-
Metadata Files:
- Readme: README-en.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Puppeteer Server
English | [中文](README.md)
## 📤 Migrate to Playwright
To experience enhanced features and broader browser support, the entire codebase of the latest version has seamlessly migrated to Playwright.
Playwright repo: [koa-playwright-server](https://github.com/pdsuwwz/koa-playwright-server)
## Introduction
🦩 Koa + ESM + TypeScript + Rollup + Nodemon + Puppeteer + ESLint (v9)
> * Fast Generate into PDF and images from any webpage.
>
> * Support merge multiple webpages into one PDF file, injection of Cookies, Watermark addition and Header and Footer insertion## ✨ Features
* ✅ Built-in ES Module + TypeScript environment
* 🌈 Separation business logic and Controllers.
* 🛡 Probably the best practice for Puppeteer project.
* 🧩 Configured routing.
* 🚧 Eslint (v9) configuration.
* ⚡ Fast build with Rollup.
* 🔌 Extensible PDF watermark, header and footer.
* 🧲 Supports merging of multiple PDF files.
* 🔥 Based on Nodemon HMR.
## Screenshot
* Merge Combine the two websites into a PDF file
> 📦 See [Merge Test 1](__test__/axios-browser.html), [Merge Test 2](__test__/axios-node.js)![image](https://user-images.githubusercontent.com/19891724/159743021-e1f9f528-d6d9-4d6b-b63f-4e71c6b72bdb.png)
## 🎯 Prerequisites
Please make sure that [Node.js](https://nodejs.org/) (>= 20.x) is installed on your operating system.
## Project structure
├── src
│ ├── controllers/ --- Server controllers
│ ├── services/ --- Server services
│ ├── config.ts --- About Environments variable
│ ├── main.ts --- Entry file
│ └── routes.ts --- Configs for routing controllers 👉 Routing## ⚡ Quick Start
### 1. Installation
```bash
pnpm install
```### 2. Running Development
```bash
pnpm dev
```### 3. Running Production
The project has built-in a `pm2`, running the `pnpm start` will automatically manage the process by `pm2`.
Run `pnpm build` to build, then run `pnpm start` to start the process managed by `pm2`:
* Build
```bash
pnpm build
```* Run
```bash
pnpm start # PORT is 8080
# or
node dist/bundle.esm.js # PORT is 5000
```## API
* `GET /image`
Generate screenshot.
```bash
curl --location --request GET \
'http://localhost:5000/image?url=https://www.baidu.com' \
--output test-image.png
```* `GET /simple-pdf`
Generate pdf.
```bash
curl --location --request GET \
'http://localhost:5000/simple-pdf?url=https://www.google.com/' \
--output test-simple-pdf.pdf
```* `POST /pdf`
Generate pdf with elements such as headers and footers.
```bash
curl --location --request POST 'http://localhost:5000/pdf' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'url=http://www.google.com' \
--data-urlencode 'cookies[0].name=token' \
--data-urlencode 'cookies[0].value=9s2d4c16-f072-16eg-b134-0642ap190006' \
--data-urlencode 'cookies[0].domain=www.google.com' --output test-complex-pdf.pdf
```👆 /pdf request parameters
| Field | Description | Type | Default Value |
| -------- | -------- | -------- | -------- |
| url | Target site url | string | — |
| cookies | Generally used as a website that requires login to access, you can add this field | Array<{ name, value, domain }> | [] |
| hasMargin | If this field is set to true, it means that the generated PDF will contain margins | boolean | true |
| isLandscape | Whether the generated PDF is horizontal | boolean | false |
| hiddenWatermark | Whether to hide watermark | boolean | false |
| attachment | Display the custom header and footer, provided that hasMargin is set to true | { header, footer } | — |* `POST /combine-pdf`
Merge multiple PDF files into one file.
See [Merge Test 1](__test__/axios-browser.html), [Merge Test 2](__test__/axios-node.js)
👆 /combine-pdf request parameters
| Field | Description | Type | Default Value |
| -------- | -------- | -------- | -------- |
| pdfList | A collection of target websites, the parameter type is an array, and each item in the array is a parameter required by `/pdf` | Array<{ pdfItem }> | [] |## Routing
In order to make the routing information more readable and transparent, the form of configuration is adopted here.
You can create an `array` and then write the routing meta information into the `array`, and reuse it in the [src/routes.ts](src/routes.ts)
```ts
const routes: Array = [
{
path: '/',
method: 'get',
action: homeController.hello
},
// here...
]
```# License
:v:
[MIT](./LICENSE)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fpdsuwwz%2Fpuppeteer-server.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fpdsuwwz%2Fpuppeteer-server?ref=badge_large)