1

I have a requirement to perform a callout, get response (byte array) that is a PDF base 64 encoded. Then, i need to show the pdf, without saving any data in Salesforce. I just need to view the response in PDF. So, is there a way to achieve this? What i have done is: I have created an LWC to perform the callout on rowaction of a Lightning data table. So onRowAction a JS function is called which calls Apex. Apex performs the callout, gets response and then i need to render the result in PDF. I have tried opening a Visualforce page (calling it either from JS Controller of LWC or from Apex, but i could not find solution... I could not pass my parameter to the page so the PDF was always blank. Please find below a high level diagram of the problem that i am facing and what i want to do.. enter image description here

1 Answer 1

2

You could do something like this in VF.

<apex:page>
<script>

    function getBase64FromCallout(){
        //Make a callout to fetch base64, you may use VF Remoting or VF action function to get the base64 string  
    }

    window.addEventListener("load",function (){
        let base64 = this.getBase64FromCallout();
        let pdfurl = "data:application/pdf;base64, " + base64;
        document.getElementById("pdfFrame").setAttribute("src",pdfurl);
    })

</script>

<iframe id="pdfFrame"></iframe>

</apex:page>

Unfortunately, LWC does not allow rendering data: and blob: urls in an iframe as it violates CSP. Alternatively, for LWC, you can try implementing PDF.js or PDFTron but it could be costlier both time and effort wise.

4
  • Hi Jay and thanks for the response. So, as per my understanding, you suggest making the callout directly from the vf page instead of making the callout in lwc and passing the blob to the vf page. Correct ? Commented Jan 17, 2021 at 19:32
  • @GiorgosGalaios Yes, this is a VF only solution. You can also do a hybrid LWC + VF solution. Embed VF into LWC using iframe. Do the callout in LWC, pass received base64 to iframe using postMessage API. iframe, i.e, VF page when receives base64 will render in another iframe.
    – Jay Rathod
    Commented Jan 18, 2021 at 4:24
  • Ok i will try it and see if it works. Thank you Jay! Commented Jan 18, 2021 at 14:49
  • Thanks for the help Jay! It worked :) Commented Jan 18, 2021 at 16:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .