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 code, had ChatGPT help me, and I still can't get this to work. When I click the button (that is the trigger to execute javascript), nothing happens. I've tried replacing with a simple line, and the same issue occurs. I've tried uploading to my LMS as a SCORM and trying Rise360 with the storyline file as an interactive piece.
Here's the code:
// 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();
};
document.head.appendChild(pdfLibScript);
}
function fillForm() {
// Read pdf form from URL
const formUrl = 'https://drive.google.com/file/d/16GAP-hIeV6_4DL61jsNm7WQZ0xk8jRYh/view?usp=drive_link';
fetch(formUrl)
.then(res => res.arrayBuffer())
.then(formPdfBytes => {
PDFLib.PDFDocument.load(formPdfBytes).then(pdfDoc => {
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);
var filename = "PhiGoals.pdf"; // Assuming filename is fixed
// Save the filled form
pdfDoc.save().then(function(pdfBytes) {
// Convert bytes to blob
var blob = new Blob([pdfBytes], { type: 'application/pdf' });
// Use Storyline's built-in function to trigger file download
var player = GetPlayer();
player.DownloadFile(blob, filename);
}).catch(function(error) {
console.error('Failed to save PDF:', error);
});
}).catch(function(error) {
console.error('Failed to load PDF document:', error);
});
}).catch(function(error) {
console.error('Failed to fetch PDF form:', error);
});
}
I also have the storyline file and the PDF attached. I appreciate anyone's help.. this is not my area of expertise.