diff --git a/app/api/pouchQueryLicNo.json b/app/api/pouchQueryLicNo.json new file mode 100644 index 0000000..18cca0a --- /dev/null +++ b/app/api/pouchQueryLicNo.json @@ -0,0 +1,3 @@ +{ + "exec": {} +} \ No newline at end of file diff --git a/public/PDF/libPDFscripts - backup1.js b/public/PDF/libPDFscripts - backup1.js new file mode 100644 index 0000000..ac16de1 --- /dev/null +++ b/public/PDF/libPDFscripts - backup1.js @@ -0,0 +1,110 @@ +// JavaScript Document + +console.log("LIBRARY: libPDFscripts.js") +//Use this function as a start - to pass the path, then datasource to update with pdfbytes, then download -- Maybe combine into one file? +//wich is more efficient? + +let popover = new bootstrap.Popover(popoverTrigger, { + content: "This dropdown is disabled.", + placement: "top", + trigger: "manual" +}); + +//filepath = /PDF/XXX.pdf, dataSource = datastore.var +// *** Use of 'rest paramater' syntax to add extra datasources at the end. +//return array of datasources i.e [datastore,data_view1] and pass that array to updatePdfFields +//Consider default values = filepath = '/PDF/masterTemplate.pdf' +async function toBase64(filePath, ...dataSources) { + console.log(filePath); + const pdfBytes = await fetch(filePath).then((res) => res.arrayBuffer()); + + // console.log(dataSources); // should be only 2, + updatePdfFields(pdfBytes, dataSources); + + //return pdfBytes +} + +async function updatePdfFields(pdfBytes, dataSources) { + // Load the PDF document + console.log("pdfBytes.size =", pdfBytes.byteLength); + console.log(dataSources); + const pdfDoc = await PDFLib.PDFDocument.load(pdfBytes); + const form = pdfDoc.getForm(); + + // Loop over each data source passed to the function + dataSources.forEach((dataSource) => { + // Loop over each record in the current data source + dataSource.forEach((record) => { + // Loop over each key-value pair in the record + Object.entries(record).forEach(([fieldName, fieldValue]) => { + console.log(fieldName, " => ", fieldValue); + + // Find the form field in the PDF by name + /** !!! BLOCK 1 Start + const formField = form.getTextField(fieldName); + // If the field exists in the PDF, update it with the value + if (formField) { + formField.setText(String(fieldValue)); + } + **/ // BLOCK 1 END + }); + }); + }); + + // Serialize the PDF back to bytes + const updatedPdfBytes = await pdfDoc.save(); + + // Trigger download of the updated PDF + downloadPdf(updatedPdfBytes, "UpdatedDocument.pdf"); +} + +// Function to trigger download of the updated PDF +function downloadPdf(pdfBytes, fileName) { + // Create a Blob from the PDF bytes + const blob = new Blob([pdfBytes], { type: "application/pdf" }); + const url = URL.createObjectURL(blob); + + // Create a link element for the download + const link = document.createElement("a"); + link.href = url; + link.download = fileName; + + // Append the link to the document, trigger a click, and remove it afterward + document.body.appendChild(link); + link.click(); + document.body.removeChild(link); + + // Release the object URL to free up memory + URL.revokeObjectURL(url); +} + +// Usage Example +async function main() { + // Load your PDF as bytes (e.g., using fetch) + const pdfBytes = await fetch("path/to/your.pdf").then((res) => + res.arrayBuffer() + ); + + // Sample data sources from Wappler + const dataSource1 = [{ fieldName1: "Value1", fieldName2: "Value2" }]; + const dataSource2 = [ + { fieldName3: "AnotherValue1", fieldName4: "AnotherValue2" }, + ]; + + // Call the function with multiple data sources + await updatePdfFields(pdfBytes, dataSource1, dataSource2); +} + +function generateRandomFilename() { + const characters = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + let filename = ""; + for (let i = 0; i < 8; i++) { + const randomIndex = Math.floor(Math.random() * characters.length); + filename += characters[randomIndex]; + } + console.log("Random Filename: ", filename); + return filename; +} + + diff --git a/public/dmxAppConnect/config.js b/public/dmxAppConnect/config.js index 4632941..fb60b8b 100644 --- a/public/dmxAppConnect/config.js +++ b/public/dmxAppConnect/config.js @@ -40,6 +40,27 @@ dmx.config({ } ], "local": {} + }, + "data_view2": { + "meta": [ + { + "name": "$id", + "type": "number" + }, + { + "type": "text", + "name": "numSection" + }, + { + "type": "text", + "name": "pointValue" + }, + { + "type": "text", + "name": "description" + } + ], + "outputType": "array" } } }); diff --git a/public/js/libProcessScript.js b/public/js/libProcessScript.js index 4cb01c4..fc33781 100644 --- a/public/js/libProcessScript.js +++ b/public/js/libProcessScript.js @@ -49,101 +49,83 @@ async function fetchPdfBytes(filePath) { } // Update form fields in the PDF + + + async function updatePdfFields( pdfBytes, - dataSources + dataSources, + //skipMissingFields = false ) { console.log("Datasources Count:", dataSources.length) console.log(JSON.stringify(dataSources, null, 2)); - try { - const pdfDoc = await PDFLib.PDFDocument.load(pdfBytes); - const form = pdfDoc.getForm(); - dataSources.forEach((dataSource, dsIndex) => { - console.log(`Processing dataSource[${dsIndex}]:`, dataSource); - if (!Array.isArray(dataSource)) { - console.warn(`dataSource[${dsIndex}] is not an array. Skipping.`); + // try { + const pdfDoc = await PDFLib.PDFDocument.load(pdfBytes); + const form = pdfDoc.getForm(); + + + dataSources.forEach((dataSource, dsIndex) => { + console.log(`Processing dataSource[${dsIndex}]:`, dataSource); + + if (!Array.isArray(dataSource)) { + console.warn(`dataSource[${dsIndex}] is not an array. Skipping.`); + return; + } + let ii = 0 + dataSource.forEach((record, recIndex) => { + console.log(`Processing record[${recIndex}]:`, record); + + if (!record || typeof record !== 'object') { + console.warn(`record[${recIndex}] is undefined or not an object. Skipping.`); return; } - dataSource.forEach((record, recIndex) => { - console.log(`Processing record[${recIndex}]:`, record); - - if (!record || typeof record !== 'object') { - console.warn(`record[${recIndex}] is undefined or not an object. Skipping.`); - return; - } - - // Extract numSection and pointValue dynamically or other 'key' components. - const { numSection } = record; - const pointValue = record.pointValue || record.Points; // Fallback to Points if pointValue is missing + // Extract numSection and pointValue dynamically or other 'key' components. + const { numSection } = record; + const pointValue = record.pointValue || record.Points; // Fallback to Points if pointValue is missing - if (numSection && pointValue) { - console.log(`Updating PDF field: ${numSection} with value: ${pointValue}`); + if (numSection && pointValue) { + console.log(`Updating PDF field: ${numSection} with value: ${pointValue}`); - try { - // Use numSection as the dynamic field name - const formField = form.getTextField(numSection); + try { + // Use numSection as the dynamic field name + const formField = form.getTextField(numSection); - if (formField) { - formField.setText(String(pointValue)); - console.log(`Field "${numSection}" updated with value "${pointValue}".`); - } else { - console.warn(`Field "${numSection}" not found in PDF.`); - } - } catch (err) { - console.error(`Error updating field "${numSection}":`, err); + if (formField) { + ii++ + formField.setText(String(pointValue)); + console.log(`Field "${numSection}" updated with value "${pointValue}".`); + } else { + console.warn(`Field "${numSection}" not found in PDF.`); } - } else { - console.warn(`Missing numSection or pointValue in record[${recIndex}].`); + } catch (err) { + console.error(`Error updating field "${numSection}":`, err); } - }); + } else { + console.warn(`Missing numSection or pointValue in record[${recIndex}].`); + } }); + console.log(`${ii} Form field updates complete.`); + }); - /** - - dataSources.forEach((dataSource) => { - //console.log("Current Datasource", dataSource) - dataSource.forEach((record) => { - console.log(record) - //console.log("Working in: ", record) - Object.entries(record).forEach(([fieldName, fieldValue]) => { - dd(`Field: ${fieldName} Value: ${fieldValue}`) //debugger - try { - console.log(`----------------> form ${fieldName}`) - const formField = form.getTextField(fieldName); - if (formField) { - // console.info(`Field "${fieldName}" OK`); - dd(`${fieldName} --> ${fieldValue} OK`); - formField.setText(String(fieldValue)); - } else if (!skipMissingFields) { - //dd(`Skipping Field: ${fieldName} NOT FOUND`) - console.warn(`Field "${fieldName}" not found.`); - } - } catch (err) { - console.error(`Error updating field "${fieldName}":`); //console.error(`Error updating field "${fieldName}":`, err); - } - }); - }); - }); - */ - // console.log(JSON.stringify(dataSources, null, 2)); + return await pdfDoc.save(); /// MOVE TO the Main PROCESS and DOWNLOD PDF function or as last call. (need await??) Maybe make it an actual 'ready' to download button. + - return await pdfDoc.save(); - } catch (err) { - console.error("Error updating PDF fields:", err); - throw err; - } } + + + // Function to trigger download of the updated PDF function downloadPdf(pdfBytes, fileName) { + const blob = new Blob([pdfBytes], { type: "application/pdf" }); const url = URL.createObjectURL(blob); @@ -151,7 +133,7 @@ function downloadPdf(pdfBytes, fileName) { href: url, download: fileName, }); - + console.log("DOWNLOAD Filled PDF") link.click(); URL.revokeObjectURL(url); // Clean up the URL immediately } @@ -164,7 +146,7 @@ function generateRandomFilename(length = 8, prefix = "", suffix = "") { ); const filename = `${prefix}${array.join("")}${suffix}`; - dd("Random Filename:", filename); + //dd("Random Filename:", filename); return filename; } diff --git a/public/listFields.html b/public/listFields.html index 887740e..b5c5240 100644 --- a/public/listFields.html +++ b/public/listFields.html @@ -1,10 +1,43 @@ - - + + - -Untitled Document - + + + + PDF Form Fields + +

Hardcoded PDF Form Fields

+ + + diff --git a/views/index0.ejs b/views/index0.ejs index fd50cf7..a328243 100644 --- a/views/index0.ejs +++ b/views/index0.ejs @@ -10,7 +10,7 @@ columns: [ {table: "da", column: "db_signatureData"} ], - table: {name: "da"}, + table: {name: "db_training", alias: "da"}, primary: "_id", joins: [], wheres: { diff --git a/views/observationPage.ejs b/views/observationPage.ejs index 1472d4d..7b1e3e4 100644 --- a/views/observationPage.ejs +++ b/views/observationPage.ejs @@ -1,4 +1,5 @@ + @@ -309,7 +310,12 @@ -
+
+
+
+
+
+
@@ -360,7 +366,7 @@ -