WIP: fixing the loop for recognizing a datasource. Line 90, updatePdfFields*

This commit is contained in:
Wappler 2025-01-02 02:46:07 -06:00
parent f7b44e14a5
commit f209ad162c
2 changed files with 31 additions and 16 deletions

View File

@ -77,6 +77,9 @@ async function updatePdfFields(
} }
let ii = 0 let ii = 0
dataSource.forEach((record, recIndex) => { dataSource.forEach((record, recIndex) => {
if (ii == 0) {
console.log(JSON.stringify(record, null, 2))
}
console.log(`Processing record[${recIndex}]:`, record); console.log(`Processing record[${recIndex}]:`, record);
if (!record || typeof record !== 'object') { if (!record || typeof record !== 'object') {
@ -85,17 +88,26 @@ async function updatePdfFields(
} }
// Extract numSection and pointValue dynamically or other 'key' components. // 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 * Working area - to wrap assignment of numSection, pointValue or a Key-Value for other datasources.
* so far this is working and success on downloading the PDF prefilled on the score.
*/
const recordToKeyArray = Object.keys(record).includes("$id")
if (recordToKeyArray) { console.log("$ID EXIST") }
const { numSection, pointValue } = record;
const rKey = numSection
const rValue = pointValue
//const pointValue = record.pointValue || record.Points; // Fallback to Points if pointValue is missing
console.log(`[numSection 2 key ==>] ${rKey},${rValue}`)
if (numSection && pointValue) { if (numSection && pointValue) {
console.log(`Updating PDF field: ${numSection} with value: ${pointValue}`); console.log(`Updating PDF field: ${numSection} with value: ${pointValue}`);
try { try {
// Use numSection as the dynamic field name // Use numSection as the dynamic field name
const formField = form.getTextField(numSection); //const formField = form.getTextField(numSection);
const formField = form.getFieldMaybe(String(numSection)); // if cannot find, then just returns 'undefined' rather than throw err.
if (formField) { if (formField) {
ii++ ii++
formField.setText(String(pointValue)); formField.setText(String(pointValue));
@ -111,6 +123,7 @@ async function updatePdfFields(
} }
}); });
console.log(`${ii} Form field updates complete.`); console.log(`${ii} Form field updates complete.`);
ii = 0;
}); });

View File

@ -1,19 +1,20 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<base href="/"> <base href="/">
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF Form Fields</title> <title>PDF Form Fields</title>
<script src="https://unpkg.com/pdf-lib/dist/pdf-lib.js"></script> <script src="https://unpkg.com/pdf-lib/dist/pdf-lib.js"></script>
</head> </head>
<body> <body>
<h1>Hardcoded PDF Form Fields</h1> <h1>Hardcoded PDF Form Fields</h1>
<ul id="field-list"></ul> <ul id="field-list"></ul>
<script> <script>
const filePath = "/PDF/Template-EDV-ERT_v4.1.pdf"
const filePath = "/PDF/Template-EDV-ERT_v4.1.pdf"
const pdfBytes = await fetchPdfBytes(filePath); const pdfBytes = await fetchPdfBytes(filePath);
// Function to load a PDF and list all form fields // Function to load a PDF and list all form fields
@ -38,6 +39,7 @@
.then(response => response.arrayBuffer()) .then(response => response.arrayBuffer())
.then(pdfBytes => listPdfFields(pdfBytes)) .then(pdfBytes => listPdfFields(pdfBytes))
.catch(error => console.error('Error loading PDF:', error)); .catch(error => console.error('Error loading PDF:', error));
</script> </script>
</body> </body>
</html> </html>