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
Nihar SharmaNihar Sharma 

Save Pdf as a attachement in Notes and attachment using apex class(Without clicking on Button)

Hi All,

My question is regarding Save Pdf as in Notes and Attachment under the record,

So, my issue is,

First controller is fill the document(PDF) and Second controller can Save that PDF using that first Controller, How can i Do this ?

Or any other solution to save this PDF in Notes and Attachment ?

Please Help..

Thank you.
Himanshu ParasharHimanshu Parashar
Hi Nihar,

Here are the steps you can following


1. Create a new vf page name GeneratedPDF and set renderaspdf attribute in <apex:page  and write controller of this page which will accpet the
 
<apex:page standardController="yourcontroller" showHeader="false" renderAs="pdf">

//REST OF THE CODE

</apex:page>



2. This will be your second conroller method which you can call from anywhere.
public pageReference savePDF() { 
	pageReference pdf = Page.GeneratedPDF;
	pdf.getParameters().put('id',RECORDID);
	pdf.setRedirect(true);
		
	blob body = pdf.getContentAsPDF();
	string filename = 'my File Name';
		
	attachment theFile = new attachment();
		
	theFile.isPrivate = false;
	theFile.body = body;
	theFile.ParentId = activeCase.id;
	theFile.Name = filename;

	insert theFile;

	return null;
}

You will learn one thing here that is  pdf.getContentAsPDF() will return page content as pdf


Thanks,
Himanshu
Nihar SharmaNihar Sharma
Hi Himanshu,

Thanks for your reply.

My question is i do have a page which is called from custom button from contract object, when i click on that button page is renderAs pdf and after that i want to save the pdf to the contract object (In Notes And Attachment).
In Controller there is a logic which is bringing text Block of this pdf and merging the field of contract so, this action performs in the constructor of the controller.
Now, please tell me how can i SAVE this pdf ?

​Thank you once again.
Ankit Arora 30Ankit Arora 30
@Everyone

I want to convert VF to PDF and then save it to Notes and attachments and below are the VF pages and apex class.

When I run the process, a pdf is created successfully at the right location under the Custom__c but its blank. When I "login to community as a user", again the same vf page is blank but when I create a custom link of vf page in Custom__c object, I see the entire letter in pdf. Before putting the extension and action tag, I was able to see the same page from the custom link and from "login to community as a user". Can you please tell me what I am doing wrong, why its showing blank page.
Also VF page render the output dynamically, its not a static page.
Please help me.

1st Visualforce Page: (Letter)
<apex:page standardcontroller="Custom__c" extensions="attachPDFToCustom" action="{!attachPDF}" standardStylesheets="false" showHeader="false" applyHtmlTag="false" applyBodyTag="false" docType="html-5.0" >
<apex:include pageName="Letter_V2"/>

2nd Visualforce Page: (Letter_V2)
<apex:page renderAs="pdf"> </apex:page>

Apex Class:
public class attachPDFToCustom {

public attachPDFToCustom(ApexPages.StandardController standardPageController) {
    }
 
public void attachPDF() {
PageReference pdfPage = Page.Letter_V2;
Attachment attach = new Attachment();
Blob pdfBlob;
      try {
             pdfBlob = pdfPage.getContent();
           }
       catch (VisualforceException e) {
             pdfBlob = Blob.valueOf('Some Text');
           }
                
attach.parentId = ApexPages.currentPage().getParameters().get('id');
attach.Name = 'Letter - '+ system.Now() + ' .pdf';
attach.body = pdfBlob;
insert attach;
    }
}


Thank you

Regards,
Ankit