function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jonathan Wolff 7Jonathan Wolff 7 

Creating Apex with PageReference.getContentAsPDF() to create pdf in flow

Hello, I have a Visualforce template. I want to use an apex class with PageReference.getContentAsPDF() to create a pdf through a flow with the VF template. Could you give me an example of the Apex class to use for my "Visualforce code" that I can add to a flow to create a pdf. (I want to achive, that the fields of the visualforce like (!Account.Name) are filled with screen flow inputs)

Thank you in advance.
Greetings
Jonathan

My Visualforce code:
<apex:page standardController="Account" renderAs="pdf"  >
    
   
    
    <h1>Welcome to APP!</h1>
        
        <p>Thank you, <b><apex:outputText value=" {!Account.Name}"/></b>, for 
            working with APP.</p>
        
        <p>Your account details are:</p>
        
        <table>
            <tr><th>Account Name</th>
                <td><apex:outputText value="{!Account.Name}"/></td>
            </tr>
            <tr><th>Account Rep</th>
                <td><apex:outputText value="{!Account.Owner.Name}"/></td>
            </tr>
            <tr><th>Customer Since</th>
                <td><apex:outputText value="{0,date,long}">
                    <apex:param value="{!Account.CreatedDate}"/>
                    </apex:outputText></td>
            </tr>
        </table>
       
</apex:page>


 
Maharajan CMaharajan C
Hi Jonathan,

Please find the below code as sample:
 
public class GeneratePDF{
   
@InvocableMethod(Label = 'PDF genreation' description='Input recordId')
public static void generatePDF(Id AccountId) {

     PageReference page = Page.AccountVFPageName  // Use your VF Page Name

     // add your record id which is comming from Flow
     page.getParameters().put('Id', AccountId);
	 
     // generate and PDF blob
     Blob contentBlob = page.getContentAsPDF();
     
    ContentVersion cv = new ContentVersion();
    cv.VersionData = contentBlob;
    cv.Title = 'Account Pdf';
    cv.PathOnClient = 'Account.pdf' ;
    insert cv;                
    cv = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id LIMIT 1];
        
    ContentDocumentLink cdl = new ContentDocumentLink();
    cdl.ContentDocumentId = cv.ContentDocumentId;
    cdl.ShareType = 'I';
    cdl.LinkedEntityId = AccountId;
    insert cdl;
	}
}

Use the below links for reference : 

https://automationchampion.com/2021/10/05/generating-a-quote-pdf-using-salesforce-flow/

https://github.com/Rakeshistom/Auto-Generates-Quote-PDF/blob/master/generateQuotePdfDocument.cls

https://trailhead.salesforce.com/en/trailblazer-community/feed/0D54S00000A90DSSAZ

Thanks,
Maharajan.C
Jonathan Wolff 7Jonathan Wolff 7
Hi Maharajan, I created the Apex class like that, but get 2 errors for it. How do I solve them?

User-added image
public class GeneratePDF{
   
@InvocableMethod(Label = 'PDF genreation' description='')
public static void generatePDF(Id AccountId) {

     PageReference page = Page.PDF_Account_test  

    
     page.getParameters().put('Id', AccountId);
     Blob contentBlob = page.getContentAsPDF();
     
    ContentVersion cv = new ContentVersion();
    cv.VersionData = contentBlob;
    cv.Title = 'Account Pdf';
    cv.PathOnClient = 'Account.pdf' ;
    insert cv;                
    cv = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id LIMIT 1];
        
    ContentDocumentLink cdl = new ContentDocumentLink();
    cdl.ContentDocumentId = cv.ContentDocumentId;
    cdl.ShareType = 'I';
    cdl.LinkedEntityId = AccountId;
    insert cdl;
	}
}

 
Maharajan CMaharajan C
Try the below one :
 
public class GeneratePDF{
   
@InvocableMethod(Label = 'PDF genreation' description='')
public static void generatePDF(List<Id> AccountId) {

     PageReference page = Page.PDF_Account_test;

    
     page.getParameters().put('Id', AccountId.get(0));
     Blob contentBlob = page.getContentAsPDF();
     
    ContentVersion cv = new ContentVersion();
    cv.VersionData = contentBlob;
    cv.Title = 'Account Pdf';
    cv.PathOnClient = 'Account.pdf' ;
    insert cv;                
    cv = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id LIMIT 1];
        
    ContentDocumentLink cdl = new ContentDocumentLink();
    cdl.ContentDocumentId = cv.ContentDocumentId;
    cdl.ShareType = 'I';
    cdl.LinkedEntityId = AccountId.get(0);
    insert cdl;
	}
}

Thanks,
Maharajan.C