@measurequick/measurequick-report-generator 1.3.13 → 1.3.14
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.
package/package.json
CHANGED
|
@@ -21,14 +21,12 @@ export async function getReport(payload) {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
// Process All Photos
|
|
24
|
+
let canProcessReport = false;
|
|
24
25
|
let previousPhotoSectionName;
|
|
25
26
|
let docIndex = 0;
|
|
27
|
+
let initialProjectPhotoPageLoaded = false;
|
|
26
28
|
let photoSectionKeys = Object.keys(payload.photos.project);
|
|
27
29
|
if (photoSectionKeys && photoSectionKeys.length > 0) {
|
|
28
|
-
docs[docIndex] = await PDFDocument.load(util._base64ToArrayBuffer(page.base64));
|
|
29
|
-
forms[docIndex] = docs[docIndex].getForm();
|
|
30
|
-
forms[docIndex].getButton("FullWidthLogo").setImage(await docs[docIndex].embedPng(mqLogoBytes));
|
|
31
|
-
forms[docIndex].getTextField("Header").setText(`Project Photos`);
|
|
32
30
|
let photoPosition = 1;
|
|
33
31
|
for (let pskIndex = 0; pskIndex < photoSectionKeys.length; pskIndex++) { // Iterate over X user-defined photo sections within each photo collection
|
|
34
32
|
let photoSectionKey = photoSectionKeys[pskIndex];
|
|
@@ -38,39 +36,47 @@ export async function getReport(payload) {
|
|
|
38
36
|
if (photos && photos.length > 0) {
|
|
39
37
|
for (let pIndex = 1; pIndex <= photos.length; pIndex++) { // Iterate over X user-defined photos within each photo section
|
|
40
38
|
let photo = photos[pIndex-1];
|
|
41
|
-
|
|
42
|
-
photoPosition = 1;
|
|
43
|
-
docIndex++;
|
|
44
|
-
docs[docIndex] = await PDFDocument.load(util._base64ToArrayBuffer(page.base64));
|
|
45
|
-
forms[docIndex] = docs[docIndex].getForm();
|
|
46
|
-
forms[docIndex].getButton("FullWidthLogo").setImage(await docs[docIndex].embedPng(mqLogoBytes));
|
|
47
|
-
forms[docIndex].getTextField("Header").setText(`Project Photos`);
|
|
48
|
-
}
|
|
49
|
-
let imageToSet;
|
|
39
|
+
//If image has proper photo data, continue processing, else do not try and make an empty page.
|
|
50
40
|
if (photo && photo.base64 && typeof photo.base64 == 'string') {
|
|
51
|
-
|
|
52
|
-
|
|
41
|
+
if(!initialProjectPhotoPageLoaded){
|
|
42
|
+
initialProjectPhotoPageLoaded = true;
|
|
43
|
+
canProcessReport = true;
|
|
44
|
+
//Initial Page, this needs to only publish this page IF and only IF it has valid image data
|
|
45
|
+
docs[docIndex] = await PDFDocument.load(util._base64ToArrayBuffer(page.base64));
|
|
46
|
+
forms[docIndex] = docs[docIndex].getForm();
|
|
47
|
+
forms[docIndex].getButton("FullWidthLogo").setImage(await docs[docIndex].embedPng(mqLogoBytes));
|
|
48
|
+
forms[docIndex].getTextField("Header").setText(`Project Photos`);
|
|
49
|
+
}
|
|
50
|
+
if (photoPosition > maxPhotosPerPage) { // Need a new page
|
|
51
|
+
photoPosition = 1;
|
|
52
|
+
docIndex++;
|
|
53
|
+
docs[docIndex] = await PDFDocument.load(util._base64ToArrayBuffer(page.base64));
|
|
54
|
+
forms[docIndex] = docs[docIndex].getForm();
|
|
55
|
+
forms[docIndex].getButton("FullWidthLogo").setImage(await docs[docIndex].embedPng(mqLogoBytes));
|
|
56
|
+
forms[docIndex].getTextField("Header").setText(`Project Photos`);
|
|
57
|
+
}
|
|
58
|
+
let imageToSet;
|
|
59
|
+
if (photo && photo.base64 && typeof photo.base64 == 'string') {
|
|
60
|
+
if (photo.base64.includes("image/jpeg")) imageToSet = await docs[docIndex].embedJpg(util._base64ToArrayBuffer(photo.base64));
|
|
61
|
+
else if (photo.base64.includes("image/png")) imageToSet = await docs[docIndex].embedPng(util._base64ToArrayBuffer(photo.base64));
|
|
62
|
+
}
|
|
63
|
+
if (imageToSet) forms[docIndex].getButton(`photo${pSize}${photoPosition}`).setImage(imageToSet);
|
|
64
|
+
let photoSubText = photoSection.name;
|
|
65
|
+
let isFirstPhotoOfSection = previousPhotoSectionName !== photoSectionName;
|
|
66
|
+
if (isFirstPhotoOfSection && photoSection.hasNotes && photoSection.notes) photoSubText += `: ${photoSection.notes}`;
|
|
67
|
+
forms[docIndex].getTextField(`Notes${photoPosition}`).setText(`${photoSubText}`);
|
|
68
|
+
photoPosition++;
|
|
69
|
+
previousPhotoSectionName = photoSectionName;
|
|
53
70
|
}
|
|
54
|
-
if (imageToSet) forms[docIndex].getButton(`photo${pSize}${photoPosition}`).setImage(imageToSet);
|
|
55
|
-
let photoSubText = photoSection.name;
|
|
56
|
-
let isFirstPhotoOfSection = previousPhotoSectionName !== photoSectionName;
|
|
57
|
-
if (isFirstPhotoOfSection && photoSection.hasNotes && photoSection.notes) photoSubText += `: ${photoSection.notes}`;
|
|
58
|
-
forms[docIndex].getTextField(`Notes${photoPosition}`).setText(`${photoSubText}`);
|
|
59
|
-
photoPosition++;
|
|
60
|
-
previousPhotoSectionName = photoSectionName;
|
|
61
71
|
}
|
|
62
72
|
}
|
|
63
73
|
}
|
|
64
|
-
docIndex++;
|
|
65
74
|
}
|
|
66
75
|
|
|
76
|
+
let initialEquipmentPhotoPageLoaded = false;
|
|
67
77
|
photoSectionKeys = Object.keys(payload.photos.equipment);
|
|
68
78
|
let hasPhotos = payload.photos.equipment && payload.photos.equipment.photo_models_serials && payload.photos.equipment.photo_models_serials.photos && payload.photos.equipment.photo_models_serials.photos.length > 0;
|
|
69
79
|
if (photoSectionKeys && photoSectionKeys.length > 0 && hasPhotos) {
|
|
70
|
-
docs[docIndex] = await PDFDocument.load(util._base64ToArrayBuffer(page.base64));
|
|
71
|
-
forms[docIndex] = docs[docIndex].getForm();
|
|
72
|
-
forms[docIndex].getButton("FullWidthLogo").setImage(await docs[docIndex].embedPng(mqLogoBytes));
|
|
73
|
-
forms[docIndex].getTextField("Header").setText(`Equipment Photos`);
|
|
74
80
|
let photoPosition = 1;
|
|
75
81
|
for (let pskIndex = 0; pskIndex < photoSectionKeys.length; pskIndex++) { // Iterate over X user-defined photo sections within each photo collection
|
|
76
82
|
let photoSectionKey = photoSectionKeys[pskIndex];
|
|
@@ -80,26 +86,39 @@ export async function getReport(payload) {
|
|
|
80
86
|
if (photos && photos.length > 0) {
|
|
81
87
|
for (let pIndex = 1; pIndex <= photos.length; pIndex++) { // Iterate over X user-defined photos within each photo section
|
|
82
88
|
let photo = photos[pIndex-1];
|
|
83
|
-
if (photoPosition > maxPhotosPerPage) { // Need a new page
|
|
84
|
-
photoPosition = 1;
|
|
85
|
-
docIndex++;
|
|
86
|
-
docs[docIndex] = await PDFDocument.load(util._base64ToArrayBuffer(page.base64));
|
|
87
|
-
forms[docIndex] = docs[docIndex].getForm();
|
|
88
|
-
forms[docIndex].getButton("FullWidthLogo").setImage(await docs[docIndex].embedPng(mqLogoBytes));
|
|
89
|
-
forms[docIndex].getTextField("Header").setText(`Equipment Photos`);
|
|
90
|
-
}
|
|
91
|
-
let imageToSet;
|
|
92
89
|
if (photo && photo.base64 && typeof photo.base64 == 'string') {
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
//Initial page load, this should wait until we know there is VALID image data
|
|
91
|
+
if (!initialEquipmentPhotoPageLoaded){
|
|
92
|
+
initialEquipmentPhotoPageLoaded = true;
|
|
93
|
+
canProcessReport = true;
|
|
94
|
+
if (initialProjectPhotoPageLoaded && docIndex===0) docIndex++;
|
|
95
|
+
docs[docIndex] = await PDFDocument.load(util._base64ToArrayBuffer(page.base64));
|
|
96
|
+
forms[docIndex] = docs[docIndex].getForm();
|
|
97
|
+
forms[docIndex].getButton("FullWidthLogo").setImage(await docs[docIndex].embedPng(mqLogoBytes));
|
|
98
|
+
forms[docIndex].getTextField("Header").setText(`Equipment Photos`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (photoPosition > maxPhotosPerPage) { // Need a new page
|
|
102
|
+
photoPosition = 1;
|
|
103
|
+
docIndex++;
|
|
104
|
+
docs[docIndex] = await PDFDocument.load(util._base64ToArrayBuffer(page.base64));
|
|
105
|
+
forms[docIndex] = docs[docIndex].getForm();
|
|
106
|
+
forms[docIndex].getButton("FullWidthLogo").setImage(await docs[docIndex].embedPng(mqLogoBytes));
|
|
107
|
+
forms[docIndex].getTextField("Header").setText(`Equipment Photos`);
|
|
108
|
+
}
|
|
109
|
+
let imageToSet;
|
|
110
|
+
if (photo && photo.base64 && typeof photo.base64 == 'string') {
|
|
111
|
+
if (photo.base64.includes("image/jpeg")) imageToSet = await docs[docIndex].embedJpg(util._base64ToArrayBuffer(photo.base64));
|
|
112
|
+
else if (photo.base64.includes("image/png")) imageToSet = await docs[docIndex].embedPng(util._base64ToArrayBuffer(photo.base64));
|
|
113
|
+
}
|
|
114
|
+
if (imageToSet) forms[docIndex].getButton(`photo${pSize}${photoPosition}`).setImage(imageToSet);
|
|
115
|
+
let photoSubText = photoSection.name;
|
|
116
|
+
let isFirstPhotoOfSection = previousPhotoSectionName !== photoSectionName;
|
|
117
|
+
if (isFirstPhotoOfSection && photoSection.hasNotes && photoSection.notes) photoSubText += `: ${photoSection.notes}`;
|
|
118
|
+
forms[docIndex].getTextField(`Notes${photoPosition}`).setText(`${photoSubText}`);
|
|
119
|
+
photoPosition++;
|
|
120
|
+
previousPhotoSectionName = photoSectionName;
|
|
95
121
|
}
|
|
96
|
-
if (imageToSet) forms[docIndex].getButton(`photo${pSize}${photoPosition}`).setImage(imageToSet);
|
|
97
|
-
let photoSubText = photoSection.name;
|
|
98
|
-
let isFirstPhotoOfSection = previousPhotoSectionName !== photoSectionName;
|
|
99
|
-
if (isFirstPhotoOfSection && photoSection.hasNotes && photoSection.notes) photoSubText += `: ${photoSection.notes}`;
|
|
100
|
-
forms[docIndex].getTextField(`Notes${photoPosition}`).setText(`${photoSubText}`);
|
|
101
|
-
photoPosition++;
|
|
102
|
-
previousPhotoSectionName = photoSectionName;
|
|
103
122
|
}
|
|
104
123
|
}
|
|
105
124
|
}
|
|
@@ -107,12 +126,17 @@ export async function getReport(payload) {
|
|
|
107
126
|
}
|
|
108
127
|
|
|
109
128
|
// Prepare Deliverable
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
129
|
+
if (canProcessReport){
|
|
130
|
+
forms.forEach(form => { form.flatten() });
|
|
131
|
+
let pdfDoc = await PDFDocument.create();
|
|
132
|
+
for (let i = 0; i < docs.length; i++) {
|
|
133
|
+
const [nextPage] = await pdfDoc.copyPages(docs[i], [0]);
|
|
134
|
+
pdfDoc.insertPage(i, nextPage);
|
|
135
|
+
}
|
|
136
|
+
return await pdfDoc.saveAsBase64({ dataUri: true });
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
return false;
|
|
115
140
|
}
|
|
116
|
-
return await pdfDoc.saveAsBase64({ dataUri: true });
|
|
117
141
|
} catch (error) {}
|
|
118
142
|
}
|