1

I am using a HttpRequest to GET a PDF:

http://www.example.com/path/some.pdf

I am using response.getBodyAsBlob() to store the data into a ContentVersion

I can view the PDF if I open the ContentVersion via the Lightning UI.

Then I want to display the PDF in an iframe so that the user can print from within a custom component.

<apex:page controller="PDFPreviewController" showHeader="false" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false">
    <body>
         <iframe src="data:application/pdf;base64;{!PDF}" height="100%" width="100%"></iframe>
    </body>
</apex:page>

Using controller:

public with sharing class PDFPreviewController {

    public String getPDF() {
        return EncodingUtil.base64Encode([SELECT VersionData FROM ContentVersion WHERE ID = :RecordID].VersionData);
    }

    private String RecordID {
        get {
            return ApexPages.currentPage().getParameters().get('id');
        }
    }
}

But the PDF does not display.

I get this message in chrome:

This site can’t be reached
The web page at
data:application/pdf;base64;JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7/KQovQ3JlYXRvciAo/v8AdwBrAGgAdABtAGwAdABvAHAAZABmACAAMAAuADEAMgAuADQALQBkAGUAdikKL1Byb2R1Y2VyICj+/wBRAHQAIAA0AC4AOAAuADcpCi9DcmVhdGlvbkRhdGUgKEQ6MjAxOTA3MjIxOTAwMjlaKQo+PgplbmRvYmoKMyAwIG9iago8PAovVHlwZSAvRXh0R1N0YXRlCi9TQSB0cnVlCi9TTSAwLjAyCi9jYSAxLjAKL0NBIDEuMAovQUlTIGZhbHNlCi9TTWFzayAvTm9uZT4+CmVuZG9iago0IDAgb2JqClsvUGF0dGVybi
........ DEwNzUxIDAwMDAwIG4gCjAwMDAwMTEwMjAgMDAwMDAgbiAKMDAwMDAxNTExOCAwMDAwMCBuIAowMDAwMDE1NDU3IDAwMDAwIG4gCjAwMDAwMTUwOTcgMDAwMDAgbiAKdHJhaWxlcgo8PAovU2l6ZSAzMwovSW5mbyAxIDAgUgovUm9vdCAxOCAwIFIKPj4Kc3RhcnR4cmVmCjE2MzE5CiUlRU9GCg==
might be temporarily down or it may have moved permanently to a new web address.
ERR_INVALID_URL

Questions

  1. What am I doing wrong?
  2. How can I fix it?
  • can you add JS code? This seems wrong src="data:application/pdf;base64;{!PDF}" , expression in quotes never worked for me. Can you inspect iframe src if its fine? – Pranay Jaiswal Jul 22 at 18:52
  • Instead of src="data:application/pdf;base64;{!PDF}" , do something like src={pdfUrl} and in js pdfUrl = "data:application/pdf;base64;" + base64STringFromApex; – Pranay Jaiswal Jul 22 at 18:57
  • In VF the properly way to write the src I believe is: src="{! 'data:application/pdf;base64;' + PDF}" – Brian Miller Jul 22 at 19:20
  • Try a different approach - make the apex:page with the renderAs="pdf" attribute, and then use apex:includes on that page in an outer page? – Brian Miller Jul 22 at 19:23
  • See apex:include. Ex: <apex:include pageName="MyPdfPage" /> – Brian Miller Jul 22 at 19:50
1

You have to alter your getPDF() method and prepend data:application/pdf;base64,

public String getPDF() {
        return  'data:application/pdf;base64,'+EncodingUtil.base64Encode([SELECT VersionData FROM ContentVersion WHERE ID = :RecordID].VersionData);
    }

Markup:

 <iframe src="{!PDF}" height="100%" width="100%"></iframe>

enter image description here

  • The problem was being caused by this: base64; where I should have been using base64, ... how frustrating!! thank you!! – Robs Jul 22 at 19:54

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.