nodejs 生成海报
时间: 2023-11-30 20:42:43 浏览: 192
以下是使用NodeJS生成海报的两种方案:
1. 使用Puppeteer库调用浏览器的page对象,基于page.screenshot截图并保存到磁盘。
```javascript
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch(); const page = await browser.newPage();
await page.goto('https://www.example.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})();
```
2. 将HTML作为SVG的外联元素,利用SVG的API导出为图片。
```javascript
const fs = require('fs');
const { createCanvas, loadImage } = require('canvas');
const canvas = createCanvas(200, 200);
const ctx = canvas.getContext('2d');
(async () => {
const image = await loadImage('path/to/image.jpg');
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
fs.writeFileSync('output.png', canvas.toBuffer());
})();
```
阅读全文