boletos-ademitech 1.0.1 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,12 @@
1
1
  'use strict';
2
2
 
3
3
  const path = require('path');
4
+ const fs = require('fs');
4
5
  const StringUtils = require('../utils/string-utils');
5
6
  const ObjectUtils = require('../utils/object-utils');
6
7
  const Pdf = require('pdfkit');
7
8
  const {Base64Encode} = require('base64-stream');
9
+ const sharp = require('sharp');
8
10
 
9
11
  const GeradorDeLinhaDigitavel = require('./gerador-de-linha-digitavel');
10
12
  const diretorioDeFontes = path.join(__dirname, '/fontes');
@@ -44,7 +46,8 @@ var GeradorDeBoleto = (function () {
44
46
  };
45
47
 
46
48
  GeradorDeBoleto.prototype.gerarPDF = function (args) {
47
- return new Promise((resolve) => {
49
+ return new Promise(async (resolve, reject) => {
50
+ try {
48
51
  if (typeof args === 'function') {
49
52
  args = pdfDefaults;
50
53
  }
@@ -75,7 +78,8 @@ var GeradorDeBoleto = (function () {
75
78
  pdf.registerFont('negrito-italico', timesNewRomanNegritoItalico);
76
79
  pdf.registerFont('codigoDeBarras', code25I);
77
80
 
78
- boletos.forEach(function escreveOsDadosDoBoleto(boleto, indice) {
81
+ for (let indice = 0; indice < boletos.length; indice++) {
82
+ const boleto = boletos[indice];
79
83
  // DESENHAR LAYOUT
80
84
 
81
85
  const banco = boleto.getBanco();
@@ -594,7 +598,39 @@ var GeradorDeBoleto = (function () {
594
598
  const codigoDeBarras = banco.geraCodigoDeBarrasPara(boleto);
595
599
  const linhaDigitavel = GeradorDeLinhaDigitavel(codigoDeBarras, banco);
596
600
 
597
- pdf.image(banco.getImagem(), args.ajusteX + margemDoSegundoBlocoLayout, args.ajusteY + segundaLinha2 - 5, {
601
+ async function normalizeImageToJpegBuffer(image) {
602
+ return sharp(image)
603
+ .flatten({ background: '#FFFFFF' })
604
+ .jpeg({
605
+ quality: 100,
606
+ mozjpeg: false
607
+ })
608
+ .toBuffer();
609
+ }
610
+
611
+ async function normalizeQrCodeToPngBuffer(image) {
612
+ if (Buffer.isBuffer(image)) {
613
+ return sharp(image)
614
+ .png()
615
+ .toBuffer();
616
+ }
617
+
618
+ if (typeof image === 'string' && image.startsWith('data:image')) {
619
+ const base64 = image.split(',')[1];
620
+
621
+ return sharp(Buffer.from(base64, 'base64'))
622
+ .png()
623
+ .toBuffer();
624
+ }
625
+
626
+ return sharp(image)
627
+ .png()
628
+ .toBuffer();
629
+ }
630
+
631
+ const logoBanco = await normalizeImageToJpegBuffer(banco.getImagem());
632
+
633
+ pdf.image(logoBanco, args.ajusteX + margemDoSegundoBlocoLayout, args.ajusteY + segundaLinha2 - 5, {
598
634
  height: alturaDoLogotipoDoBanco
599
635
  });
600
636
 
@@ -670,8 +706,10 @@ var GeradorDeBoleto = (function () {
670
706
  );
671
707
 
672
708
  // Gerar QR code e adicionar ao PDF
709
+ const qrCodeImage = await normalizeQrCodeToPngBuffer(boleto.getQrCode());
710
+
673
711
  pdf.image(
674
- boleto.getQrCode(),
712
+ qrCodeImage,
675
713
  args.ajusteX + 470,
676
714
  args.ajusteY + linha212 + 40,
677
715
  {
@@ -1149,7 +1187,7 @@ var GeradorDeBoleto = (function () {
1149
1187
  if (indice < boletos.length - 1) {
1150
1188
  pdf.addPage();
1151
1189
  }
1152
- });
1190
+ };
1153
1191
 
1154
1192
  if (args.base64) {
1155
1193
 
@@ -1169,6 +1207,9 @@ var GeradorDeBoleto = (function () {
1169
1207
  pdf.end();
1170
1208
  resolve(pdf);
1171
1209
  }
1210
+ } catch (error) {
1211
+ reject(error);
1212
+ }
1172
1213
  });
1173
1214
  };
1174
1215
 
@@ -611,7 +611,7 @@ var Boleto = (function () {
611
611
  diferencaEmDias = (diferencaEmDias % 10000);
612
612
  }
613
613
 
614
- return Math.floor(diferencaEmDias).toString();
614
+ return Math.floor(diferencaEmDias).toString().padStart(4, '0');
615
615
  };
616
616
 
617
617
  Boleto.prototype.comEspecieMoeda = function (_especieMoeda) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "boletos-ademitech",
3
- "version": "1.0.1",
3
+ "version": "1.0.4",
4
4
  "description": "Gerar boletos para vários bancos",
5
5
  "main": "lib/index.js",
6
6
  "directories": {
@@ -47,7 +47,8 @@
47
47
  "moment": "^2.30.1",
48
48
  "moment-timezone": "^0.5.45",
49
49
  "pdfkit": "0.10.0",
50
- "qrcode": "^1.5.3"
50
+ "qrcode": "^1.5.3",
51
+ "sharp": "^0.34.5"
51
52
  },
52
53
  "devDependencies": {
53
54
  "eslint": "^6.5.1",