2

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

4
  • 1
    Can you elaborate what you mean by Streaming PDF, and can you post the code of how you are receiving the PDF currently? Commented Nov 21, 2019 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
    Commented Nov 21, 2019 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? Commented Nov 21, 2019 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
    Commented Nov 21, 2019 at 18:46

2 Answers 2

2

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());
    }
}
6
  • 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
    Commented Nov 21, 2019 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. Commented Nov 21, 2019 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
    Commented Nov 21, 2019 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 Commented Nov 21, 2019 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
    Commented Nov 21, 2019 at 20:11
0

Adding for others to understand how to identify the correct format of the response received from the api. Specifically when you expect a pdf file from the api. If not done correctly you will se blank pdf pages.

https://developer.salesforce.com/forums/?id=9062I000000IUKAQA4

pasting my answer below incase link breaks

HttpResponse response = h.send(req);

Its important to understand why blob.valueof(response.getBody()) did not work and response.getbodyasblob() works.

When using this

blob.valueof(response.getBody()) response.getBody() is getting the body from the response in String format. the PDF file gets corrupted here itself. We the use blob.valueof() to convert the String to Blob for saving as contentversion.attachment.

response.getbodyasblob() this methods retrieves the body from response directly as Blob. So no conversion takes place and pdf file is intact.

Why EncodingUtil.Base64Decode(response.getBody()) does not work here is because the response from the API is not a Base64 string. If you actually check the response you will find strange characters. Base64 only supports these characters:

Uppercase letters (indices 0-25): ABCDEFGHIJKLMNOPQRSTUVWXYZ Lowercase letters (indices 26-51): abcdefghijklmnopqrstuvwxyz Digits (indices 52-61): 0123456789 Special symbols (indices 62-63): +/

So trying to using EncodingUtil.Base64Decode(response.getBody()) with our response will give error of invalid characters. If you dig deeper and check the response received using System.debug(response.getBody()) you will realize that it is actually a PDF file format (the PDF specification for creating pdf files here (https://resources.infosecinstitute.com/topic/pdf-file-format-basic-structure/)). So we do not have to decode or uncompress the response in any way. We have to just retrieve the file as it is (as a blob) and save it to salesforce.

Example of (response.getBody()) you will see for these api responses which return PDF files.

%PDF-1.4 %���� 2 0 obj <</ColorSpace/DeviceRGB/Subtype/Image/Height 200/Filter/FlateDecode/Type/XObject/DecodeParms<</Columns 420/Colors 3/Predictor 15/BitsPerComponent 8>>/Width 420/Length 6827/BitsPerComponent 8>>stream x�� |���gfw�MBr�r�p1Ax)X�QZ,�*Z����h��`�XĖ��Z���)��A���A�=f�'�� �3��N�e��}?���d���;��3�� �����Bv� ;����

You must log in to answer this question.

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