Friday, 13 June 2014

View PDF Files in salesforce.com


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 = '0019000000YSebM';
        List < Attachment > attachments = [SELECT Id, Name, ContentType, ParentId
            FROM Attachment
            WHERE ParentId = : accountId
            AND Name like '%report_a0W3000000lMuNgEAK.pdf%'
            ORDER BY LastModifiedDate DESC
        ];
        attachmentExists = false;
        contentPdf = false;
        if (attachments.size() > 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;
        }
    }
}

visual force 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}';
        </script>
    </apex:outputPanel>
    <apex:outputPanel layout="block" rendered="{!AND(attachmentExists,NOT(contentPdf))}">
        Attachment is not in PDF format. 
    </apex:outputPanel>
    <apex:outputPanel rendered="{!NOT(attachmentExists)}">
        No attachment found.
    </apex:outputPanel>
</apex:page>


No comments:

Post a Comment