Created
January 12, 2026 04:36
-
-
Save msrivastav13/3cdf1704262121385d58eb2c29c07d9e to your computer and use it in GitHub Desktop.
Demonstrates Blob.toPdf() with Visualforce PDF Rendering
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * CertificatePdfGenerator - Demonstrates Blob.toPdf() with Visualforce PDF Rendering | |
| * | |
| * Spring '26 Release Update: | |
| * Blob.toPdf() now uses the Visualforce PDF rendering service which provides: | |
| * - Expanded font support including multibyte characters (CJK - Chinese, Japanese, Korean) | |
| * - Default font changed from serif to sans-serif | |
| * - Consistent rendering across the Salesforce Platform | |
| * When: Enforced in Summer '26. Check Trust Status for your instance's upgrade date. | |
| * | |
| * @since API Version 66.0 | |
| */ | |
| public with sharing class CertificatePdfGenerator { | |
| /** | |
| * Generates a professional certificate PDF | |
| * | |
| * @param recipientName Name of the certificate recipient | |
| * @param courseName Name of the completed course | |
| * @param completionDate Date of completion | |
| * @param instructorName Name of the instructor | |
| * @return Blob The generated certificate PDF | |
| */ | |
| public static Blob generateCertificate( | |
| String recipientName, | |
| String courseName, | |
| Date completionDate, | |
| String instructorName | |
| ) { | |
| String htmlContent = buildCertificateHtml(recipientName, courseName, completionDate, instructorName); | |
| // The magic happens here - Blob.toPdf() now uses Visualforce rendering! | |
| return Blob.toPdf(htmlContent); | |
| } | |
| /** | |
| * Generates a certificate and saves it to Salesforce Files | |
| * | |
| * @param fileName Name for the PDF file (without extension) | |
| * @param recipientName Name of the certificate recipient | |
| * @param courseName Name of the completed course | |
| * @param completionDate Date of completion | |
| * @param instructorName Name of the instructor | |
| * @return Id The ContentVersion Id of the saved file | |
| */ | |
| public static Id generateAndSaveToFiles( | |
| String fileName, | |
| String recipientName, | |
| String courseName, | |
| Date completionDate, | |
| String instructorName | |
| ) { | |
| // Generate the PDF using the new Visualforce rendering service | |
| Blob pdfBlob = generateCertificate(recipientName, courseName, completionDate, instructorName); | |
| // Save to Salesforce Files (ContentVersion) | |
| ContentVersion cv = new ContentVersion(); | |
| cv.Title = fileName; | |
| cv.PathOnClient = fileName + '.pdf'; | |
| cv.VersionData = pdfBlob; | |
| cv.IsMajorVersion = true; | |
| insert cv; | |
| return cv.Id; | |
| } | |
| /** | |
| * Builds the HTML content for a professional certificate | |
| * Demonstrates CSS styling capabilities with the new rendering engine | |
| */ | |
| private static String buildCertificateHtml( | |
| String recipientName, | |
| String courseName, | |
| Date completionDate, | |
| String instructorName | |
| ) { | |
| return '<!DOCTYPE html>' + | |
| '<html>' + | |
| '<head>' + | |
| '<style>' + | |
| // Body styling with gradient background | |
| 'body { ' + | |
| ' font-family: "Georgia", serif; ' + | |
| ' margin: 0; ' + | |
| ' padding: 40px; ' + | |
| ' background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);' + | |
| '}' + | |
| // Certificate border with double gold line | |
| '.certificate-border {' + | |
| ' border: 8px double #c9a227;' + | |
| ' background: white;' + | |
| ' padding: 50px;' + | |
| ' text-align: center;' + | |
| '}' + | |
| // Header with letter spacing | |
| '.certificate-header {' + | |
| ' color: #c9a227;' + | |
| ' font-size: 14px;' + | |
| ' letter-spacing: 8px;' + | |
| ' text-transform: uppercase;' + | |
| ' margin-bottom: 20px;' + | |
| '}' + | |
| // Main title | |
| '.certificate-title {' + | |
| ' font-size: 42px;' + | |
| ' color: #333;' + | |
| ' margin-bottom: 30px;' + | |
| ' font-family: "Times New Roman", serif;' + | |
| '}' + | |
| // Descriptive text | |
| '.certificate-text {' + | |
| ' color: #666;' + | |
| ' font-size: 16px;' + | |
| ' margin-bottom: 15px;' + | |
| '}' + | |
| // Recipient name with underline | |
| '.recipient-name {' + | |
| ' font-size: 36px;' + | |
| ' color: #0070d2;' + | |
| ' font-family: "Trebuchet MS", sans-serif;' + | |
| ' margin: 30px 0;' + | |
| ' padding-bottom: 10px;' + | |
| ' border-bottom: 2px solid #c9a227;' + | |
| ' display: inline-block;' + | |
| '}' + | |
| // Course name | |
| '.course-name {' + | |
| ' font-size: 24px;' + | |
| ' color: #333;' + | |
| ' font-weight: bold;' + | |
| ' margin: 20px 0;' + | |
| '}' + | |
| // Date styling | |
| '.certificate-date {' + | |
| ' color: #999;' + | |
| ' font-size: 14px;' + | |
| ' margin-top: 30px;' + | |
| '}' + | |
| // Signature line | |
| '.signature-line {' + | |
| ' border-top: 1px solid #333;' + | |
| ' width: 200px;' + | |
| ' padding-top: 10px;' + | |
| ' font-size: 12px;' + | |
| ' color: #666;' + | |
| '}' + | |
| '</style>' + | |
| '</head>' + | |
| '<body>' + | |
| '<div class="certificate-border">' + | |
| '<div class="certificate-header">Certificate of Completion</div>' + | |
| '<div class="certificate-title">Achievement Award</div>' + | |
| '<div class="certificate-text">This is to certify that</div>' + | |
| '<div class="recipient-name">' + String.escapeSingleQuotes(recipientName) + '</div>' + | |
| '<div class="certificate-text">has successfully completed the course</div>' + | |
| '<div class="course-name">' + String.escapeSingleQuotes(courseName) + '</div>' + | |
| '<div class="certificate-date">Completed on ' + completionDate.format() + '</div>' + | |
| // Signature section | |
| '<table style="width: 100%; margin-top: 40px;">' + | |
| '<tr>' + | |
| '<td style="text-align: center; width: 50%;">' + | |
| '<div class="signature-line">' + String.escapeSingleQuotes(instructorName) + '<br/>Instructor</div>' + | |
| '</td>' + | |
| '<td style="text-align: center; width: 50%;">' + | |
| '<div class="signature-line">Salesforce<br/>Training Authority</div>' + | |
| '</td>' + | |
| '</tr>' + | |
| '</table>' + | |
| '</div>' + | |
| '</body>' + | |
| '</html>'; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment