0

I have a requirement to perform a callout from an LWC to get invoices data and then display as a PDF the data returned. So i have an LWC with a lightning data table and on row action it calls an apex class to do the callout and get the result. In order to do the rendering to PDF, i created a visualforce page which is called from apex. However, i didn't manage to find solution i was getting several errors like "ERROR: Formula Expression is required on the action attributes." or like "common.apex.runtime.impl.executionexception argument cannot be null". Seems like i am doing something wrong when passing the data from Apex to the Visualforce.

so, the issue is:

  1. i have got the invoice data of structure:
public class el_invoices{
        @AuraEnabled
        public Boolean Success {get; set;}
        @AuraEnabled
        public String ErrorCode {get; set;}
        @AuraEnabled
        public String ErrorMessage {get; set;}
        @AuraEnabled
        public String BillNo {get; set;}
        @AuraEnabled
        public String BillData {get; set;}
    }
  1. Then on specific invoice action i call from my LWC an APEX function showPDF
@AuraEnabled
    public static void showPDF(String s){
        System.debug('in show pdf: '+s);
        navigate(s);
    }
    
    public static blob navigate(String s) {
        PageReference pg = new PageReference('/apex/el_generatePDF');
        pg.getParameters().put('displayText', 'sdfsdf');
        
        pg.setRedirect(true);
        //blob doccontent = pg.getContent();
        //ystem.debug('doccontent '+doccontent);
        system.debug('show pdf finish');
        return pg.getContent();
    }
  1. But my visualforce is always blank, like i cannot pass data from my apex class to the "displayText" attribute. Has anyone found solution on such topic? Again, what i want is to make a callout, and show a PDF (not save anything in the database)
<!--<apex:page controller="el_generatePDFController" contentType="application/pdf" renderAs="pdf">-->
<apex:page controller="el_generatePDFController" contentType="application/pdf" renderAs="pdf">

    <apex:form >
        <!-- Page code -->
        <apex:inputhidden id="hidData" value="{!displayText}"/>
        {!displayText}
    </apex:form>
    
</apex:page>
public with sharing class el_generatePDFController {
    
    public String displayText {get; set;}

    public String getdisplayText (){
        /*//System.debug('s: '+ s);
        System.debug('pdfdatastring: '+ pdfdatastring);
        return EncodingUtil.base64Decode(pdfdatastring).toString();
        */
        displayText = 'giorgos';
        return displayText;
    }
}

UDATE: I edit my question to show at a high level the problem that i am facing: enter image description here

0

You can't generate a form in the page that would be manipulated into rendering the PDF, because that would be a second round trip. You can either (a) put the content into a page parameter (maximum limit is about 3k characters), or, as I would recommend, use Blob.toPdf() to directly generate the PDF!

Here's a revised version of your method that should help:

public static blob navigate(String s) {
    return Blob.valueOf(s).toPDF();
}

Of course, you have some more work to do, but this should get you started. Just render a full, proper HTML document, pass it to your Apex method, and then you can render the PDF immediately.

8
  • Hi sfdcfox and thank you very much for your response! Could you please help me with what "render a full, proper HTML document" means? I should return my blob to the LWC? Or call a vf page passing the blob content as a parameter? I tried both but did not find solution. Jan 15 at 14:19
  • @GiorgosGalaios You don't need the VF page at all. Simply generate an HTML document, and call toPDF().
    – sfdcfox
    Jan 15 at 14:54
  • How could i generate an html document from lwc? You mean to reference a static resource? It is still not clear to me :( Jan 15 at 15:43
  • @GiorgosGalaios The string is literally HTML, such as <html><head><title>Report</title></head><body>...</body></html>, then passed in to Blob.valueOf, and then converted to a PDF via toPDF(). How you get to having that string is up to you. You could build it in JavaScript, Apex, or a combination thereof.
    – sfdcfox
    Jan 15 at 15:47
  • and then this string will be rendered as pdf in the LWC html part ? Jan 15 at 15:56

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.