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
Ryan DRyan D 

render a page as pdf in an iframe AND attach it to the record

I have a scenario that I need some advice on. 

 

Here is the user experience:

 

1. user clicks on a custom field("generate pdf") on the Opportunity page layout. 

2. clicking the field takes them to my "Landing page" where they are given a list of choices for which type of pdf report the want to create.

3. once they click their choice, which is an apex:outputLink to the renderAs pdf page, it opens the pdf page in an iFrame within my "Landing page".  From there they can use the browser-native print, save whatever.

 

I have this part working great.  The only thing I have to do now is, once they click on the outputLink and the page is generated, I want that pdf page to automatically get inserted into the Opportunity record as well as appear in the iFrame.

 

A little more background: the pdf page has it's own controller and a ton of classes behind it.  The landing page currently uses the Opportunity standardController so I can reference some fields from Opportunity.  

 

I was thinking I could use a controller extension to handle the insertion because I already found some code to insert a PDF page to a record.  The problem is that I think this will generate a second copy of the pdf page, which is quite heavy if the custom object being reported on has lots of data in it.

 

Another theory: could I somehow reference the page that is in the iFrame once it's rendered and then insert that page into the opportunity?

 

Any thoughts or help on this would be greatly appreciated.

 

Here is the basic code that I found already to insert the record:

//Call your VF page
        // WIth out any parameters to the vfpage.
        pageReference pdf = Page.YOURVFPAGE;
       //With paramters
        PageReference pdf = new PageReference('/apex/SamplePdfDoc?Id='+theParameter);
        Attachment attach = new Attachment();
        Blob body;
        //the getContent()method cannot used in Trigger, but there is a workaround which is a little tricky.
        body = pdf.getContent();
        attach.Body = body;
        attach.Name = 'pdfName.pdf';
        attach.IsPrivate = false;
        attach.ParentId = 'opprtunityID';
        insert attach;

 

 

sfdc2016sfdc2016
vf page:

<apex:page standardController="Opportunity" extensions="OppExt">
    <apex:includeScript value="https://code.jquery.com/jquery-1.9.1.min.js"/>
<html>

<script type="text/javascript"> 
    
            function generateIFrame() {   
                
          var oppId = document.getElementById('{!$Component.oId}').innerHTML;
                
                var doc = document.getElementById("volvo").value;
                
                 Visualforce.remoting.Manager.invokeAction(
                    '{!$RemoteAction.OppExt.getIFrame}', JSON.stringify(oppId), JSON.stringify(doc), 
                    function(result, event) {
                        alert('In callback function');
                        if(event.type === 'exception') {
                            alert('In exception');
                               console.log("exception");
                              console.log("----------");
                            console.log(event);
                          }
                        else if (event.status) {
                            alert('In else if');
                            console.log(result);
                            alert(result);
                            var elmnt = document.getElementById("if");
                elmnt.src = result
                             
                    }
                        else {
                            alert('In else');
                            console.log(event.message);
                        }
                       
                });
                }
        
     </script> 
     
   <div>
  <apex:outputText label="Opportunity Name" style="color:#A9A9A9;font-size:20px" value="{!$CurrentPage.parameters.id}" id="oId"/>
  </div>
  <div>
  <select id="car">
  <option id="volvo" value="volvo">Volvo</option>
  <option id="saab" value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>
  </div>
    
    <div>
        <input id="check" type="checkbox" name="vehicle" value="yes" checked="false" /> I have a bike
      </div>
        
  <div>
  <button id="generate" onclick="generateIFrame()">Generate</button>
  </div>
  <iframe id="if" frameborder="0" height="600px" name="j_id2" scrolling="no" title="Content" width="100%" />
 
  </html>
</apex:page>

iframe page:

<apex:page renderAs="pdf" standardController="Opportunity" extensions="OppExt">
    <script type="text/javascript">
    document.getElementById("p1").style.color = "red"; 
    </script>
  <!-- Begin Default Content REMOVE THIS -->
  <h1 id="header">Congratulations</h1>
  <p id="p1" style="color:red">
      This is your new Page: volvo
    </p>
  <!-- End Default Content REMOVE THIS -->
 Name: <apex:outputText value="{!Opportunity.Name}"/>
</apex:page>

controller extension:

public class OppExt {
    
    public OppExt(){}
    
    public OppExt(ApexPages.StandardController std){
        
    }
    
    @RemoteAction
    public static string getIFrame(string oppId, string docName, string cBox){
        
        String opportunityId = (String)System.JSON.deserialize(oppId, String.class);
        String document = (String)System.JSON.deserialize(docName, String.class);
       
        System.debug('doc name '+document);
        System.debug('opp id '+opportunityId);
        
        String link;
        
        link = 'https://sachintendulkar.na50.visual.force.com/apex/'+document+'?id='+opportunityId;
        System.debug('link is '+link);
        return link;
    } 
    
}