3

I'm calling an external endpoint which send back a pdf report as the response. Request should have some header info as well so thinking execute the request in the controller. But how is it possible to render the pdf if I can't return a PageReference ? Any other trick? Out of mind.

public void generateReport(){
        if(refreshToken != null){
            String credentials = EncodingUtil.base64Encode(Blob.valueOf('Administrator:'));

             HttpRequest req = new HttpRequest();
             String endPoint = 'endpointURL';
             req.setEndpoint(endPoint);
             req.setMethod('GET');
             req.setTimeout(60000);
             req.setHeader('content-type','application/x-www-form-urlencoded; charset=utf-8');
             req.setHeader('Authorization', 'Bearer '+accessToken);  
             //setting other headers

             Http http = new Http();
             HTTPResponse res = http.send(req); 
         }
    }

Testing with a command Link

<apex:commandLink target="_blank"  action="{!generateReport}" value="Report Link"/>}" value="Report Link"/>
2
  • Could you please tell me what kind of response your getting from the database ?
    – Vineeth
    Commented Jun 17, 2015 at 9:49
  • Hope it's to be a PDF content, I used Attachment object to handle it as @LaceySnr suggested.
    – highfive
    Commented Jun 17, 2015 at 11:23

2 Answers 2

2

You might be better off just making this request via Javascript from the client side if you can do that without any security concerns.

The only real way I can think of achieving this otherwise would be to store the PDF response you get into an attachment, and then provide the user with a link to that instead. There's a few issues here though: you could quickly fill up the storage available in the org, and large PDFs would be too big for the heap.

1
  • Mind blowing about the second option! I thought about JavaScript method but most of the forums blaming for browser compatibility isn't it? :(
    – highfive
    Commented Jun 17, 2015 at 8:35
1

To anyone it may help, this is what I've come up with the help of @LaceySnr ideas.

public void generateReport(){
    if(refreshToken != null){
        String credentials = EncodingUtil.base64Encode(Blob.valueOf('Administrator:'));

         HttpRequest req = new HttpRequest();
         String endPoint = 'endpointURL';
         req.setEndpoint(endPoint);
         req.setMethod('GET');
         req.setTimeout(60000);
         req.setHeader('content-type','application/x-www-form-urlencoded; charset=utf-8');
         req.setHeader('Authorization', 'Bearer '+accessToken);  
         //setting other headers

         Http http = new Http();
         HTTPResponse res = http.send(req); 

         //below snippet is the solution
         transient Blob blbPDFContent = res.getBodyAsBlob();                 
         transient Attachment attachmentPDF = new Attachment();
         attachmentPdf.parentId = myParenObjecttId;
         attachmentPdf.name = 'Report.pdf';
         attachmentPdf.body = blbPDFContent;
         insert attachmentPDF;
     }
}

This is far better than just rendering the pdf for us since in this way it's there in salesforce environment for future reference as well (Only if you are not worry about the storage the much as @LeceySnr mentioned) .

3
  • What's significance of credentials and accessToken variable, I see one declared but not used and other not declared and yet used. Also I tried similar approach (to get a HTTP webpage) and resultant PDF file says Invalid PDF file. Any clue here? Commented Sep 1, 2015 at 8:01
  • though variable usage is not the main topic here, credentials is used under setting other params . Question is about using a pdf response, solution is storing it as an attachment which can be used in future as well
    – highfive
    Commented Sep 1, 2015 at 8:30
  • I tried similar approach to generate PDF attachment from a webpage, but the result PDF file generated from my code is invalid pdf file. Any inputs here on what I need to take care when generating PDF attachment from a web page ? Commented Sep 3, 2015 at 7:27

You must log in to answer this question.

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