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 

PDF unable to load..when using oncomplete or rerender..?

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..
pradeep naredlapradeep naredla
hi mike,

      PageReference pdf = Page.GenerateQuotePDF;
    pdf.getParameters().put('id',parentId);
    pdf.setRedirect(true);

replace the part of code with this. hope it will work.

thanks.
mike1051mike1051
Hi Pradeep

Thanks for your reply..I tried this but i am facing the same issue..I am able to save the PDF but is is not opening..It says failed to load document.

I have changed like you said..

Any other suggestions..?

public void savePdf() {

    PageReference pdf = Page.GenerateQuotePDF;
    pdf.getParameters().put('id',parentId);
    pdf.setRedirect(true);

    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;

  }


Tejpal KumawatTejpal Kumawat
Hello Mike would you try this snippt :

public PageReference 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;
   
pdf.setRedirect(true);
return pdf;
  }


Thanks
Tejpal
Vinit_KumarVinit_Kumar
Mike,

I juste tested the below code works for me in my org.

VF page :

<apex:page controller="uploadPDF">
  <apex:form>
  <apex:pageBlock id="theBlock">
   <apex:commandButton value="Save to Opportunity" action="{!savePdf}" reRender="theBlock"/>
  </apex:pageBlock>
  </apex:form>
</apex:page>

Apex Class

public class uploadPDF
{   
    
    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;

    }
}

If this helps,please mark it as best answer to help others :)
mike1051mike1051
Hey

Still its not working

Can you suggest some thing else..
Vinit_KumarVinit_Kumar
Mike,

What's not working for you coz the same code works for me.

I don't understand why ot would work for me and not for you.
mike1051mike1051
Hello Vinit

I have reposted this question with some more details..Can you please check this and tell me why i am facing this issue.


https://developer.salesforce.com/forums?id=906F0000000AYV3IAO