[SalesForce] Convert Image to PDF and send via Email in Salesforce

I believe due to locker service these libraries are no longer working in Lightning component.
Verified in : https://developer.salesforce.com/docs/component-library/tools/locker-service-console
Is there any way to generate pdf out of image base64 data in lightning component.

Update:

How can I pass id dynamically while creating pdf using renderAs from Apex:

<apex:page id="testPage" applyBodyTag="false" renderAs="pdf"
           standardStylesheets="false"
           applyHtmlTag="false" showHeader="false" controller="TestCtrl">


    <img src="/servlet/servlet.FileDownload?file={!MyFileId}" width="100%" height="100%"/>
</apex:page>

I tried this, but I think as its a bing variable it did not work.

    Attachment attachment = new Attachment();
    attachment.ContentType = 'image/png';
    attachment.Name = 'Test123.png';
    attachment.ParentId = 'CaseId';
    attachment.Body = EncodingUtil.base64Decode(fetchBase64Image());
    insert attachment;
    attachmentId = attachment.Id;
    Page.ImageToPDF_ACE.getParameters().put('MyFileId', attachmentId);

So that I can make a fucture callout with that Id and use page.getContents to generate PDF.

Best Answer

Here is the way to convert image to PDF and send it from Lightning, or Visualforce or LWC:

@AuraEnabled
public static Object fetchBase64Image2(){
    Attachment attachment = new Attachment();
    attachment.ContentType = 'image/png';
    attachment.Name = 'Test123.png';
    attachment.ParentId = 'CaseId';
    attachment.Body = EncodingUtil.base64Decode(fetchBase64Image());
    insert attachment;
    attachmentId = attachment.Id;
    PageReference pageReference2 = new PageReference('/apex/ImageToPDF?FileId='+attachmentId);
    fetchContentDataFromPage(pageReference2.getUrl());
    return null;

}

@Future(Callout=true)
public static void fetchContentDataFromPage(String pageURL){
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

    Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
    attach.setContentType('application/pdf');
    attach.setFileName('Test.pdf');
    attach.setInline(false);
    attach.setBody(new PageReference(pageURL).getContent());
    email.setSubject('Test Email');
    List<String> lStrings = new List<String>();
    lStrings.add('Test@test.com');
    email.setToAddresses(lStrings);
    email.setPlainTextBody('Thank You .');
    email.setFileAttachments(new Messaging.EmailFileAttachment[]{
            attach
    });
    Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{
            email
    });
}

fetchBase64Image2, this method can be invoked from Lightning, here you can pass the base64 version of image file, or base64 version of any file.

Inside that method I just created an attachment on Case. And added the attachmentId to the Param of the new page reference. In the new page have a renderAs tag as PDF. Which will render the image as PDF.

Next we are going to fire a future callout to fetch the page contents of the page reference we just created.

fetchContentDataFromPage(pageReference2.getUrl());

and finally send the image as pdf from future.

Here is the VF Page:

<apex:page id="testPage" applyBodyTag="false" renderAs="pdf"
           standardStylesheets="false"
           applyHtmlTag="false" showHeader="false" controller="TestClass">


    <img src="/servlet/servlet.FileDownload?file={!$CurrentPage.parameters.FileId}" width="100%" height="100%"/>
</apex:page>
Related Topic