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
sonaperthsonaperth 

attach pdf to notes & attachments

hi guys,

how to attach a pdf file to 'notes & attachments' part of an object?

i was able to generate a pdf file using the visualforce with a custom controller.

then they ask to automatically generate and Attach the pdf file to the 'notes & attachment' as well when you press the custom 'buttons and links'.

all i can find is how to attach it to email.

can anyone help? thanks in advance.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
prageethprageeth

Hello sonaperth; 

Try this;

public void attach() {

Attachment myAttach = new Attachment();

myAttach.ParentId = 'xxxxxxxxxx';//Id of the object to which the page is attached

myAttach.name = 'DisplayName.pdf';

PageReference myPdf = Page.myPdfPage;//myPdfPage is the name of your pdf page

myAttach.body = myPdf.getContentAsPdf();

insert myAttach;

} 

 

 

 

Message Edited by prageeth on 11-26-2009 08:08 PM

All Answers

prageethprageeth

Hello sonaperth; 

Try this;

public void attach() {

Attachment myAttach = new Attachment();

myAttach.ParentId = 'xxxxxxxxxx';//Id of the object to which the page is attached

myAttach.name = 'DisplayName.pdf';

PageReference myPdf = Page.myPdfPage;//myPdfPage is the name of your pdf page

myAttach.body = myPdf.getContentAsPdf();

insert myAttach;

} 

 

 

 

Message Edited by prageeth on 11-26-2009 08:08 PM
This was selected as the best answer
sonaperthsonaperth
i just found this 'attachment' object, and your example is just right on time, thank you very much!
pmozz01pmozz01

I am a novice at programming and have finally finished my renderPDF page and it looks fine.  However, I cannot figure out what the next step is to save and attach.  The code below is a mystery to me. Can someone help?  Is this supposed to be created as an Apex Class? a Controller? or is it part of the VF page that renders the PDF? or something else?  What does "public void attach" mean?  I cannot find anything to help me finish this project. 

 

I wanted to render a particular Lead Layout as a PDF, which I created in VF, and added a button.  What I would really like is if, on the click of the button, the PDF is rendered and saved as an attachment.  Please Help!!!

 

Thank you.

Message Edited by pmozz01 on 11-30-2009 02:43 PM
RelaxItsJustCodeRelaxItsJustCode

 

Prageeth,

 

How do I use the code snippet that you have provided so that I can save VF pages renderas PDF to notes & attachments (I mean does this come in a class or part of the VF controller, sorry I'm a little new when it comes down to VF stuff)?

 

I will give kudos if you can help me.

 

Thank you,

Steve Laycock

Jay reddyJay reddy
Hi sonaperth,

i kinda have the same requirement, adding the manually attached document to the notes and attachment section. As above, where did you add the controller code? As a separate controller or added to vf page controller?!

thanks,
GR
Jay reddyJay reddy
Also could you share the vf and controlle code as well, please.
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