How to create PDF file from String Content using Salesforce Apex?

How to create PDF file from String Content using Salesforce Apex?

To generate PDF file from String Content using Salesforce Apex, use toPdf(). Check the following sample code for reference.

Sample Code:

Blob blobContent = Blob.toPdf( 'Testing File' );
ContentVersion objCV = new ContentVersion(
    Title = 'Test',
    PathOnClient = 'test.pdf',
    VersionData = blobContent,
    IsMajorVersion = true,
    ContentLocation = 'S'
);
insert objCV;
objCV = [
    SELECT ContentDocumentId
    FROM ContentVersion
    WHERE Id =: objCV.Id
];
ContentDocumentLink objCDL = new ContentDocumentLink(
    ContentDocumentId = objCV.ContentDocumentId,
    LinkedEntityId = '0013t00002XXRyDAAX',
    Visibility = 'AllUsers'
);
insert objCDL;

Output:

Leave a Reply