+ Start a Discussion
Rick MacGuiganRick MacGuigan 

How to save file as PDF to the new 'Files' object

What is the object reference to save a visual force page to the new Files object. It is currently going to the old Notes & Attachments object. 
Also, the pdf that does save to Notes and Attachment is blank. Does not contain the VF page as a pdf. Do I need to create another VF page that renders as a pdf only ? 
<apex:page standardController="Account_Summary__c" readOnly="false" extensions="AccountSummaryController" >
....
<apex:commandButton value="Save PDF and Return to Account" action="{!savePDFAndReturn}" />
 
//save page as PDF to Files and return user back to the account object.

    public PageReference savePDFAndReturn(){
        PageReference pageRef = null;

        try {
             PageReference saveRef = stdController.save();
             if (saveRef != null) { 
            
            // process PDF file attachment
             string pdfFileName = getFileName(System.today(), this.AccountSummary.Name);
 
             PageReference summaryPageRef = Page.AccountSummary;            
             summaryPageRef.getParameters().put('id', this.AccountSummary.Id);   

             Blob pdfSummary = (Test.isRunningTest() ? Blob.valueOf('Test Invoice') : summaryPageRef.getContentAsPDF());            
            
           /*Create the attachment record to save the PDF content.*/
             Attachment pdfDocument = new Attachment();
             pdfDocument.Name = pdfFileName;
             pdfDocument.Name = 'CRE_TestFile';
             pdfDocument.ParentId = this.AccountSummary.Id;  
             //pdfDocument.ParentId = 'a0F2C000000GzKfUAK';                
             pdfDocument.Body = pdfSummary;
             pdfDocument.ContentType = 'application/pdf';

             insert pdfDocument; 
            
            //*************************************
             pageRef = new PageReference('/' + this.ParentAcct.Id);
             pageRef.getParameters().put('inline','0');
             pageRef.setRedirect(true);
            }
        }
        catch (Exception ex) {
            ApexPages.addMessages(ex);
        }

        return pageRef;
    }   
 
 
  public static string getFileName(Date pdfDate, string summaryName) {
    //string formattedDate = getDateFormatted(pdfDate);
    string formattedDate = pdfDate.format();
    string[] values = new string[]{
       formattedDate
      ,summaryName
    };

    return String.format('{0} - {1} - Summary.pdf', values);
  }