boletos-ademitech 1.0.3 → 1.0.5

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,41 @@ 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
+ if (Buffer.isBuffer(image)) {
603
+ return sharp(image)
604
+ .flatten({ background: '#FFFFFF' })
605
+ .jpeg({
606
+ quality: 100,
607
+ mozjpeg: false
608
+ })
609
+ .toBuffer();
610
+ }
611
+
612
+ if (typeof image === 'string' && image.startsWith('data:image')) {
613
+ const base64 = image.split(',')[1];
614
+
615
+ return sharp(Buffer.from(base64, 'base64'))
616
+ .flatten({ background: '#FFFFFF' })
617
+ .jpeg({
618
+ quality: 100,
619
+ mozjpeg: false
620
+ })
621
+ .toBuffer();
622
+ }
623
+
624
+ return sharp(image)
625
+ .flatten({ background: '#FFFFFF' })
626
+ .jpeg({
627
+ quality: 100,
628
+ mozjpeg: false
629
+ })
630
+ .toBuffer();
631
+ }
632
+
633
+ const logoBanco = await normalizeImageToJpegBuffer(banco.getImagem());
634
+
635
+ pdf.image(logoBanco, args.ajusteX + margemDoSegundoBlocoLayout, args.ajusteY + segundaLinha2 - 5, {
598
636
  height: alturaDoLogotipoDoBanco
599
637
  });
600
638
 
@@ -670,8 +708,10 @@ var GeradorDeBoleto = (function () {
670
708
  );
671
709
 
672
710
  // Gerar QR code e adicionar ao PDF
711
+ const qrCodeImage = await normalizeImageToJpegBuffer(boleto.getQrCode());
712
+
673
713
  pdf.image(
674
- boleto.getQrCode(),
714
+ qrCodeImage,
675
715
  args.ajusteX + 470,
676
716
  args.ajusteY + linha212 + 40,
677
717
  {
@@ -1047,7 +1087,7 @@ var GeradorDeBoleto = (function () {
1047
1087
 
1048
1088
  pdf.font('negrito')
1049
1089
  .fontSize(args.tamanhoDaFonteDoTitulo)
1050
- .text('Código de Baixa', args.ajusteX + 370, args.ajusteY + tituloDaSetimaLinha + 159, {
1090
+ .text('Código de Baixa', args.ajusteX + 370, args.ajusteY + tituloDaSetimaLinha + 182, {
1051
1091
  lineBreak: false,
1052
1092
  width: 294,
1053
1093
  align: 'left'
@@ -1055,7 +1095,7 @@ var GeradorDeBoleto = (function () {
1055
1095
 
1056
1096
  pdf.font('negrito')
1057
1097
  .fontSize(args.tamanhoDaFonteDoTitulo)
1058
- .text('Autenticação Mecânica', args.ajusteX + 360, args.ajusteY + tituloDaSetimaLinha + 171.5, {
1098
+ .text('Autenticação Mecânica', args.ajusteX + 360, args.ajusteY + tituloDaSetimaLinha + 192, {
1059
1099
  lineBreak: false,
1060
1100
  width: 294,
1061
1101
  align: 'left'
@@ -1149,7 +1189,7 @@ var GeradorDeBoleto = (function () {
1149
1189
  if (indice < boletos.length - 1) {
1150
1190
  pdf.addPage();
1151
1191
  }
1152
- });
1192
+ };
1153
1193
 
1154
1194
  if (args.base64) {
1155
1195
 
@@ -1169,6 +1209,9 @@ var GeradorDeBoleto = (function () {
1169
1209
  pdf.end();
1170
1210
  resolve(pdf);
1171
1211
  }
1212
+ } catch (error) {
1213
+ reject(error);
1214
+ }
1172
1215
  });
1173
1216
  };
1174
1217
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "boletos-ademitech",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
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",