I have created a VF page and used "render as pdf"
<apex:commandButton value="Save to Opportunity" action="{!savePdf}" />
Its working fine when i am using above line without rerender and oncomplete but when i am adding rerender or oncomplete it is not working.I am able to save the PDF in attachment folder but when i am trying to open its showing "Unable to load document"
Even if i remove oncomplete and use Rerender then also its saving the attachment in Attachment object and when i view ,it is not loading and says "Unable to load document"
<apex:commandButton value="Save to Opportunity" action="{!savePdf}" oncomplete="myClose();"/>
Javascript Code
function myClose(){
self.close();
window.opener.location.href="/{!$CurrentPage.parameters.Id}";
}
Class Code
public void savePdf() {
PageReference pdf = Page.GenerateQuotePDF;
pdf.getParameters().put('id',parentId);
Attachment attach = new Attachment();
Blob body;
body = pdf.getContent();
attach.Body = body;
attach.Name = 'Opportunity'+ '.pdf';
attach.IsPrivate = false;
attach.ParentId = parentId;
attach.ContentType = 'application/pdf';
insert attach;
}
Please help..
Best Answer
A PDF document can only be rendered as a PDF once. If you "rerender" it, you're creating a new document for saving. When you load a saved PDF, it is already "complete" and has been "rendered". When you load it, you're loading a document with a mime type of "PDF". That's why those two key words are problematic for you in your code.
EDIT
Here's how to do the rest of what you want. You'll need to modify your controller to do it along with your VF page. Portions of the code came from http://iwritecrappycode.wordpress.com/.
In addition to the existing content, your VF Page should look something like this:
You'll need to add something like what's below to your controller:
If you call that page from a link it still opens in a new window & it forces the user to download the file. To get it to download and "close" the page, here's what you do:
Of course replace the 'yourPage' with the name of your visualforce page. The filename of course can be changed to be details from the record, or whatever you like. Now when the user clicks that button the javascript creates an invisible iframe and injects it into the DOM. Once it loads the user is prompted to download the file.
I believe that modifying kenjj776's code I've posted above with what you've already done should finish getting you to where you want to be.
If not, someplace, I used to have some button code that did this sort of thing, but can't seem to find it at the moment. You might want to look at the code for the standard quote button to see if that's of any help. I know there's also a post or two here on SF.SE on rendering to PDF & emailing the Pdf as an attatchment that you may want to search for as well as additional resources to look at.
Here's one in particular by mstOr that appears to provide a complete solution: How do I convert a visualforce page to a PDF?
The latter may perhaps give you the cleanest solution.