Forum Discussion
Storyline 360 - Generating a PDF Certificate using JavaScript
This example demonstrates how to use the pdfmake JavaScript library to generate PDF certificates in the browser using Storyline 360.
This is a followup to my original post found here. The only changes from the original are that this is now a Storyline 360 project and pdfmake has been updated to the latest version (v0.2.4 at time of posting).
The linked example provides a brief overview of how this works. I've tested this successfully in Internet Explorer, Chrome, Firefox, Chrome on Android and Safari on iOS.
The attached source files contains a basic .story file as well as a folder (certificate) that includes the required HTML and JavaScript that needs to be included within a web object within the project.
The generated PDF certificate is very basic at the moment, you'll need to refer to the pdfmake documentation to update the design.
Feel free to have a look and re-use if it's useful.
(21/03/2022 - pdfmake updated to v0.2.4 and example link replaced)
- JessikaPedersenCommunity Member
I wondered if anyone could give me direction on how to edit the "lorem ipsum" text on the certificate?
- RyanLowryCommunity Member
Hey Jessika. That text is part of the SVG background image used in the example. You'll find this included inline within certificate.js on line 45 as shown below. This isn't easily editable. Instead you'll probably want to specify your own background image.
background: [
{
svg: 'SVG_BACKGROUND_IMAGE_INCLUDED_HERE',
width: 841.89,
height: 595.28
}
],You can find more about including SVGs in the pdfmake docs - https://pdfmake.github.io/docs/0.1/document-definition-object/svgs/
Alternatively you could insert a jpeg of your own design - here: https://pdfmake.github.io/docs/0.1/document-definition-object/images/
- KateMackenzieCommunity Member
i am SO super delighted that i've managed to ALMOST create a certificate. HOWEVER, I can't for the life of me work out how to get the variables in.
I have spent 3 hours on it and i'm going nuts.also - not sure how everyone else gets their code in that fancy grey box......
- KateMackenzieCommunity Member
function createPDF() {
var D1 = '';
var D2 = '';
var player = parent.GetPlayer();
D1 = player.GetVar("firstname");
D2 = player.GetVar("lastname");
//var docDefinition = { content: 'This is an sample PDF printed with pdfMake for '+name+"!" };
var docDefinition = {
content: [
{
table: {
heights: [100,100,100,100],
widths: [500],
headerRows: 3,
body: [
['Certificate of completion'],
[HOW THE HELL DO I GET THE VARIABLES HERE?],
['completed the eSafety Early Years module'],
['We SAY and SHARE with technology'],
]
},
style: 'header',
alignment: 'center',
layout: 'noBorders'
},
- AixaRiveraCommunity Member
I really appreciate you sharing this valuable information.
- KateMackenzieCommunity Member
And next question - how do we get the download to have a name rather than file.pdf?
- KateMackenzieCommunity Member
so it needs the quote marks rahter than this
pdfMake.createPdf(docDefinition).download('Capability 5');
}- KateMackenzieCommunity Member
you are 100% correct!
THank you!
- PhilMayorSuper Hero
Matt I wish you hadn’t said that, I just built a CV builder using Storyline with a PDF output and now I am going to have too go and change it!
- DougDewanCommunity Member
Hi Ryan, not sure what we are doing wrong. We followed all the steps but still can't seem to make the course work to produce the certificate when published to Articulate Review, or the LMS sandbox we use. The only difference I can see from your example Story file and ours is on the "Success" layer of your last slide there is a "web object" but I'm not seeing in the steps how this Web Object is established in the Articulate Story file. Would you be able to provide clarity on how we can establish this?
Thank you in advance!
Doug
- RyanLowryCommunity Member
Hi Doug, the Source.zip file attached to the original post contains a folder certificate with the required files for the web object that you'll need to insert. In the example it is inserted on the success layer of the results slide but you can insert it anywhere you see fit.
Insert > Web Object > Select "certificate" folder- DougDewanCommunity Member
Hi Ryan, sorry to bother you again, I'm still having trouble getting this to work on our end. If I publish your course example to 360 Review or our Internal LMS it works fine, but the course I built doesn't. Of course my web address is different from yours but I'm also not clear on how your example is pulling the certificate from that address and incorporating it into your .Story file. Would the problem be with my Java script or the location of my file or something else. I'm at a loss.
Below is the JS we wrote:
var player = GetPlayer();
var date = new Date();
var day = String(date.getDate()).padStart(2, '0');
var month = String(date.getMonth() + 1).padStart(2, '0');
var year = date.getFullYear();
var issuedOn = month + '/' + day + '/' + year;
var expiresOn = month + '/' + day + '/' + (year + 3);
var uName = player.GetVar("uName");
var doc = new jspdf.jsPDF({
orientation: 'portrait'
});
var img = new Image;
img.onload = function () {
doc.addImage(this, 'png', 0, 0, 230, 305);
doc.setFontSize(10);
doc.setTextColor(77, 77, 79);
doc.setFont('Helvetica', 'normal');
doc.text(uName.trim(), 19, 31, null, null, 'left');
doc.text(issuedOn, 32, 43, null, null, 'left');
doc.text(expiresOn, 32, 50, null, null, 'left');
doc.save("Certificate.pdf");
};
img.crossOrigin = "";
img.src = "certificate.png";
- RyanLowryCommunity Member
Are you also able to share the files from the web object embedded on the success layer of the results slide?
- RyanLowryCommunity Member
Doug, looking at the .story file you've provided I think you are mixing two different methods of generating a PDF in Storyline. Were you following this tutorial by any chance? Rather than embedding a web object which contains the files required to generate a PDF you need to edit the published output and modify it to include the files required to generate the PDF. This is described towards the end of the tutorial under the heading "Publish and Modify the eLearning Output".
If you wish to follow the method outlined in this post you'd need to copy the JavaScript trigger as found in the .story file in Source.zip e.g.// Name of the certificate html file
var certFilename = 'certificate.html';
// HTMLCollection of elements of type iFrame
var iframeElements = document.getElementsByTagName("iframe");
// Iterate over the iFrameElements HTMLCollection
for (var i = 0; i < iframeElements.length; i++) {
/* If src of current iFrame element equals the filename set in variable
** certFilename call the generatePDF() function.
*/
var src = iframeElements[i].getAttribute('src');
if (src.indexOf(certFilename) != -1) {
iframeElements[i].contentWindow.generatePDF();
}
}- DougDewanCommunity Member
Hi Ryan, sorry I think you are correct. We are working with IT and believe I have misdirected them showing two methods.
Ideally we would like a solution that does not require modifying files each time so believe your solution is the best for that as it addressed it for each publishing with out having to add files after, is this correct?