async function updatePdfFields(pdfBytes, dataSource) { // Load the PDF document const pdfDoc = await PDFDocument.load(pdfBytes); const form = pdfDoc.getForm(); // Loop over each record in the Wappler data source dataSource.forEach((record) => { // Loop over each key-value pair in the record Object.entries(record).forEach(([fieldName, fieldValue]) => { // Find the form field in the PDF by name const formField = form.getTextField(fieldName); // If the field exists in the PDF, update it with the value if (formField) { formField.setText(String(fieldValue)); } }); }); // Serialize the PDF back to bytes and return const updatedPdfBytes = await pdfDoc.save(); return updatedPdfBytes; } // 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 source from Wappler const dataSource = [ { fieldName1: "Value1", fieldName2: "Value2" }, { fieldName1: "AnotherValue1", fieldName2: "AnotherValue2" }, ]; // Call the function to update the PDF fields const updatedPdfBytes = await updatePdfFields(pdfBytes, dataSource); // Do something with the updated PDF, e.g., download it or display it }