Skip to content

Latest commit

 

History

History
188 lines (116 loc) · 4.83 KB

README-en.md

File metadata and controls

188 lines (116 loc) · 4.83 KB

Puppeteer Server

English | 中文

📤 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

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, Merge Test 2

image

🎯 Prerequisites

Please make sure that Node.js (>= 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

pnpm install

2. Running Development

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
pnpm build
  • Run
pnpm start # PORT is 8080
# or
node dist/bundle.esm.js # PORT is 5000

API

  • GET /image

Generate screenshot.

curl --location --request GET \
'http://localhost:5000/image?url=https://www.baidu.com' \
--output test-image.png
  • GET /simple-pdf

Generate pdf.

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.

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, Merge Test 2

👆 /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

const routes: Array<RouteConfig> = [
  {
    path: '/',
    method: 'get',
    action: homeController.hello
  },
  // here...
]

License

✌️

MIT

FOSSA Status