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

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 ? – Giorgos Galaios Jan 17 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 Jan 18 at 4:24
  • Ok i will try it and see if it works. Thank you Jay! – Giorgos Galaios Jan 18 at 14:49
  • Thanks for the help Jay! It worked :) – Giorgos Galaios Jan 18 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.