-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (114 loc) · 3.36 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import express from 'express';
import {
pdfCreateToBufferAsync,
pdfCreateToStreamAsync,
getTemplateData,
checkParams,
compileAndGenerateHTML,
encryptBase64PDF,
encryptPDFHandleStream
} from './src/index.js';
const app = express();
app.use(express.json());
const cache = new Map();
/**
* Generate a PDF from a static HTML template
*/
app.get('/static', async (req, res) => {
try {
const templatePath = './pdf-templates/static.html';
const data = await getTemplateData(cache, templatePath);
const dataBuffer = Buffer.from(data, 'base64');
const html = compileAndGenerateHTML(dataBuffer);
const options = {
type: 'pdf',
format: 'A4',
orientation: 'portrait'
};
res.setHeader('Content-Type', 'application/pdf');
const stream = await pdfCreateToStreamAsync(html, options);
stream.pipe(res);
} catch (error) {
console.error(error);
return res.status(500).json({ message: error.message });
}
});
/**
* Generate a PDF from a dynamic Handlebars template file and optionaly
* add a secure password requirement.
*/
app.post('/dynamic', async (req, res) => {
const { tool, location, name, password, secure } = req.body;
const params = { tool, location, name, secure };
try {
checkParams(params);
} catch (error) {
return res.status(400).json({ message: error.message });
}
try {
const templatePath = './pdf-templates/dynamic.hbs';
const data = await getTemplateData(cache, templatePath);
const dataBuffer = Buffer.from(data, 'base64');
const html = compileAndGenerateHTML(dataBuffer, params);
const options = {
type: 'pdf',
format: 'A4',
orientation: 'portrait'
};
const buffer = await pdfCreateToBufferAsync(html, options);
const secureOptions = secure
? {
userPassword: password,
ownerPassword: password,
userProtectionFlag: '4'
}
: {};
res.setHeader('Content-Type', 'application/pdf');
encryptPDFHandleStream(buffer, secureOptions, res);
} catch (error) {
console.error(error);
return res.status(500).json({ message: error.message });
}
});
app.get('/64', async (req, res) => {
const paramsExample = {
tool: 'Node.js',
location: 'here',
name: 'Iglan',
secure: true,
password: 'aaa'
};
const templatePath = './pdf-templates/dynamic.hbs';
try {
const data = await getTemplateData(cache, templatePath);
const dataBuffer = Buffer.from(data, 'base64');
const html = compileAndGenerateHTML(dataBuffer, paramsExample);
const options = {
type: 'pdf',
format: 'A4',
orientation: 'portrait'
};
const buffer = await pdfCreateToBufferAsync(html, options);
const secureOptions = {
userPassword: paramsExample.password,
ownerPassword: paramsExample.password,
userProtectionFlag: '4'
};
const base64string = encryptBase64PDF(buffer, secureOptions);
const fileName = Date.now();
res.status(200).send(`
<a
href="data:application/pdf;base64,${base64string}"
download="${fileName}.pdf"
>Download PDF</a>
`);
// Or a JSON Base64 response below
// res.status(200).json({
// pdf: base64string
// });
} catch (error) {
console.error(error);
return res.status(500).json({ message: error.message });
}
});
app.listen(3000, () => console.log('Server Up'));