• sfdc2016
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
My requirement: 
step 1: completed 

I have two visualforce pages, page 1 has a drop down select field, a checkbox and a button. When i click the button, page 2(rendered as pdf) is displayed as an iframe.

step 2: when i select the checkbox in page 1, I should be able to highlight the apex:outputText field in the iframe.

Any thoughts or help on this would be greatly appreciated.

Thanks in advance!

Page 1:
<apex:page standardController="Opportunity" extensions="OppExt">
<apex:includeScript value="https://code.jquery.com/jquery-1.9.1.min.js"/>
<apex:stylesheet value="{!$Resource.customCSS}" /> 
<html>
<script type="text/javascript"> 
function generateIFrame() {
var oppId = document.getElementById('{!$Component.oId}').innerHTML;

console.log(oppId);
var doc = document.getElementById("volvo").value;
var cBox = document.getElementById("check").checked;
Visualforce.remoting.Manager.invokeAction(
                    '{!$RemoteAction.OppExt.getIFrame}', JSON.stringify(oppId), JSON.stringify(doc), JSON.stringify(cBox),
                    function(result, event) {
                    if(event.type === 'exception') {
                            alert('In exception');
                        }
                        else if (event.status) {
                     var elmnt = document.getElementById("if");
                            elmnt.src = result; 

                       }
                        else {
                            alert('In else');
                            console.log(event.message);
                        }
                    });
                }
        </script> 



  <!-- Begin Default Content REMOVE THIS -->

  <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>

    <span>
        <apex:iframe id="if"/>
    </span>


  </html>
</apex:page>

Page 2 (iframe):
<apex:page renderAs="pdf" standardController="Opportunity" extensions="OppExt" docType="html-5.0" showHeader="false" sidebar="false" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false" >

     <apex:stylesheet value="{!$Resource.customCSS}" />  
<apex:includeScript value="https://code.jquery.com/jquery-1.9.1.min.js"/>

    <h1 id="header">Congratulations</h1>
  <p id="p1">
      This is your new Page: volvo
    </p>

    <apex:form >
 Name: <apex:outputText id="oppName" value="{!Opportunity.Name}"/>
        </apex:form>

</apex:page>
Controller Extension:
global class OppExt {

    public static String chBox;
    public string testBox = 'test';
    public OppExt(){}

    public OppExt(ApexPages.StandardController std){

    }

    @RemoteAction
    global 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);
        chBox = (String)System.JSON.deserialize(cBox, String.class);

        String link;


        link = 'https://na50.visual.force.com/apex/'+document+'?id='+opportunityId;

        return link;
    } 

}


 

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;