function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
juppyjuppy 

Can I display pdf content in a vf page?

Hi all,

I have a client who has a couple of hundred pdf documents he wants to load into and view in sf - not enough to make it worthwhile using a document storage solution.

 

I can load a pdf as an attachment, and retrieve that in apex, but cannot get the attachment body to display on a vf page.

 

Does anyone know if this can be done please - and how to go about it?

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

If the PDF is downloaded, that rather smacks that the browser doesn't know what to do with it.  In mine (google chrome) it displays the PDFs inline.  

 

If you are using the adobe plugin, there's a help page for that at:

 

http://kb2.adobe.com/cps/328/328233.html

 

 

All Answers

bob_buzzardbob_buzzard

I've done this in the past by adding an iframe whose src is the attachment download URL, e.g.

 

<apex:repeat value="{!My_Object__c.attachments}" var="attachment">
   <apex:iframe src="{!URLFOR($Action.Attachment.Download, attachment.Id)}" />
</apex:repeat>

 

Ispita_NavatarIspita_Navatar

Hi ,

            You can display the pdf content in a visual force page by using code snippet below

 

List<Document> d = [Select id,body, bodyLength, ContentType, Url from Document where Name ='ConsentForm'];

                    NOTE: ConsentForm is a pdf document which is uploaded in document.

                                  if document or attachment content type is "application/pdf"

string strOrgId  = UserInfo.getOrganizationId();

string content = '/servlet/servlet.ImageServer?id=' + Contentdataid + '&oid='+ strOrgId ;                                              PageReference pg = new PageReference(content);

pg.setRedirect(true);

return pg;

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

juppyjuppy

Thanks for the quick response Bob (btw, I enjoyed A Most Peculiar Practice too...)

 

Had time to try your suggestion, and also SE's suggestion for treating the pdf as a Document, and sadly neither worked for me; the pdf is downloaded, but not displayed in the vf page, which is what I need to do.

 

I've tried several variants of your code including making the controller an extension of my custom object - full code follows:

 

 

public with sharing class SPVFC_ProductLabel
{
    public SPVFC_ProductLabel(ApexPages.StandardController sc)
    {
        
    }
}

 

 

<apex:page standardController="Product_Label__c" extensions="SPVFC_ProductLabel" >
    <apex:form >
        <apex:repeat value="{!Product_Label__c.Attachments}" var="attachment">
            <apex:iframe src="{!URLFOR($Action.Attachment.Download, attachment.Id)}" />
        </apex:repeat>
    </apex:form>  
</apex:page>

 

 

Any ideas?

 

Warmest regards

Mike

bob_buzzardbob_buzzard

If the PDF is downloaded, that rather smacks that the browser doesn't know what to do with it.  In mine (google chrome) it displays the PDFs inline.  

 

If you are using the adobe plugin, there's a help page for that at:

 

http://kb2.adobe.com/cps/328/328233.html

 

 

This was selected as the best answer