-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.js
44 lines (36 loc) · 896 Bytes
/
handler.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
42
43
44
'use strict';
var pdf = require('pdf-lib');
module.exports.watermark = async event => {
let docFont;
var data = JSON.parse(event.body);
var buf = Buffer.from(data.pdf, 'base64');
const existingPdfBytes = buf;
const pdfDoc = await pdf.PDFDocument.load(existingPdfBytes)
if (data.font){
docFont = await pdfDoc.embedFont(pdf.StandardFonts[data.font]);
}else{
docFont = await pdfDoc.embedFont(pdf.StandardFonts.Helvetica);
}
const pages = pdfDoc.getPages()
pages.forEach(function (page) {
page.drawText(data.watermark, {
x: data.x,
y: data.y,
size: 12,
font: docFont,
color: pdf.rgb(0, 0, 0),
})
})
const pdfBytes = await pdfDoc.save()
return {
statusCode: 200,
body: JSON.stringify(
{
status: true,
pdf: Buffer.from(pdfBytes).toString('base64')
},
null,
2
),
};
};