This repository was archived by the owner on Oct 1, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathgenerate-timbres.js
More file actions
65 lines (56 loc) · 2.69 KB
/
Copy pathgenerate-timbres.js
File metadata and controls
65 lines (56 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
const fs = require('fs-extra');
const { create } = require('xmlbuilder2');
const bwipjs = require('bwip-js');
const { cN, getSheetData } = require('./database.js');
const paths = require('./paths.js');
const signedBoletaDteFolderPath = paths.getSignedBoletaDteFolderPath();
const timbresBoletaFolderPath = paths.getTimbresBoletaFolderPath();
const barrasBoletaFolderPath = paths.getBarCodesBoletaFolderPath();
async function generateBarcodes() {
try {
const files = await fs.readdir(signedBoletaDteFolderPath);
for (const [index, file] of files.entries()) {
const fileContents = await fs.readFile(`${signedBoletaDteFolderPath}\\${file}`, 'utf8');
const tedXml = create(fileContents).end();
const tedString = tedXml.slice(tedXml.indexOf('<TED'), tedXml.indexOf('<TmstFirma>'));
const rut = tedString.slice(tedString.indexOf('<RR>'), tedString.indexOf('</RR>')).replace('<RR>', '');
const timbre = await bwipjs.toBuffer({
bcid: 'pdf417', // Barcode type
text: tedString, // Text to encode
scale: 10, // 1x scaling factor for exact control
height: 20, // Y Dimension (Row Height), 3:1 ratio to X Dim
width: 41, // X Dimension (X Width)
includetext: false, // No text below barcode
columns: 13, // Adjust column count as needed
eclevel: 5, // Error correction level
padding: 2, // Quiet Zone padding (0.25 inches approximated to module width)
compaction: 'binary', // Byte Compaction Mode
});
await fs.writeFile(`${timbresBoletaFolderPath}\\${rut}.png`, timbre);
const excelDataObject = await getSheetData(0);
const cdgIntRecep = String(excelDataObject[index][cN().cdgIntRecep]);
const barra = await bwipjs.toBuffer({
bcid: 'code128', // Barcode type
text: cdgIntRecep, // Text to encode
scale: 10, // 1x scaling factor for exact control
height: 10, // Y Dimension (Row Height), 3:1 ratio to X Dim
width: 60, // X Dimension (X Width)
includetext: false, // No text below barcode
eclevel: 5, // Error correction level
padding: 2, // Quiet Zone padding (0.25 inches approximated to module width)
compaction: 'binary', // Byte Compaction Mode
});
if (!excelDataObject[index][cN().isFactura]) {
await fs.writeFile(`${barrasBoletaFolderPath}\\${cdgIntRecep}.png`, barra);
}
}
const logGenerateBarcodes = `Barcodes successfully saved to drive`;
console.log(logGenerateBarcodes);
return logGenerateBarcodes;
} catch (error) {
const logError = `ERROR: Failed to generate PDF417 Timbres: ${error}`;
console.log(logError);
return logError;
}
}
module.exports = { generateBarcodes };