-
Notifications
You must be signed in to change notification settings - Fork 1
/
basic_async.js
53 lines (45 loc) · 1.12 KB
/
basic_async.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
var fonts = {
Roboto: {
normal: 'fonts/Roboto-Regular.ttf',
bold: 'fonts/Roboto-Medium.ttf',
italics: 'fonts/Roboto-Italic.ttf',
bolditalics: 'fonts/Roboto-MediumItalic.ttf'
}
};
var PdfPrinter = require('pdfmake');
var printer = new PdfPrinter(fonts);
var fs = require('fs');
// Create a document definition
const docDefinition = {
content: [
{ text: 'My First PDF with PDFMake', fontSize: 16, bold: true },
{
text: 'This is a simple example of PDF generation with Node.js and PDFMake.',
},
],
}
var options = {
// ...
}
// Create a Promise to generate the PDF
const generatePDF = () => {
return new Promise((resolve, reject) => {
var pdfDoc = printer.createPdfKitDocument(docDefinition, options);
const stream = pdfDoc.pipe(fs.createWriteStream('myFirstPDF.pdf'));
pdfDoc.end();
stream.on('finish', () => {
resolve('Async PDF generated successfully!')
})
stream.on('error', (error) => {
reject(error)
})
})
}
// Usage
generatePDF()
.then((message) => {
console.log(message)
})
.catch((error) => {
console.error('Error generating PDF:', error)
})