-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (32 loc) · 1.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const { createCanvas, loadImage, registerFont } = require('canvas');
const path = require('path');
const express = require('express');
const app = express();
const port = 3000;
app.get('/', async (req, res) => {
if (!req.query.name || req.query.name.length < 3)
return res
.status(400)
.json({ error: 'No name or name less than 3 letters' });
let fileName = path.join(__dirname, 'image.png');
let fontName = path.join(__dirname, 'font.ttf');
let nameText = req.query.name.toUpperCase().split(' ').slice(0, 3).join(' ');
const image = await loadImage(fileName);
registerFont(fontName, { family: '29LT Kaff' });
const canvas = createCanvas(image.width, image.height, 'pdf');
const ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0);
ctx.font = '38px "29LT Kaff"';
ctx.fillStyle = '#11BAB1';
ctx.fillText(nameText, 165, 625);
res.writeHead(200, {
'Content-Type': 'application/pdf',
'Content-Disposition': `attachment; filename="${nameText}.pdf"`,
});
const stream = canvas.createPDFStream();
stream.pipe(res);
image.src = null;
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});