@measurequick/measurequick-report-generator 1.2.18 → 1.2.20

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@measurequick/measurequick-report-generator",
3
- "version": "1.2.18",
3
+ "version": "1.2.20",
4
4
  "description": "Generates PDF documents for various MeasureQuick applications.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -5,57 +5,66 @@ import * as photos6 from '../base-64/photos6.js';
5
5
  import * as base64 from '../base-64/icons.js';
6
6
  import * as util from '../util.js';
7
7
 
8
- export async function getReport(payload) { console.log("Photos Report"); console.log(payload);
9
- let docs = [], forms = [], page = photos6, size = "Small";
10
- if (payload.meta.pdf_settings.maxPhotosPerPage == 4) {
8
+ export async function getReport(payload) {
9
+
10
+ const mqLogoBytes = util._base64ToArrayBuffer(base64.mqLogo);
11
+ let maxPhotosPerPage = payload.meta.pdf_settings.maxPhotosPerPage;
12
+
13
+ // Layout Configuration
14
+ let docs = [], forms = [], page = photos6, pSize = "Small";
15
+ if (maxPhotosPerPage == 4) {
11
16
  page = photos4;
12
- size = "Mid";
13
- } else if (payload.meta.pdf_settings.maxPhotosPerPage == 2) {
17
+ pSize = "Mid";
18
+ } else if (maxPhotosPerPage == 2) {
14
19
  page = photos2;
15
- size = "Large";
20
+ pSize = "Large";
16
21
  }
17
- let keys = ["project", "equipment", "site", "customer"];
18
- let lastTag, currentTag;
19
- for (let k = 0; k < keys.length; k++) {
20
- let photoCount = 0, pageCount = 0, sectionNum = 0, count = 0;
21
- if (payload.photos[keys[k]]) {
22
- currentTag = keys[k];
23
- let sections = Object.keys(payload.photos[keys[k]]);
24
- for (const [section, data] of Object.entries(payload.photos[keys[k]])) photoCount += data.photos.length;
25
- pageCount = Math.ceil(photoCount / payload.meta.pdf_settings.maxPhotosPerPage);
26
- for (let i = 0; i < pageCount; i++) {
27
- const doc = await PDFDocument.load(util._base64ToArrayBuffer(page.base64));
28
- const form = doc.getForm();
29
- form.getTextField("Header").setText(`${util.capitalizeFirstLetter(keys[k])} Photos`);
30
- const mqLogoBytes = util._base64ToArrayBuffer(base64.mqLogo);
31
- const mqLogo = await doc.embedPng(mqLogoBytes);
32
- form.getButton("FullWidthLogo").setImage(mqLogo);
33
- docs.push(doc);
34
- forms.push(form);
35
- for (let j = 1; j <= payload.meta.pdf_settings.maxPhotosPerPage; j++) {
36
- let image;
37
- if (!payload.photos[keys[k]][sections[sectionNum]].photos[count]) {
38
- count = 0;
39
- sectionNum++;
40
- if (!payload.photos[keys[k]][sections[sectionNum]]) break;
41
- }
42
- let photo;
43
- if (payload.photos[keys[k]][sections[sectionNum]].photos[count]) {
44
- photo = payload.photos[keys[k]][sections[sectionNum]].photos[count].base64;
45
- count++;
46
- if (photo.includes("image/jpeg")) image = await doc.embedJpg(util._base64ToArrayBuffer(photo));
47
- else if (photo.includes("image/png")) image = await doc.embedPng(util._base64ToArrayBuffer(photo));
48
- if (image) form.getButton(`photo${size}${j}`).setImage(image);
49
- let firstPhoto = (lastTag != currentTag);
50
- lastTag = currentTag;
51
- let notes = payload.photos[keys[k]][sections[sectionNum]].name;
52
- if (payload.photos[keys[k]][sections[sectionNum]].hasNotes && firstPhoto) notes += ": " + payload.photos[keys[k]][sections[sectionNum]].notes;
53
- form.getTextField(`Notes${j}`).setText(`${notes}`);
54
- }
55
- }
56
- }
22
+
23
+ // Process All Photos
24
+ let previousPhotoCollectionName;
25
+ let photoCollectionNames = ["project", "equipment", "site", "customer"];
26
+ for (let pcnIndex = 0; pcnIndex < photoCollectionNames.length; pcnIndex++) { // Iterate over all 4 photo photo collections
27
+ let photoCollectionName = photoCollectionNames[pcnIndex];
28
+ let photoCollection = payload.photos[photoCollectionName];
29
+ let photoSectionKeys = Object.keys(photoCollection);
30
+ for (let pskIndex = 0; pskIndex < photoSectionKeys.length; pskIndex++) { // Iterate over X user-defined photo sections within each photo collection
31
+ let photoSectionKey = photoSectionKeys[pskIndex];
32
+ let photoSection = photoCollection[photoSectionKey];
33
+ let photos = photoSection.photos;
34
+ if (!photos || photos.length == 0) break;
35
+ let photoPosition = 1;
36
+ let doc = await PDFDocument.load(util._base64ToArrayBuffer(page.base64));
37
+ let form = doc.getForm();
38
+ docs.push(doc);
39
+ forms.push(form);
40
+ form.getButton("FullWidthLogo").setImage(await doc.embedPng(mqLogoBytes));
41
+ form.getTextField("Header").setText(`${util.capitalizeFirstLetter(photoCollectionName)} Photos`);
42
+ for (let pIndex = 1; pIndex <= photos.length; pIndex++) { // Iterate over X user-defined photos within each photo section
43
+ let photo = photos[pIndex-1];
44
+ if (photoPosition > maxPhotosPerPage) { // Need a new page
45
+ photoPosition = 1;
46
+ doc = await PDFDocument.load(util._base64ToArrayBuffer(page.base64));
47
+ form = doc.getForm();
48
+ docs.push(doc);
49
+ forms.push(form);
50
+ form.getButton("FullWidthLogo").setImage(await doc.embedPng(mqLogoBytes));
51
+ form.getTextField("Header").setText(`${util.capitalizeFirstLetter(photoCollectionName)} Photos`);
52
+ }
53
+ let imageToSet;
54
+ if (photo.base64.includes("image/jpeg")) imageToSet = await doc.embedJpg(util._base64ToArrayBuffer(photo.base64));
55
+ else if (photo.base64.includes("image/png")) imageToSet = await doc.embedPng(util._base64ToArrayBuffer(photo.base64));
56
+ if (imageToSet) form.getButton(`photo${pSize}${pIndex}`).setImage(imageToSet);
57
+ let photoSubText = photoSection.name;
58
+ let isFirstPhotoOfSection = previousPhotoCollectionName == photoCollectionName;
59
+ if (isFirstPhotoOfSection && photoSection.hasNotes) photoSubText += `: ${photoSection.notes}`;
60
+ form.getTextField(`Notes${pIndex}`).setText(`${photoSubText}`);
61
+ photoPosition++;
62
+ previousPhotoCollectionName = photoCollectionName;
57
63
  }
64
+ }
58
65
  }
66
+
67
+ // Prepare Deliverable
59
68
  forms.forEach(form => { form.flatten() });
60
69
  let pdfDoc = await PDFDocument.create();
61
70
  for (let i = 0; i < docs.length; i++) {
@@ -64,6 +73,3 @@ export async function getReport(payload) { console.log("Photos Report"); console
64
73
  }
65
74
  return await pdfDoc.saveAsBase64({ dataUri: true });
66
75
  }
67
-
68
- // MAX # of Characters for 2-Photo Pages: 95 Characters
69
- // MAX # of Characters for 4-Photo & 6-Photo Pages: 42 Characters