nrzmalik

Articulate Storyline PDF Certificate JavaScript

Jun 20th, 2025
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 5.38 KB | Source Code | 0 0
  1. // Storylline Certificate Generator
  2. //Author: N R Z Malik  
  3. //Website: https://eblog.nrzmalik.com
  4. //Version: 1.1
  5.  
  6. // ========== CONFIGURATION VARIABLES ==========
  7. // Colors - Change these values to customize the certificate appearance
  8. var COLORS = {
  9.     primary: [27, 20, 100],        // Main theme color (red)
  10.     white: [255, 255, 255],      // White for text and lines
  11.     black: [0, 0, 0],            // Black for text and lines
  12.     border: [27, 20, 100]        // Border color (same as primary)
  13. };
  14.  
  15. // Logo configuration (optional)
  16. // Paste your base64 logo size (180*180). Generate it from https://www.base64-image.de/ code here, or leave empty string to disable logo
  17. var LOGO_BASE64 = ""; // Example: "data:image/png;base64,iVBORw0KGgoAAAANSUhEU..."
  18.  
  19. // Logo settings
  20. var LOGO_CONFIG = {
  21.     width: 40,           // Reduced size to fit better
  22.     height: 40,          // Reduced size to fit better
  23.     rightMargin: 0,     // Distance from right edge
  24.     bottomMargin: 0     // Distance from bottom (aligned with date area)
  25. };
  26.  
  27. // ========== CERTIFICATE GENERATION ==========
  28. var confettiScript = document.createElement('script');
  29. confettiScript.src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js';
  30. confettiScript.async = true;
  31. confettiScript.onload = function() {
  32.     generateCertificate();
  33. };
  34. document.head.appendChild(confettiScript);
  35.  
  36. function generateCertificate() {
  37.     var date = new Date();
  38.     var dd = String(date.getDate()).padStart(2, '0');
  39.     var mm = String(date.getMonth() + 1).padStart(2, '0');
  40.     var yyyy = date.getFullYear();
  41.     date = mm + '/' + dd + '/' + yyyy;
  42.  
  43.     /*
  44.     //For Storyline builtin variables, use the following code:
  45.  
  46.     var player=GetPlayer();
  47.     var name=player.GetPlayer("Name");
  48.  
  49.     //And Remove or Comment the next following line:
  50.     */
  51.  
  52.     var name = prompt("Please enter your name:", "");
  53.     if (!name) {
  54.          name = prompt("Please enter your name:", "");
  55.     }
  56.    
  57.     var doc = new jsPDF({
  58.         orientation: 'landscape'
  59.     });
  60.  
  61.     var pageWidth = doc.internal.pageSize.getWidth();
  62.     var pageHeight = doc.internal.pageSize.getHeight();
  63.  
  64.     // Main border
  65.     doc.setLineWidth(2);
  66.     doc.setDrawColor(...COLORS.border);
  67.     doc.rect(10, 10, pageWidth - 20, pageHeight - 20);
  68.  
  69.     // Header background
  70.     doc.setFillColor(...COLORS.primary);
  71.     doc.rect(10, 10, pageWidth - 20, 40, 'F');
  72.  
  73.     // Header decorative lines
  74.     doc.setDrawColor(...COLORS.white);
  75.     doc.setLineWidth(1);
  76.     doc.line(10, 40, pageWidth - 10, 40);
  77.     doc.line(30, 10, 30, 40);
  78.     doc.line(pageWidth - 30, 10, pageWidth - 30, 40);
  79.  
  80.     // Header title
  81.     doc.setTextColor(...COLORS.white);
  82.     doc.setFontSize(30);
  83.     doc.setFont('Helvetica', 'bold');
  84.     var title0 = "Certificate of Completion";
  85.  
  86.     var nameWidth = doc.getStringUnitWidth(title0) * doc.internal.getFontSize() / doc.internal.scaleFactor;
  87.     var xPos = (pageWidth - nameWidth) / 2;
  88.     doc.text(title0, xPos, 30, null, null, 'left');
  89.  
  90.     // Certificate content
  91.     doc.setTextColor(...COLORS.black);
  92.  
  93.     doc.setFontSize(28);
  94.     doc.setFont('Helvetica','normal');
  95.     var title2 = "THIS IS TO CERTIFY THAT";
  96.     var nameWidth = doc.getStringUnitWidth(title2) * doc.internal.getFontSize() / doc.internal.scaleFactor;
  97.     var xPos = (pageWidth - nameWidth) / 2;
  98.     doc.text(title2, xPos, 70, null, null, 'left');
  99.  
  100.     // Recipient name
  101.     doc.setFontSize(50);
  102.     doc.setFont('Helvetica','bold');
  103.     var nameWidth = doc.getStringUnitWidth(name) * doc.internal.getFontSize() / doc.internal.scaleFactor;
  104.     var xPos = (pageWidth - nameWidth) / 2;
  105.     doc.text(name, xPos, 100, null, null, 'left');
  106.  
  107.     // Completion text
  108.     doc.setFont('Helvetica','normal');
  109.     doc.setFontSize(28);
  110.     var title3 = "You have successfully completed:";
  111.     var nameWidth = doc.getStringUnitWidth(title3) * doc.internal.getFontSize() / doc.internal.scaleFactor;
  112.     var xPos = (pageWidth - nameWidth) / 2;
  113.     doc.text(title3, xPos, 140, null, null, 'left');
  114.  
  115.     // Course title
  116.     var pageTitle = document.title;
  117.     doc.setFontSize(24);
  118.     doc.setFont('Helvetica','bold');
  119.     var nameWidth = doc.getStringUnitWidth(pageTitle) * doc.internal.getFontSize() / doc.internal.scaleFactor;
  120.     var xPos = (pageWidth - nameWidth) / 2;
  121.     doc.text(pageTitle, xPos, 160, null, null, 'left');
  122.  
  123.     // Date section
  124.     doc.setDrawColor(...COLORS.black);
  125.     doc.setLineWidth(0.5);
  126.     doc.line(18, 190, 43, 190);
  127.  
  128.     doc.setFontSize(14);
  129.     doc.setTextColor(...COLORS.black);
  130.     doc.text(date, 18, 188, null, null, 'left');
  131.     doc.text("Date", 25, 195, null, null, 'left');
  132.  
  133.     // Add logo if available (positioned at bottom-right, mirroring date position)
  134.     if (LOGO_BASE64 && LOGO_BASE64.trim() !== "") {
  135.         try {
  136.             // Position logo on right side, mirroring the date position on the left
  137.             var logoX = pageWidth - 18 - LOGO_CONFIG.width; // Same distance from right as date from left (18px)
  138.             var logoY = 188 - LOGO_CONFIG.height + 25; // Align with date Y position, adjusted for logo height
  139.             doc.addImage(LOGO_BASE64, 'PNG', logoX, 169, LOGO_CONFIG.width, LOGO_CONFIG.height);
  140.         } catch (error) {
  141.             console.warn("Could not add logo to certificate:", error);
  142.         }
  143.     }
  144.  
  145.     // Save the certificate
  146.     doc.save(name + " Certificate.pdf");
  147. }
  148.  
Add Comment
Please, Sign In to add comment