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
Brad Nordling 9Brad Nordling 9 

Attach pdf in apex different than upload attachment to object using platform upload

I have a list view button that creates a pdf file from a VF page and saves it as an attachment on the parent object.  All seems to be working except it does not work to view the pdf on the SF mobile app.  You only see the first page of the pdf. 

So when I look at the list of attachments on the object, I see the files and can select one and it opens in a servlet.FileDownload page.  If I download and then attach the same pdf (or any pdf) to the object using the platform Upload Files button, then a couple things are different.  The attachment appears in the list with a pdf icon, whereas the one attached during the apex process does not.  When I click on this manually uploaded file, it does not open in a separate servlet.FileDownload window.  It opens in a pdf viewer within the current page with a little "X" in the corner to close it.  It also renders correctly in the mobile app and I can scroll through the pages.

Now these different behaviors make me thing that if I can attach the pdf file in a different way and make it look and act as if I uploaded it through the Upload Files button, then it should work on mobile.  Any ideas on why this difference exists? 

public class DSBAttachPDF {
    public DSBAttachPDF(ApexPages.StandardController stdController) {       
    }

public pageReference saveAttachment() {
        string DSB = 'Daily Safety Briefing';                    
        argrc__Committee__c comm = [SELECT Id from argrc__Committee__c WHERE Name = :DSB];   
        List<argrc__Committee_Meeting__c> pageMtgLst = [SELECT Id, Name FROM argrc__Committee_Meeting__c WHERE argrc__Committee__c = :comm.Id ORDER BY Meeting_Date__c DESC LIMIT 1]; 
        if (pageMtgLst.size() > 0) {
            PageReference pdf = Page.DSB_Meeting_Reports_PDF_Attachment;
            Attachment attach = new Attachment();
            Blob body = pdf.getContentAsPDF();
            
            pdf.getParameters().put('generatepdf','true');
            attach.Body = body;
            attach.Name = 'DSB Roll Summary.PDF';
            attach.IsPrivate = false;
            attach.contenttype = 'application/pdf';
            attach.ParentId = pageMtgLst[0].Id;
            insert attach;

            PageReference pageRef = new PageReference('/'+pageMtgLst[0].Id);   
            pageRef.setRedirect(true);
            return pageRef;
        }
        else {
            return null;
        }
    }
}

Here's how the files appear in the list view.  The PDF icons were manually uploaded.  Notice the size.  I click the Apex generated ones, they open in servlet.fileDownload.  I click the manually entered ones, they open in the pdf viewer.  Notice the file names. Any ideas?

User-added image
Brad Nordling 9Brad Nordling 9
Has anybody created a PDF from a Visualforce page through an Action or List View button?  And then attached it to a parent object?