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
Leticia Monteiro Freitas 4Leticia Monteiro Freitas 4 

Download a pdf from a lightning component

Hello, I'm rendering a base64 file to pdf in a lightning component. Using the model of kummark22 (https://github.com/kumarrk21/PDFViewer)

But in this way I cant download. Can anyone help me?
Deepali KulshresthaDeepali Kulshrestha
Hi Leticia,

i just tried this and was able to create the attachment successfully: - 

I created one vf page that renders a PDF:
VF Page Code:-->

<apex:page controller="TextVFPDFController" renderAs="PDF">

    {!opp.Name}

</apex:page>
I am just showing opportunity name in PDF. Here is the controller for vf page - 
Apex code:-->

public class TextVFPDFController {

    public Opportunity opp{get;set;}

    public TextVFPDFController(){

        Id oppId = apexpages.currentpage().getparameters().get('id');

        opp = [select id,Name from opportunity where id=: oppId];

    }

}

Now i created one lightning component  which has only one save button , on click of this button , it will create same pdf and attach it to an account 
 
view sourceprint?

public class TestAppController {

    @auraEnabled

    public static void savePDFOpportunity(){
        PageReference pdfPage = new PageReference('/apex/TextVFPDF');
        pdfPage.getParameters().put('Id', '0062800000C044o');

        Blob pdfContent = pdfPage.getContent();

        Attachment attach1= new Attachment();

        attach1.ParentId = '0010K00001e2kcG';

        attach1.Name = 'Test Attachment for PDF';

        attach1.Body = pdfContent;

        attach1.contentType = 'application/pdf';

        insert attach1;

    }

}
 

<aura:application controller="TestAppController">
    <lightning:button variant="brand" label="Save" onclick="{! c.savePDF }" />
</aura:application>
 

({

    savePDF : function(cmp, event, helper) {
        var action = cmp.get("c.savePDFOpportunity");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                alert('Attachment saved successfully');

            }
            else if (state === "INCOMPLETE") {
                // do something
            }
                else if (state === "ERROR") {

                    var errors = response.getError();
                    if (errors) {
                        if (errors[0] && errors[0].message) {
                            console.log("Error message: " +
                                        errors[0].message);
                        }
                    } else {
                        console.log("Unknown error");
                    }
                }
        });
        $A.enqueueAction(action);
    }

})


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com