-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-pdf.js
More file actions
86 lines (65 loc) · 2.69 KB
/
generate-pdf.js
File metadata and controls
86 lines (65 loc) · 2.69 KB
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
const puppeteer = require('puppeteer');
const path = require('path');
const Handlebars = require('handlebars');
const fs = require('fs-extra');
const helper = require('./handlebar-helpers.js');
const utilF = require('./util-file.js');
const billHtmlTemplatePath = path.join(__dirname, 'public', 'puppeteer', 'bill-template.html');
const billHtmlPath = path.join(__dirname, 'public', 'puppeteer', 'bill.html');
Handlebars.registerHelper('formatCurrency', helper.formatCurrency);
Handlebars.registerHelper('formatCurrencyWithSymbol', helper.formatCurrencyWithSymbol);
Handlebars.registerHelper('omitTitleIfNil', helper.omitTitleIfNil);
Handlebars.registerHelper('omitValueIfNil', helper.omitValueIfNil);
Handlebars.registerHelper('addSpaceIfNotNil', helper.addSpaceIfNotNil);
Handlebars.registerHelper('dateFormatter', helper.dateFormatter);
Handlebars.registerHelper('subsidioPercentageFormatter', helper.subsidioPercentageFormatter);
Handlebars.registerHelper('subsidioM3Formatter', helper.subsidioM3Formatter);
Handlebars.registerHelper('repactacionCuotaFormatter', helper.repactacionCuotaFormatter);
Handlebars.registerHelper('repactacionDeudaFormatter', helper.repactacionDeudaFormatter);
let folio;
(async function () {
folio = await utilF.getPrimerFolioDisponible();
})();
async function generatePDF(data) {
console.log(data);
try {
const formattedData = {};
Object.keys(data).map(originalKey => {
const newKey = originalKey
.toLowerCase()
.split(' ')
.map((word, index) => {
if (index > 0 && word) {
return word[0].toUpperCase() + word.slice(1);
}
return word;
})
.join('');
formattedData[newKey] = data[originalKey];
});
formattedData['folio'] = Number(folio) + Number(formattedData['n#']) - 1;
formattedData['barCodeNumber'] =
String(folio).padStart(7, '0') +
String(formattedData['numeroCliente']).padStart(8, '0') +
String(formattedData['totalPagar']).padStart(8, '0');
console.log(formattedData);
const templateHtml = await fs.readFile(billHtmlTemplatePath, 'utf8');
const template = Handlebars.compile(templateHtml);
const html = template(formattedData);
await fs.writeFile(billHtmlPath, html);
// console.log(billHtmlPath);
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(billHtmlPath);
const pdf = await page.pdf({
path: 'utility-bill.pdf', // Saves PDF directly to a file
format: 'A4',
printBackground: true,
});
await browser.close();
return pdf;
} catch (error) {
console.error(`Failed to create PDF: ${error}`);
}
}
module.exports = { generatePDF };