1

I am trying to open a PDF that I am receiving as a streaming PDF from a microservice and open a visualforce page that contains the viewable PDF file. I can convert the streaming PDF to base64 using EncodingUtil.base64Encode. But am having troubles viewing the PDF from the visual force page.

Edit: Apex Class

@AuraEnabled
public static String getBase(){

    String pdfContent =  TenDashboardscallsController.HousehldQprReportPDF();
    Blob beforeBlob = blob.valueOf(pdfContent);
    String myBlob = EncodingUtil.base64Encode(beforeBlob);
    return myBlob;

}

PDF Response

enter image description here

Link to Original Response from TenDashboardscallsController.HousehldQprReportPDF() that is in the Apex class above https://justpaste.it/5zw0r

  • 1
    Can you elaborate what you mean by Streaming PDF, and can you post the code of how you are receiving the PDF currently? – Bryan Anderson Nov 21 '19 at 17:33
  • @BryanAnderson I have edited the original post to show the apex and the response I am getting for the PDF microservice. When I host it in a VF page I am seeing 16 pages of a PDF with nothing on it. – Sam Tucker Nov 21 '19 at 18:40
  • 1
    So I see you are using the Annotation @AuraEnabled but you mention trying to view in a VF page. Are you trying to show the VF page in a Aura or LWC component? – Bryan Anderson Nov 21 '19 at 18:41
  • @BryanAnderson I am currently using an Aura component with a buttonMenu, and the ideal situation is to have this PDF be pulled up on a click of one of the MenuItems from the button menu on a different tab. So the PDF would be hosted on a VF page that would be invoked from a Aura component. – Sam Tucker Nov 21 '19 at 18:46
1

So I put together a sample VF page and controller that seems to work:

VF Page:
<apex:page controller="pdfController">
    <object data="{!'data:application/pdf;base64,'+pdfBase64}" type="application/pdf"></object>
</apex:page>

Controller:

public class pdfController
{
    public String pdfBase64 { get; private set; }

    public pdfController()
    {
        Http h = new Http();

        HttpRequest req = new HttpRequest();

        req.setMethod('GET');
        req.setEndpoint('http://www.africau.edu/images/default/sample.pdf');

        HttpResponse res = h.send(req);

        pdfBase64 = EncodingUtil.base64Encode(res.getBodyAsBlob());
    }
}
  • This is fantastic! I will look into using this. However at the moment I do not have access to get the entire endpoint that the callout is using. Is there anyway to do this without setting an endpoint and just by encoding the response I am getting into a base64? I am able to get the whole response into a base64 string but not sure how to show that as a PDF on the VF page – Sam Tucker Nov 21 '19 at 19:19
  • @SamTucker I would debug what the string pdfContent content looks like first. If it is already in base64, you can just pass it back to the VF page like I did without encoding it. – Bryan Anderson Nov 21 '19 at 19:22
  • I have passed the base64 of the response into your code and I am seeing the blank 16 pages of a pdf again. I have linked the response I am getting from the callout. Not sure if it is a problem with the response I am getting or somewhere else down the line. – Sam Tucker Nov 21 '19 at 20:05
  • @SamTucker so what I was suggesting was, was to just do a System.debug() of your pdfContent from the line String pdfContent = TenDashboardscallsController.HousehldQprReportPDF(); and "see" if it looks like base64 content – Bryan Anderson Nov 21 '19 at 20:07
  • when I do this it gives back this justpaste.it/5zw0r which seems to be some PDF format with XML and UTF-8, but I am able to encode that into base64 format and pass it through to the VF page and it still displays the blank PDF. Thanks for all the great help by the way, much appreciated! – Sam Tucker Nov 21 '19 at 20:11

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.