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
mike1051mike1051 

'Failed to load document' error after PDF save.?

I am opening a pop up VF page as 'render as pdf'.I have created a button Save and on click on save i am closing the pop window and saving PDf to opportunity.

Issue

When i am saving the code when calling the savePdf method without rerender and oncomplete.Its saving the attachment and i am able to open the attachment in opportunity without issue.When i am using either using rerender or oncomplete i am able to save the attachment but it says ".'Failed to load document'"

Its been a week i am stuck please help.

<apex:form>

function myClose(){

    self.close();
    window.opener.location.href="/{!$CurrentPage.parameters.Id}";

    //you could also use self.close();   
    }
<apex:pageBlock mode="maindetail" id="thePb"> 

      <apex:pageBlockButtons >
        <apex:commandButton value="Save to Opportunity" action="{!savePdf}" oncomplete="myClose();"  />

      </apex:pageBlockButtons>
      <apex:pageMessages />

     <iframe height="600px" id="Page" name="InvoicePDF" src="/apex/GenerateQuotePDF?id={!parentId}" width="100%"></iframe>

    </apex:pageBlock>
  </apex:form>


Class

public void savePdf() {

    PageReference pdf = Page.GenerateQuotePDF;
    pdf.getParameters().put('id',parentId);
     pdf.setRedirect(true);
    // create the new attachment
  Attachment attach = new Attachment();

    // the contents of the attachment from the pdf
    Blob body;

        body = pdf.getContent();

    attach.Body = body;
    // add the user entered name
    attach.Name = 'Opportunity'+ '.pdf'; 
    attach.IsPrivate = false;
    // attach the pdf to the account
    attach.ParentId = parentId;
    attach.ContentType = 'application/pdf';
    insert attach;

  }

Vinit_KumarVinit_Kumar
Can you remove OnComplete attribute from Command button and then try ??
mike1051mike1051
Yes Its working  when i remove oncomplete and rerender.But i need oncomplete to close the window..I also need rerender to show a loading image..
Any suggestions,..
Vinit_KumarVinit_Kumar
So,here is the latest code in my org with oncomplete and reRender and working fine for me :-

VF page :
<apex:page standardController="Opportunity" extensions="uploadPDF">

<script>
function myClose(){

    self.close();
    window.opener.location.href="/{!$CurrentPage.parameters.Id}";

    //you could also use self.close();   
    }
</script>
  <apex:form >
  <apex:pageBlock id="theBlock">
   <apex:commandButton value="Save to Opportunity" action="{!savePdf}" reRender="theBlock" oncomplete="myClose();"/>
  </apex:pageBlock>
     

  
  </apex:form>
</apex:page>

Apex Class :
public class uploadPDF
{

    public uploadPDF(ApexPages.StandardController controller) {

    }

    
    
    public void savePdf() {

    Document doc = [select body,id from document where name='PDF Doc'];
    Attachment attach = new Attachment();

    Blob body;
   // body = pdf.getContent();
    attach.Body = doc.body;
    attach.Name = 'Opportunity'+ '.pdf'; 
    attach.IsPrivate = false;
    attach.ParentId = '0069000000JqM6A';
    attach.ContentType = 'application/pdf';
    insert attach;

  }
}
Can you remove the iFram tag from your VF page and another thing your onComplete function would only work if your action method returns true,if it is returning false it won't.

Hope this helps !!
mike1051mike1051
Hey

I cant remove iFrame as its my inner page.On outer page i have save buttons.If i remove iframe PDF will not come.Both of them have a same controller.

I am stuck on this issue for a week now but no solution

User-added image