@ind-rcg/plugins-printengine 252.1008.0 → 258.1001.0

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.
@@ -111,6 +111,7 @@ class MacroHandler {
111
111
  for (let i = 0; i < macroList.length; i++) {
112
112
  this.__parseKeyValuePair(macroList[i], result);
113
113
  }
114
+ // eslint-disable-next-line no-unused-vars
114
115
  } catch (err){
115
116
  // some invalid value encountered during parse
116
117
  result = {};
@@ -10,6 +10,7 @@ const TableCellIdentifier = require('./tableCellIdentifier');
10
10
 
11
11
  // required for webpack include (browser impl), used in conjunction with BrowserFonts definition
12
12
  const BrowserFontsVfs = require('pdfmake/build/vfs_fonts.js');
13
+ const classLog = require("./logClass");
13
14
 
14
15
  // => use 'Roboto' font (pdfMake default) due to licensing (Apache 2.0)
15
16
  // https://fonts.google.com/specimen/Roboto || https://en.wikipedia.org/wiki/Roboto
@@ -82,6 +83,9 @@ const TableLayouts = {
82
83
  }
83
84
  };
84
85
 
86
+ const ThermalPrintLayoutPageMargins = [5,5,5,5];
87
+ let __log = new classLog();
88
+
85
89
  const STYLE_HEADING1 = "heading1";
86
90
  const STYLE_HEADING2 = "heading2";
87
91
  const STYLE_PARAGRAPH = "paragraph";
@@ -105,6 +109,10 @@ class PdfConverter {
105
109
  this.__pdfHeight = null;
106
110
  }
107
111
 
