BeansproutMo921
9 months agoCommunity Member
Javascript Certificate fail..!
Hello everyone and a very happy New Year!
I'm useless at code and just grab chunks here and there and basically use trial and error until something works the way I intended... but I'm stumped on this one!
I'm trying to make a button that you click to download a certificate that contains the date at time of clicking (this part is working fine) and that also adds in the typed variable as the Learners name displayed on the certificate.
I have everything set up but when you click the Download certificate button nothing happens...
Please can someone much cleverer than I am take a little look at my code to see what I'm doing wrong?
Thank you! đŸ˜€
Variables set up in my SL course:
- uName (typed variable for the user to add their own name)
- todaysDate (todays date)
My code;
//Retrieve Player Variables
var player = GetPlayer();
learnerName = player.GetVar("uName");
learnerDate = player.GetVar("todaysDate");
Constructpdf();
async function Constructpdf() {
//Dynamically load pdf-lib JavaScript module
await loadMods();
//Call the fillForm asynch function
await fillForm();
}
async function loadMods() {
await import("https://unpkg.com/pdf-lib/dist/pdf-lib.js");
}
async function fillForm() {
//Read pdf form
const { PDFDocument } = PDFLib;
const formUrl = 'https:urlinkhere.pdf';
const formPdfBytes = await fetch(formUrl).then (res => res.arrayBuffer());
const pdfDoc = await PDFDocument.load(formPdfBytes);
const form = pdfDoc.getForm();
//Get field names
const nameField = form.getTextField('uName');
const dateField = form.getTextField('todaysDate');
//Fill in form
var filename = learnerName + ".pdf"
//Flatten form
form.flatten();
//Save form
const pdfBytes = await pdfDoc.save();
//Download the form
await import('https://unpkg.com/tiny-save-as/dist/tiny-save-as.esm.js')
.then(({default: saveAs}) => {
const blob = new Blob([pdfBytes], {type: 'application/octet-stream'});
saveAs(blob, filename);
})
.catch((err) => {
console.log(err);
});
}