Forum Discussion
JasonHalterman
5 months agoCommunity Member
Storyline / Javascript Help
Hello!
I am trying (and failing miserably and consistently!) to have Storyline create a downloadable PDF form filled by user input. I've searched through previous discussions, used some of that co...
Nedim
5 months agoCommunity Member
You had an 'await' statement inside fillForm() but it was not declared as async. This code should work if you also fix the variable name (change it to PPDueDate in the variables panel) to avoid the "Error filling form."
// Retrieve Player Variables
var player = GetPlayer();
var learnerRCGoal = player.GetVar("RCGoal");
var learnerRCDueDate = player.GetVar("RCDueDate");
var learnerRCActionPlan = player.GetVar("RCActionPlan");
var learnerPPGoal = player.GetVar("PPGoal");
var learnerPPDueDate = player.GetVar("PPDueDate");
var learnerPPActionPlan = player.GetVar("PPActionPlan");
var learnerSMSBGoal = player.GetVar("SMSBGoal");
var learnerSMSBDue = player.GetVar("SMSBDue");
var learnerSMSBActionPlan = player.GetVar("SMSBActionPlan");
var learnerRVGoal = player.GetVar("RVGoal");
var learnerRVDate = player.GetVar("RVDate");
var learnerRVActionPlan = player.GetVar("RVActionPlan");
// Construct PDF when player variables are retrieved
ConstructPdf();
function ConstructPdf() {
loadPdfLib(); // Load PDF-lib module and fill form
}
function loadPdfLib() {
// Load pdf-lib library script dynamically
var pdfLibScript = document.createElement('script');
pdfLibScript.src = 'https://cdnjs.cloudflare.com/ajax/libs/pdf-lib/1.16.0/pdf-lib.min.js';
pdfLibScript.onload = function() {
fillForm();
};
pdfLibScript.onerror = function() {
console.error('Failed to load pdf-lib script.');
};
document.head.appendChild(pdfLibScript);
}
async function fillForm() {
// Read pdf form from URL
const formUrl = 'https://sigep.org/wp-content/uploads/PhiGoals.pdf';
try {
const formPdfBytes = await fetch(formUrl).then(res => res.arrayBuffer());
const pdfDoc = await PDFLib.PDFDocument.load(formPdfBytes);
const form = pdfDoc.getForm();
// Get field objects from the form
const RCGoalField = form.getTextField('RCGoal');
const RCDueDateField = form.getTextField('RCDueDate');
const RCActionPlanField = form.getTextField('RCActionPlan');
const PPGoalField = form.getTextField('PPGoal');
const PPDueDateField = form.getTextField('PPDueDate');
const PPActionPlanField = form.getTextField('PPActionPlan');
const SMSBGoalField = form.getTextField('SMSBGoal');
const SMSBDueField = form.getTextField('SMSBDue');
const SMSBActionPlanField = form.getTextField('SMSBActionPlan');
const RVGoalField = form.getTextField('RVGoal');
const RVDateField = form.getTextField('RVDate');
const RVActionPlanField = form.getTextField('RVActionPlan');
// Set text values in the form fields
RCGoalField.setText(learnerRCGoal);
RCDueDateField.setText(learnerRCDueDate);
RCActionPlanField.setText(learnerRCActionPlan);
PPGoalField.setText(learnerPPGoal);
PPDueDateField.setText(learnerPPDueDate);
PPActionPlanField.setText(learnerPPActionPlan);
SMSBGoalField.setText(learnerSMSBGoal);
SMSBDueField.setText(learnerSMSBDue);
SMSBActionPlanField.setText(learnerSMSBActionPlan);
RVGoalField.setText(learnerRVGoal);
RVDateField.setText(learnerRVDate);
RVActionPlanField.setText(learnerRVActionPlan);
//Save form
const pdfBytes = await pdfDoc.save();
//Download the form
const { default: saveAs } = await import('https://unpkg.com/tiny-save-as/dist/tiny-save-as.esm.js');
const blob = new Blob([pdfBytes], { type: 'application/octet-stream' });
saveAs(blob, 'PhiGoals.pdf');
} catch (error) {
console.error('Error filling form:', error);
}
}