112
+ __isThermalPrintLayout() {
113
+ return this.__pdfHeight === 'auto';
114
+ }
115
+
108
116
  __applyPageBreakData(pdfDefinition) {
109
117
  if (!_.isUndefined(pdfDefinition.dynamicPageBreak) && pdfDefinition.dynamicPageBreak &&
110
118
  !_.isUndefined(pdfDefinition.dynamicPageBreakData) && _.isArray(pdfDefinition.dynamicPageBreakData) && pdfDefinition.dynamicPageBreakData.length > 0) {
@@ -497,9 +505,9 @@ class PdfConverter {
497
505
  return result;
498
506
  }
499
507
 
500
- __createTableColumn(content, style, colSpan, rowSpan, alignment, bold, italics, cellId) {
501
- let bTextOnly = _.isNil(content.image) && _.isNil(style) && _.isNil(colSpan) && _.isNil(rowSpan)
502
- && _.isNil(alignment) && _.isNil(bold) && _.isNil(italics) && _.isNil(cellId);
508
+ __createTableColumn(content, style, attributesJson) {
509
+ let bTextOnly = _.isNil(content.image) && _.isNil(style) && _.isNil(attributesJson.colSpan) && _.isNil(attributesJson.rowSpan)
510
+ && _.isNil(attributesJson.alignment) && _.isNil(attributesJson.bold) && _.isNil(attributesJson.italics) && _.isNil(attributesJson.cellId);
503
511
 
504
512
  if (bTextOnly) {
505
513
  return content.text;
@@ -517,23 +525,23 @@ class PdfConverter {
517
525
  if (!_.isNil(style)) {
518
526
  column.style = style;
519
527
  }
520
- if (!_.isNil(colSpan)) {
521
- column.colSpan = colSpan;
528
+ if (!_.isNil(attributesJson.colSpan)) {
529
+ column.colSpan = attributesJson.colSpan;
522
530
  }
523
- if (!_.isNil(rowSpan)) {
524
- column.rowSpan = rowSpan;
531
+ if (!_.isNil(attributesJson.rowSpan)) {
532
+ column.rowSpan = attributesJson.rowSpan;
525
533
  }
526
- if (!_.isNil(alignment)) {
527
- column.alignment = alignment;
534
+ if (!_.isNil(attributesJson.alignment)) {
535
+ column.alignment = attributesJson.alignment;
528
536
  }
529
- if (!_.isNil(bold)) {
530
- column.bold = bold;
537
+ if (!_.isNil(attributesJson.bold)) {
538
+ column.bold = attributesJson.bold;
531
539
  }
532
- if (!_.isNil(italics)) {
533
- column.italics = italics;
540
+ if (!_.isNil(attributesJson.italics)) {
541
+ column.italics = attributesJson.italics;
534
542
  }
535
- if (!_.isNil(cellId)) {
536
- column.id = cellId;
543
+ if (!_.isNil(attributesJson.cellId)) {
544
+ column.id = attributesJson.cellId;
537
545
  }
538
546
 
539
547
  return column;
@@ -547,7 +555,14 @@ class PdfConverter {
547
555
  let alignmentAttribute = this.__checkEnumAttribute(thOrTdNode, 'alignment', ["left", "center", "right"], null);
548
556
  let cellId = this.__checkStringAttribute(thOrTdNode, 'id', null);
549
557
 
550
- return [colSpanAttributeValue, rowSpanAttributeValue, alignmentAttribute, boldAttribute, italicsAttribute, cellId];
558
+ return {
559
+ colSpan: colSpanAttributeValue,
560
+ rowSpan: rowSpanAttributeValue,
561
+ alignment: alignmentAttribute,
562
+ bold: boldAttribute,
563
+ italics: italicsAttribute,
564
+ cellId: cellId
565
+ };
551
566
  }
552
567
 
553
568
  __evaluateTableDefinitionRowBreak(tableNode, dynamicPageBreak) {
@@ -605,24 +620,10 @@ class PdfConverter {
605
620
  let bNoWidths = false;
606
621
  let theadNode = _.find(tableNode.elements, {name: 'thead'});
607
622
  let tbodyNode = _.find(tableNode.elements, {name: 'tbody'});
623
+ let theadAlignmentValues = [];
608
624
 
609
625
  if(!_.isNil(theadNode)) {
610
- _.forEach(theadNode.elements, headerTrNode=> {
611
- let row = [];
612
- _.forEach(headerTrNode.elements, value => {
613
- let attributeValues = this.__getTableCellAttributes(value);
614
-
615
- if (pdfTableDefinition.table.headerRows === 0) {
616
- let widthAttributeValue = this.__checkIntegerAttribute(value, 'width', ['*', 'auto'], true, 'auto');
617
- pdfTableDefinition.table.widths.push(widthAttributeValue);
618
- }
619
- let content = this.__getThOrTdContent(value, settings);
620
- row.push(this.__createTableColumn(content, 'tableHeader', ...attributeValues));
621
- });
622
-
623
- pdfTableDefinition.table.body.push(row);
624
- pdfTableDefinition.table.headerRows += 1;
625
- });
626
+ this.__getTheadNodeDefinition(theadNode, pdfTableDefinition, settings, theadAlignmentValues);
626
627
  } else if(!_.isNil(tbodyNode)) {
627
628
  bNoWidths = true;
628
629
  } else {
@@ -633,24 +634,7 @@ class PdfConverter {
633
634
  }
634
635
 
635
636
  if(!_.isNil(tbodyNode)) {
636
- _.forEach(tbodyNode.elements, tbodyTrNode=> {
637
- let row = [];
638
- _.forEach(tbodyTrNode.elements, value => {
639
- let attributeValues = this.__getTableCellAttributes(value);
640
-
641
- if ((bNoWidths === true)) {
642
- let widthAttributeValue = this.__checkIntegerAttribute(value, 'width', ['*', 'auto'], true, 'auto');
643
- pdfTableDefinition.table.widths.push(widthAttributeValue);
644
- }
645
- let content = this.__getThOrTdContent(value, settings);
646
- content.text = this.__applyNumberFormatAttribute(value, content.text);
647
-
648
- row.push(this.__createTableColumn(content, null, ...attributeValues));
649
- });
650
-
651
- bNoWidths = false;
652
- pdfTableDefinition.table.body.push(row);
653
- });
637
+ this.__getTbodyNodeDefinition(tbodyNode, pdfTableDefinition, bNoWidths, settings, theadAlignmentValues);
654
638
  }
655
639
 
656
640
  this.__checkSpansAndColumns(pdfTableDefinition);
@@ -658,8 +642,89 @@ class PdfConverter {
658
642
  return pdfTableDefinition;
659
643
  }
660
644
 
645
+ __getTheadNodeDefinition(theadNode, pdfTableDefinition, settings, theadAlignmentValues) {
646
+ _.forEach(theadNode.elements, headerTrNode=> {
647
+ let row = [];
648
+ _.forEach(headerTrNode.elements, value => {
649
+ let attributesJson = this.__getTableCellAttributes(value);
650
+
651
+ if (pdfTableDefinition.table.headerRows === 0) {
652
+ let widthAttributeValue = this.__checkIntegerAttribute(value, 'width', ['*', 'auto'], true, 'auto');
653
+ pdfTableDefinition.table.widths.push(widthAttributeValue);
654
+ }
655
+ let content = this.__getThOrTdContent(value, settings);
656
+ row.push(this.__createTableColumn(content, 'tableHeader', attributesJson));
657
+
658
+ //store the alignment values from th to be set to corresponding td for thermal print layout
659
+ if(this.__isThermalPrintLayout()) {
660
+ theadAlignmentValues.push(attributesJson.alignment || 'left');
661
+ }
662
+ });
663
+
664
+ pdfTableDefinition.table.body.push(row);
665
+ pdfTableDefinition.table.headerRows += 1;
666
+ });
667
+ }
668
+
669
+ __getTbodyNodeDefinition(tbodyNode, pdfTableDefinition, bNoWidths, settings, theadAlignmentValues) {
670
+ _.forEach(tbodyNode.elements, tbodyTrNode=> {
671
+ let row = [];
672
+ _.forEach(tbodyTrNode.elements, (value, index) => {
673
+ //for thermal printer layout the alignment value for each td should be same as that column's th OR by default left
674
+ //if alignment for th is not defined
675
+ if (this.__isThermalPrintLayout()) {
676
+ value.attributes = value.attributes || {}; // Ensure attributes exist for the tdNode
677
+ value.attributes.alignment = theadAlignmentValues[index] || 'left'; // Set alignment to the corresponding th
678
+ }
679
+ let attributesJson = this.__getTableCellAttributes(value);
680
+ if ((bNoWidths === true)) {
681
+ let widthAttributeValue = this.__checkIntegerAttribute(value, 'width', ['*', 'auto'], true, 'auto');
682
+ pdfTableDefinition.table.widths.push(widthAttributeValue);
683
+ }
684
+ let content = this.__getThOrTdContent(value, settings);
685
+ content.text = this.__applyNumberFormatAttribute(value, content.text);
686
+
687
+ row.push(this.__createTableColumn(content, null, attributesJson));
688
+ });
689
+
690
+ bNoWidths = false;
691
+ pdfTableDefinition.table.body.push(row);
692
+ });
693
+ }
694
+
695
+ __getLineFeedDefinition() {
696
+ return { "text": "\n" };
697
+ }
698
+
699
+ __getRuleDefinition(definition) {
700
+ //for thermal PL pageMargins will always be ThermalPrintLayoutPageMargins
701
+ const horizontalMarginSum = ThermalPrintLayoutPageMargins[0] + ThermalPrintLayoutPageMargins[2];
702
+ const x2 = definition.pageSize.width - horizontalMarginSum;
703
+ let result = {
704
+ canvas: [
705
+ {
706
+ type: 'line',
707
+ x1: 0, y1: 0,
708
+ x2: x2, y2: 0,
709
+ lineWidth: 1,
710
+ dash: {
711
+ length: 4,
712
+ space: 2
713
+ }
714
+ },
715
+ ]
716
+ };
717
+ return result;
718
+ }
719
+
661
720
  __addBody(definition, json) {
662
721
 
722
+ //requirement: for thermal PL the watermark should be added at the top and bottom of the preview
723
+ //add watermark at the top of the body
724
+ if(this.__isThermalPrintLayout()) {
725
+ this.__getWatermarkDefinitionForThermalPrintLayout(definition, json);
726
+ }
727
+
663
728
  _.forEach(json.elements, contractElement => {
664
729
  if(contractElement.name === 'table') {
665
730
  let tableDefintion = this.__getTableDefinition(contractElement);
@@ -676,9 +741,50 @@ class PdfConverter {
676
741
  } else if (contractElement.name === 'img' || contractElement.name === 'signature') {
677
742
  let imageDefintion = this.__getImageDefinition(contractElement, true);
678
743
  definition.content.push(imageDefintion);
744
+ } else if (contractElement.name === 'lineFeed') {
745
+ if(this.__isThermalPrintLayout()) {
746
+ let lineFeedDefintion = this.__getLineFeedDefinition();
747
+ definition.content.push(lineFeedDefintion);
748
+ } else {
749
+ //linefeed is only supported for thermal PL so log a warning and ignore
750
+ __log.warn(contractElement.name + ' is supported only with thermal print layouts.');
751
+ }
752
+ } else if (contractElement.name === 'rule') {
753
+ if(this.__isThermalPrintLayout()) {
754
+ let ruleDefintion = this.__getRuleDefinition(definition);
755
+ definition.content.push(ruleDefintion);
756
+ } else {
757
+ //rule is only supported for thermal PL so log a warning and ignore
758
+ __log.warn(contractElement.name + ' is supported only with thermal print layouts.');
759
+ }
679
760
  }
680
761
  });
681
762
 
763
+ //add watermark at the bottom of the body
764
+ if(this.__isThermalPrintLayout()) {
765
+ this.__getWatermarkDefinitionForThermalPrintLayout(definition, json);
766
+ }
767
+
768
+ }
769
+
770
+ __getWatermarkDefinitionForThermalPrintLayout(definition, reportLayout) {
771
+ if(!_.isNil(reportLayout.attributes) && !_.isNil(reportLayout.attributes.watermark)) {
772
+ let watermarkDefinition = { "text": reportLayout.attributes.watermark };
773
+
774
+ //for thermal PL the watermark font size should be same as the H2 font size
775
+ let styleJson = this.__definition.styles[STYLE_HEADING2];
776
+
777
+ if (!_.isNil(styleJson.fontSize)) {
778
+ watermarkDefinition.fontSize = styleJson.fontSize;
779
+ }
780
+
781
+ //the watermark style should bold and center aligned
782
+ watermarkDefinition.bold = true;
783
+ watermarkDefinition.alignment = "center";
784
+ watermarkDefinition.margin = [0, 5, 0, 5];
785
+
786
+ definition.content.push(watermarkDefinition);
787
+ }
682
788
  }
683
789
 
684
790
  __addHeader(definition, json) {
@@ -765,9 +871,15 @@ class PdfConverter {
765
871
  }
766
872
  }
767
873
  if(!_.isNil(reportLayout.attributes) && !_.isNil(reportLayout.attributes.watermark)){
768
- definition.watermark = {text: reportLayout.attributes.watermark, opacity:0.1, fontSize: 120};
874
+ //for thermal PL the watermark shouldn't be shown in the background so added a check
875
+ if(!this.__isThermalPrintLayout()) {
876
+ definition.watermark = {text: reportLayout.attributes.watermark, opacity:0.1, fontSize: 120};
877
+ }
769
878
  }
770
- if(!_.isNil(reportLayout.attributes) && !_.isNil(reportLayout.attributes.pageMargins)){
879
+ //for bluetooth thermal PL the pageMargins is not supported from the contracts and should be provided implicitly
880
+ if(this.__isThermalPrintLayout()) {
881
+ definition.pageMargins = ThermalPrintLayoutPageMargins;
882
+ } else if(!_.isNil(reportLayout.attributes) && !_.isNil(reportLayout.attributes.pageMargins)){
771
883
  definition.pageMargins = JSON.parse(reportLayout.attributes.pageMargins);
772
884
  }
773
885
  }
@@ -1095,7 +1207,7 @@ class PdfConverter {
1095
1207
  } else {
1096
1208
  let pdfDocGenerator = null;
1097
1209
  if (pdfDefinition.defaultStyle.font !== "PrintFont") {
1098
- pdfDocGenerator = this.__pdfMake.createPdf(pdfDefinition, TableLayouts, BrowserFonts, BrowserFontsVfs.pdfMake.vfs);
1210
+ pdfDocGenerator = this.__pdfMake.createPdf(pdfDefinition, TableLayouts, BrowserFonts, BrowserFontsVfs.vfs);
1099
1211
  }
1100
1212
  else {
1101
1213
  pdfDocGenerator = this.__pdfMake.createPdf(pdfDefinition, TableLayouts, BrowserFonts, pdfDefinition.printFont);