Summer's 26

Generate PDF in Apex (no VF page) – Spring ’26 release

By Apex Sikho · September 11, 2026

1. Apex: Generate PDF & Save as File (Spring ’26 way)

public with sharing class PdfService {

 @AuraEnabled public static Id generateAndAttachPdf(Id recordId) {

 // Build HTML dynamically String html = '' + '<html>' + '<head>' + '<style>' + 'body { font-family: Arial; font-size: 12px; }' + 'h1 { color: #2E86C1; }' + '</style>' + '</head>' + '<body>' + '<h1>Invoice</h1>' + '<p>Record Id: ' + recordId + '</p>' + '<p>Date: ' + Date.today().format() + '</p>' + '</body>' + '</html>';

 // Convert HTML → PDF (Spring 26) Blob pdfBlob = Blob.valueOf(html).toPdf();

 // Create File (ContentVersion) ContentVersion cv = new ContentVersion(); cv.Title = 'Invoice_' + recordId; cv.PathOnClient = 'Invoice.pdf'; cv.VersionData = pdfBlob; insert cv;

 // Get ContentDocumentId cv = [ SELECT ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id ];

 // Link file to record ContentDocumentLink cdl = new ContentDocumentLink(); cdl.ContentDocumentId = cv.ContentDocumentId; cdl.LinkedEntityId = recordId; cdl.ShareType = 'V'; cdl.Visibility = 'AllUsers'; insert cdl;

 return cv.ContentDocumentId; } }

2. LWC HTML

<template> <lightning-button label="Generate & Download PDF" variant="brand" onclick={handleDownload}> </lightning-button> </template>

3. LWC Js : Generate + Download PDF

import { LightningElement, api } from 'lwc'; import generateAndAttachPdf from '@salesforce/apex/PdfService.generateAndAttachPdf';

export default class PdfDownloader extends LightningElement { @api recordId;

 handleDownload() { generateAndAttachPdf({ recordId: this.recordId }) .then(contentDocumentId => { window.open( `/sfc/servlet.shepherd/document/download/${contentDocumentId}`, '_blank' ); }) .catch(error => { console.error(error); }); } }

4.LWC Meta XML

<?xml version=”1.0″ encoding=”UTF-8″?>

<LightningComponentBundle xmlns=”http://soap.sforce.com/2006/04/metadata”>

<apiVersion>60.0</apiVersion>

<isExposed>true</isExposed>

<targets>

<target>lightning__RecordPage</target>

</targets>

</LightningComponentBundle>

Result :-

  • User clicks Generate & Download PDF
  • Apex generates PDF (no VF)
  • File saved in Files related list
  • PDF opens in new tab Final Output :- ———————————- Invoice ———————————- Record Id : 006XXXXXXXXXXXX Date : 01-Jan-2026