I am using a HttpRequest to GET a 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">
             <iframe src="data:application/pdf;base64;{!PDF}" height="100%" width="100%"></iframe>

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
might be temporarily down or it may have moved permanently to a new web address.


  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? Jul 22, 2019 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; Jul 22, 2019 at 18:57
  • In VF the properly way to write the src I believe is: src="{! 'data:application/pdf;base64;' + PDF}" Jul 22, 2019 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? Jul 22, 2019 at 19:23
  • See apex:include. Ex: <apex:include pageName="MyPdfPage" /> Jul 22, 2019 at 19:50

1 Answer 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);


 <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, 2019 at 19:54

You must log in to answer this question.

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