3

Is this post still valid as a solution to download a pdf from third-party site, then displaying it in a Visualforce page?

This is what I'm doing in my code:

Visualforce page

<apex:page controller="pdfDownloadController" sidebar="false" showHeader="false" contentType="application/pdf">
    <script>
        window.location.href = "data:application/pdf;base64,{!pdf}";
    </script>
</apex:page>

Apex Class

public class pdfDownloadController {

    public String getPdf() {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://www.irs.gov/pub/irs-pdf/fw4.pdf');
        req.setMethod('GET');                          

        // Make call
        Http client = new Http();
        HttpResponse rsp = client.send(req);

        return rsp.getBody(); 
    }

}

This is what loads when I go to this Visualforce page in lightning:

enter image description here

It doesn't appear to work when I go to classic either.

UPDATE:

As suggested by sfdcfox suggested, I used his method to display the pdf. That didn't work, but I did see these errors in the web console (Firefox):

enter image description here

I tried Chrome got a different browser response:

enter image description here

This what I get in the debug log from the response body. It looks like a pdf:

enter image description here

  • Based on the characters provided, it seems you're getting an HTML document in your response body. Have you tried debugging the value to see what "body" actually contains? We could probably use a hex dump (EncodingUtil.hexencode) to help us diagnose the problem. I strongly suspect this is a data issue, not a code issue. – sfdcfox Jun 4 '19 at 22:46
  • Is there another public site that hosts a pdf that we can try? – Tyler Zika Jun 4 '19 at 22:48
  • using that site for my endpoint, I get the same error. – Tyler Zika Jun 4 '19 at 22:52
  • Apparently navigation to data URIs is now prohibited by browsers. Please see my edit for more info, and let me know if you need anything else. – sfdcfox Jun 4 '19 at 23:13
1

When you return the body using HttpResponse.getBody(), it's going to be converted to a string like Blob[12345]. This won't be interpreted correctly as a PDF. You need to encode it as Base-64 first:

return EncodingUtil.base64Encode(rsp.getBodyAsBlob());

The page itself was being interpreted as PDF because of contentType="application/pdf". This caused the browser to try to interpret the HTML script as PDF, which caused the errors.

Fixing your VF code, and controller, results in the following:

public class pdfDownloadController {

    public String getPdf() {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://www.irs.gov/pub/irs-pdf/fw4.pdf');
        req.setMethod('GET');                          

        // Make call
        Http client = new Http();
        HttpResponse rsp = client.send(req);

        return EncodingUtil.base64Encode(rsp.getBodyAsBlob()); 
    }

}

<apex:page controller="pdfDownloadController" sidebar="false" showHeader="false">
 <script>
 window.location.href = "data:application/pdf;base64,{!pdf}";
 </script>
</apex:page>

This however, results in a new error:

Not allowed to navigate top frame to data URL: ...

It appears that this method is now blocked by web browsers.


So, basically, the method you're looking for is not directly available.

However, with a bit of HTML magic:

<apex:page controller="pdfDownloadController" sidebar="false" showHeader="false">
    <embed type="application/pdf" src="data:application/pdf;base64,{!pdf}" style="width: 100%; height: 100vh; display: inline-block" />
</apex:page>

We're able to display the PDF in the page. If you want something else, you might need to save the file as a ContentDocument if you want to be able to download/save/etc the file.

| improve this answer | |
  • @TylerZika Can you be more specific than "doesn't work"? What errors do you see in F12? Any errors in the Salesforce Developer Console? The more info we have, the more we can help you. – sfdcfox Jun 4 '19 at 22:26
  • you are a magician @sfdcfox, thanks! – Tyler Zika Jun 5 '19 at 0:39

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.