0

I am getting a pdf response from an external application and I want to show that data as a pdf on the visualforce page. The data is showing on the page but in a non-readable format.

3
  • What code you have written to share the blob. Are you storing the PDF in Salesforce as well? Jan 30 '19 at 7:01
  • No , i am not storing the response in salesforce
    – Aman Jain
    Jan 30 '19 at 7:11
  • Blob contentData = res.getBodyAsBlob(); return EncodingUtil.base64Encode(contentData);
    – Aman Jain
    Jan 30 '19 at 7:13
0

As there is no standard way of doing this unless you are storing PDF in salesforce. So you need a third party library to display PDF in VF page. You can use PDF.JS for this as there are examples available for same Which you can refer.

One you can find here.

Update: I also find a native solution which you can use.

<apex:page controller="ViewPdf">
    <iframe src="data:{!att.ContentType};base64,{!pdf}" ></iframe>
</apex:page>

Controller:

public class ViewPdf {
    public Attachment att {
        get {
            if (att == null) {
                att = [SELECT Body, ContentType, Name FROM Attachment WHERE ID = '00PG0000004COZU'];
            }
            return att;
        }
        private set;
    }
    public String pdf {
        get {
            return EncodingUtil.Base64Encode(att.body);
        }
    }
}

Display a blob as a PDF on a Visualforce page

But keep in mind if you are using apex:form in page you might hit the ViewState limit.

4
  • This is not working for me. My pdf is not showing data in the correct readable format.
    – Aman Jain
    Jan 30 '19 at 7:28
  • @AmanJain I am using the first one in many projects and didn't face any error. You need to make sure your data is correct in first place. Jan 30 '19 at 7:29
  • pls don't suggest hardcode id.. and iframe is not good if he is creating appExchange product iFrame will not pass. Jan 30 '19 at 7:39
  • @cloudZigZag Thanks for the suggestion. But he didn't mention anywhere that this is for app exchange product. Also, this is a sample code pick from SF docs. This is expected to follow the general guidelines here. Jan 30 '19 at 8:29

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.