-
Notifications
You must be signed in to change notification settings - Fork 1
/
complex_view.js
67 lines (60 loc) · 1.53 KB
/
complex_view.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
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');
var pdfContent = require('./pdf_content')
// Create a document definition
const docDefinition = {
styles: {
header: {
padding: 15,
fontSize: 18,
bold: true,
margin: [10, 0, 0, 10]
},
padding: {
padding: 15
},
shipToAddress: {
fontSize: 10,
color: 'black',
},
},
pageSize: 'A4',
pageMargins: [20, 60, 20, 270],
header: pdfContent.getHeader,
footer: pdfContent.getFooter,
content: pdfContent.getComplexData()
}
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('Complex view generated successfully!')
})
stream.on('error', (error) => {
reject(error)
})
})
}
// Usage
generatePDF()
.then((message) => {
console.log(message)
})
.catch((error) => {
console.error('Error generating PDF:', error)
})