+ Start a Discussion
Lukasz PiziakLukasz Piziak 

Displaying a PDF attachment on a record as a Visual Page

Hi All,

I'm trying to achieve the preview of my related content attached to my record (always PDF document) on my custom object as a section with Visual Page. So far I was able to create a simple visual page and embedded to my record page layout but I don't know of how to make that page will preview my PDF document taken from related content section.
I appriciate any help.
Thank you
Amritesh SahuAmritesh Sahu
Hi Lukasz,

You can utilize the built-in pdf viewing capabilities of modern browsers.
Visualforce Page
<apex:page standardController="Account" extensions="InlinePDFController" showHeader="false" sidebar="false">
    <apex:outputPanel layout="none" rendered="{!AND(attachmentExists,contentPdf)}">
        <script type="text/javascript">
            document.location.href = '/servlet/servlet.FileDownload?file={!attachmentId}';
    <apex:outputPanel layout="block" rendered="{!AND(attachmentExists,NOT(contentPdf))}">
        Attachment is not in PDF format. 
    <apex:outputPanel rendered="{!NOT(attachmentExists)}">
        No attachment found.

Controller :
public without sharing class InlinePdfController {
    public Boolean attachmentExists {get; set; }
    public Boolean contentPdf {get; set; }
    public Id attachmentId {get; set; }

    public InlinePDFController(ApexPages.StandardController standardController) {
        Id accountId = standardController.getId();
        List<Attachment> attachments = [SELECT Id, Name, ContentType, ParentId 
                                        FROM Attachment
                                        WHERE ParentId = :accountId
                                        AND Name like '%some search term%'
                                        ORDER BY LastModifiedDate DESC];
        attachmentExists = false;
        contentPdf = false;
        if(attachments.length() > 0) {
            attachmentExists = true;
            Attachment a = attachments[0];
            if(a.ContentType != null && ((a.ContentType.toLowerCase().indexOf('pdf') > -1) || (a.Name.toLowerCase().endsWith('.pdf')))
                contentPdf = true;
            attachmentId = a.Id;

Lukasz PiziakLukasz Piziak
Hi Amritesh,

Thank you for VP and Controller. 
I have a question if this PDF preview capability will work with PDF docuemnts attached to RELATED CONTENT section in my custom object record?

Lukasz PiziakLukasz Piziak
Hi Amritesh,

I have started from creating an controller. I'm new to salesforce apex coding. PLease see ans error after trying to save proposed code:
[Error] Error: Compile Error: expecting a right parentheses, found 'contentPdf' at line 19 column 16
Could yopu please advise of how I can correct this code to work in my org?
Thank you.