nrzmalik

Articulate RISE Certificate Generator

Sep 17th, 2025 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 7.10 KB | Source Code | 0 0
  1. // Storylline Certificate Generator
  2. //Author: N R Z Malik  
  3. //Website: https://eblog.nrzmalik.com
  4. //Version: 1.2 - Fixed text overflow with configurable course title
  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. // Course Title - Set your custom course title here, leave empty to use page title
  16. var CourseTitle = ""; // Example: "Advanced JavaScript Programming Course"
  17.  
  18. // Logo configuration (optional)
  19. // Paste your base64 logo size (180*180). Generate it from https://www.base64-image.de/ code here, or leave empty string to disable logo
  20. var LOGO_BASE64 = ""; // Example: "data:image/png;base64,iVBORw0KGgoAAAANSUhEU..."
  21.  
  22. // Logo settings
  23. var LOGO_CONFIG = {
  24.     width: 40,           // Reduced size to fit better
  25.     height: 40,          // Reduced size to fit better
  26.     rightMargin: 0,     // Distance from right edge
  27.     bottomMargin: 0     // Distance from bottom (aligned with date area)
  28. };
  29.  
  30. // ========== CERTIFICATE GENERATION ==========
  31. var confettiScript = document.createElement('script');
  32. confettiScript.src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js';
  33. confettiScript.async = true;
  34. confettiScript.onload = function() {
  35.     generateCertificate();
  36. };
  37. document.head.appendChild(confettiScript);
  38.  
  39. function generateCertificate() {
  40.     var date = new Date();
  41.     var dd = String(date.getDate()).padStart(2, '0');
  42.     var mm = String(date.getMonth() + 1).padStart(2, '0');
  43.     var yyyy = date.getFullYear();
  44.     date = mm + '/' + dd + '/' + yyyy;
  45.  
  46.     /*
  47.     //For Storyline builtin variables, use the following code:
  48.  
  49.     var player=GetPlayer();
  50.     var name=player.GetPlayer("Name");
  51.  
  52.     //And Remove or Comment the next following line:
  53.     */
  54.  
  55.     var name = prompt("Please enter your name:", "");
  56.     if (!name) {
  57.          name = prompt("Please enter your name:", "");
  58.     }
  59.    
  60.     var doc = new jsPDF({
  61.         orientation: 'landscape'
  62.     });
  63.  
  64.     var pageWidth = doc.internal.pageSize.getWidth();
  65.     var pageHeight = doc.internal.pageSize.getHeight();
  66.  
  67.     // Main border
  68.     doc.setLineWidth(2);
  69.     doc.setDrawColor(...COLORS.border);
  70.     doc.rect(10, 10, pageWidth - 20, pageHeight - 20);
  71.  
  72.     // Header background
  73.     doc.setFillColor(...COLORS.primary);
  74.     doc.rect(10, 10, pageWidth - 20, 40, 'F');
  75.  
  76.     // Header decorative lines
  77.     doc.setDrawColor(...COLORS.white);
  78.     doc.setLineWidth(1);
  79.     doc.line(10, 40, pageWidth - 10, 40);
  80.     doc.line(30, 10, 30, 40);
  81.     doc.line(pageWidth - 30, 10, pageWidth - 30, 40);
  82.  
  83.     // Header title
  84.     doc.setTextColor(...COLORS.white);
  85.     doc.setFontSize(30);
  86.     doc.setFont('Helvetica', 'bold');
  87.     var title0 = "Certificate of Completion";
  88.  
  89.     var nameWidth = doc.getStringUnitWidth(title0) * doc.internal.getFontSize() / doc.internal.scaleFactor;
  90.     var xPos = (pageWidth - nameWidth) / 2;
  91.     doc.text(title0, xPos, 30, null, null, 'left');
  92.  
  93.     // Certificate content
  94.     doc.setTextColor(...COLORS.black);
  95.  
  96.     doc.setFontSize(28);
  97.     doc.setFont('Helvetica','normal');
  98.     var title2 = "THIS IS TO CERTIFY THAT";
  99.     var nameWidth = doc.getStringUnitWidth(title2) * doc.internal.getFontSize() / doc.internal.scaleFactor;
  100.     var xPos = (pageWidth - nameWidth) / 2;
  101.     doc.text(title2, xPos, 70, null, null, 'left');
  102.  
  103.     // Recipient name
  104.     doc.setFontSize(50);
  105.     doc.setFont('Helvetica','bold');
  106.     var nameWidth = doc.getStringUnitWidth(name) * doc.internal.getFontSize() / doc.internal.scaleFactor;
  107.     var xPos = (pageWidth - nameWidth) / 2;
  108.     doc.text(name, xPos, 100, null, null, 'left');
  109.  
  110.     // Completion text - moved up to create more space
  111.     doc.setFont('Helvetica','normal');
  112.     doc.setFontSize(28);
  113.     var title3 = "You have successfully completed:";
  114.     var nameWidth = doc.getStringUnitWidth(title3) * doc.internal.getFontSize() / doc.internal.scaleFactor;
  115.     var xPos = (pageWidth - nameWidth) / 2;
  116.     doc.text(title3, xPos, 125, null, null, 'left'); // Changed from 140 to 125
  117.  
  118.     // Function to wrap text properly
  119.     function wrapText(text, maxWidth) {
  120.         var words = text.split(' ');
  121.         var lines = [];
  122.         var currentLine = '';
  123.        
  124.         for (var i = 0; i < words.length; i++) {
  125.             var testLine = currentLine + words[i] + ' ';
  126.             var testWidth = doc.getStringUnitWidth(testLine) * doc.internal.getFontSize() / doc.internal.scaleFactor;
  127.            
  128.             if (testWidth > maxWidth && currentLine !== '') {
  129.                 lines.push(currentLine.trim());
  130.                 currentLine = words[i] + ' ';
  131.             } else {
  132.                 currentLine = testLine;
  133.             }
  134.         }
  135.         lines.push(currentLine.trim());
  136.         return lines;
  137.     }
  138.  
  139.     // Determine course title - use CourseTitle variable if set, otherwise use page title
  140.     var pageTitle = "";
  141.     if (CourseTitle && CourseTitle.trim() !== "") {
  142.         pageTitle = CourseTitle.trim();
  143.     } else {
  144.         pageTitle = document.title;
  145.     }
  146.  
  147.     // Course title with proper text wrapping - moved up
  148.     doc.setFontSize(24);
  149.     doc.setFont('Helvetica','bold');
  150.  
  151.     // Wrap and display the course title
  152.     var maxTitleWidth = pageWidth - 40; // Leave 20px margin on each side
  153.     var titleLines = wrapText(pageTitle, maxTitleWidth);
  154.     var startY = 145; // Moved up from 160 to 145
  155.     var lineHeight = 10; // Proper line height for readability
  156.  
  157.     for (var i = 0; i < titleLines.length; i++) {
  158.         var line = titleLines[i];
  159.         var lineWidth = doc.getStringUnitWidth(line) * doc.internal.getFontSize() / doc.internal.scaleFactor;
  160.         var xPos = (pageWidth - lineWidth) / 2;
  161.         doc.text(line, xPos, startY + (i * lineHeight), null, null, 'left');
  162.     }
  163.  
  164.     // Calculate dynamic positioning for date and logo, ensuring minimum distance from title
  165.     var titleEndY = startY + ((titleLines.length - 1) * lineHeight);
  166.     var dateY = Math.max(190, titleEndY + 25); // Minimum 25px gap from last title line
  167.  
  168.     // Date section
  169.     doc.setDrawColor(...COLORS.black);
  170.     doc.setLineWidth(0.5);
  171.     doc.line(18, dateY, 43, dateY);
  172.  
  173.     doc.setFontSize(14);
  174.     doc.setTextColor(...COLORS.black);
  175.     doc.text(date, 18, dateY - 2, null, null, 'left');
  176.     doc.text("Date", 25, dateY + 5, null, null, 'left');
  177.  
  178.     // Add logo if available (positioned relative to date)
  179.     if (LOGO_BASE64 && LOGO_BASE64.trim() !== "") {
  180.         try {
  181.             var logoX = pageWidth - 18 - LOGO_CONFIG.width;
  182.             var logoY = dateY - LOGO_CONFIG.height - 2;
  183.             doc.addImage(LOGO_BASE64, 'PNG', logoX, logoY, LOGO_CONFIG.width, LOGO_CONFIG.height);
  184.         } catch (error) {
  185.             console.warn("Could not add logo to certificate:", error);
  186.         }
  187.     }
  188.  
  189.     // Save the certificate
  190.     doc.save(name + " Certificate.pdf");
  191. }
Add Comment
Please, Sign In to add comment