You need to sign in to do that
Don't have an account?

Save PDF as attachment
Hi,
I have a Visualforce page that creates a PDF contract of an opportunity, and I'm trying to save the PDF as an attachment on the opportunity. I think I'm almost there, but I'm getting an error
"DML currently not allowed
An unexpected error has occurred. Your development organization has been notified."
here is my controller extension
http://blog.jeffdouglas.com/2010/07/14/attach-a-pdf-to-a-record-in-salesforce/
Thanks,
I have a Visualforce page that creates a PDF contract of an opportunity, and I'm trying to save the PDF as an attachment on the opportunity. I think I'm almost there, but I'm getting an error
"DML currently not allowed
An unexpected error has occurred. Your development organization has been notified."
here is my controller extension
public class savePDF { // The extension constructor initializes the private member // variable pageOpp by using the getRecord method from the standard // controller. Private Opportunity pageOpp; public savePDF(ApexPages.StandardController stdController) { this.pageOpp = (Opportunity)stdController.getRecord(); system.debug('id = ' + pageOpp.id); PageReference pdf = Page.TWC_Contract; // create the new attachment Attachment attach = new Attachment(); // the contents of the attachment from the pdf Blob body; try { // returns the output of the page as a PDF body = pdf.getContent(); // need to pass unit test -- current bug } catch (VisualforceException e) { body = Blob.valueOf('Some Text'); } attach.Body = body; // add the user entered name attach.Name = 'test'; attach.IsPrivate = false; // attach the pdf to the account attach.ParentId = pageOpp.Id; insert attach; } }I kind of hypridized the code based on the post here
http://blog.jeffdouglas.com/2010/07/14/attach-a-pdf-to-a-record-in-salesforce/
Thanks,
Below is what worked for me. Note the calling of the method from the action function
VF page (to call the method). Note, I left out the rest of the contract code
And the Controller
All Answers
Like below
I put an action item in the <apex:page> block, to call the saveAttachement method. So this is now calling. However it's running twice. In addition, body = pdf.getContent(); is getting an error.
I tried pdf.getContentAsPDF();
But then I get the error
Too many nested getContent calls.
Below is what worked for me. Note the calling of the method from the action function
VF page (to call the method). Note, I left out the rest of the contract code
And the Controller
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