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
dataSource.forEach((record, recIndex) => {
if (ii == 0) {
console.log(JSON.stringify(record, null, 2))
}
console.log(`Processing record[${recIndex}]:`, record);
if (!record || typeof record !== 'object') {
@ -85,17 +88,26 @@ async function updatePdfFields(
}
// 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) {
console.log(`Updating PDF field: ${numSection} with value: ${pointValue}`);
try {
// 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) {
ii++
formField.setText(String(pointValue));
@ -111,6 +123,7 @@ async function updatePdfFields(
}
});
console.log(`${ii} Form field updates complete.`);
ii = 0;
});

View File

@ -1,5 +1,6 @@
<!DOCTYPE html>
<html lang="en">
<head>
<base href="/">
<meta charset="UTF-8">
@ -7,12 +8,12 @@
<title>PDF Form Fields</title>
<script src="https://unpkg.com/pdf-lib/dist/pdf-lib.js"></script>
</head>
<body>
<h1>Hardcoded PDF Form Fields</h1>
<ul id="field-list"></ul>
<script>
const filePath = "/PDF/Template-EDV-ERT_v4.1.pdf"
const pdfBytes = await fetchPdfBytes(filePath);
@ -40,4 +41,5 @@
.catch(error => console.error('Error loading PDF:', error));
</script>
</body>
</html>