• Michael Paulson
  • NEWBIE
  • 40 Points
  • Member since 2016

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 2
    Likes Given
  • 1
    Questions
  • 6
    Replies
Hello guys,

I have 2 visualforce pages and 1 apex controller extension for both. On the first page I capture some information that is displayed on the second page (renderAs PDF). 

There is no problem showing a pdf preview with the following code:
public PageReference showPDF()
{
  PageReference test = Page.Sales_Offer_PDF;
  test.setRedirect(false);
  test.getParameters().put('Id', o.Id);
  test.getParameters().put('pdf','true');

  return test;
}
Though its still the first page in the url.

But if I want to save the second page as a pdf file, it completely ignores the second page and tries to get the content from the first page.
Following Code:
public void savePDF()
    {
        PageReference pdf = new PageReference('/apex/Sales_Offer_PDF?id='+o.Id+'&pdf=true');
        
        //PageReference pdf = Page.Sales_Offer_PDF;               Both ways dont work...
        //pdf.getParameters().put('Id', o.Id);
        //pdf.getParameters().put('pdf','true');

        Blob body;

        ContentVersion v = new ContentVersion();
        try {
            body = pdf.getContent();
        } catch (VisualforceException e) {
            body = Blob.valueOf('Some Text');
            System.debug('### Exception savePDF: '+e);
        }
        v.versionData = body;
        v.title = 'Offer ('+Date.today().format()+').pdf';
        v.pathOnClient ='/Offer ('+Date.today().format()+').pdf';
        v.ProtectedFile__c = false;        

        insert v;

        ID cdId = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE ID =: v.Id].ContentDocumentId;

        ContentDocumentLink cdl = new ContentDocumentLink();
        cdl.ContentDocumentId = cdId;
        cdl.ShareType = 'V'; 
        cdl.LinkedEntityId = o.Id;
        cdl.Visibility = 'AllUsers'; 

        insert cdl;
    }
Btw. Im developing for Lightning Experience right now.
Can someone help me to save the second vf page as a pdf file. I need the same controller to keep the same view state..
I'll try to keep this simple as I suspect the answer might be something where there is a known workaround, but I just can't find that.
I am accepting input from the user and kicking it out in a PDF. This is being done through an iFrame (I'm also using the same code to save a PDF). So the relevant parts are below:
Apex:
PageReference ref = Page.myPDF;
ref.getParameters().put('id', account);
Blob contentData = ref.getContentAsPDF(); //THIS LINE IS WHERE THE PROBLEM COMES IN
pageRefPDF = 'data:application/pdf;base64,' + EncodingUtil.base64Encode(contentData);

Visualforce:
<apex:iframe id="frame" src="{!pageRefPDF}"/>

I'm letting the user add and remove rows of data and I'm confident I am doing this correctly because I have traced the error to a particular variable and am running debug lines on that variable right before the problem line (above). When adding a new row and saving, the debug shows the variable equaling what it should (0,1,2,3,4,5), but when I save and kick out the PDF, I am getting a 'Subscript value 6 not valid. Must be between 0 and 5' error. It is also working if I comment out the getContentAsPDF line and display the page directly rather than through an iFrame (but I need to do this for a couple reasons). Is there a known issue and workaround having to do with the getContentAsPDF method that I should be aware of?

Sorry to be vague, and let me know if more of my code would help, but I'm assuming, given the above reasoning, that the issue is related to some Apex quirk rather than something I'm doing incorrectly. Thanks in advance!
I am getting below error
User-added image

but my application has 
User-added image

May I know why am not able to pass this challenge ?
 
I'll try to keep this simple as I suspect the answer might be something where there is a known workaround, but I just can't find that.
I am accepting input from the user and kicking it out in a PDF. This is being done through an iFrame (I'm also using the same code to save a PDF). So the relevant parts are below:
Apex:
PageReference ref = Page.myPDF;
ref.getParameters().put('id', account);
Blob contentData = ref.getContentAsPDF(); //THIS LINE IS WHERE THE PROBLEM COMES IN
pageRefPDF = 'data:application/pdf;base64,' + EncodingUtil.base64Encode(contentData);

Visualforce:
<apex:iframe id="frame" src="{!pageRefPDF}"/>

I'm letting the user add and remove rows of data and I'm confident I am doing this correctly because I have traced the error to a particular variable and am running debug lines on that variable right before the problem line (above). When adding a new row and saving, the debug shows the variable equaling what it should (0,1,2,3,4,5), but when I save and kick out the PDF, I am getting a 'Subscript value 6 not valid. Must be between 0 and 5' error. It is also working if I comment out the getContentAsPDF line and display the page directly rather than through an iFrame (but I need to do this for a couple reasons). Is there a known issue and workaround having to do with the getContentAsPDF method that I should be aware of?

Sorry to be vague, and let me know if more of my code would help, but I'm assuming, given the above reasoning, that the issue is related to some Apex quirk rather than something I'm doing incorrectly. Thanks in advance!
Hello guys,

I have 2 visualforce pages and 1 apex controller extension for both. On the first page I capture some information that is displayed on the second page (renderAs PDF). 

There is no problem showing a pdf preview with the following code:
public PageReference showPDF()
{
  PageReference test = Page.Sales_Offer_PDF;
  test.setRedirect(false);
  test.getParameters().put('Id', o.Id);
  test.getParameters().put('pdf','true');

  return test;
}
Though its still the first page in the url.

But if I want to save the second page as a pdf file, it completely ignores the second page and tries to get the content from the first page.
Following Code:
public void savePDF()
    {
        PageReference pdf = new PageReference('/apex/Sales_Offer_PDF?id='+o.Id+'&pdf=true');
        
        //PageReference pdf = Page.Sales_Offer_PDF;               Both ways dont work...
        //pdf.getParameters().put('Id', o.Id);
        //pdf.getParameters().put('pdf','true');

        Blob body;

        ContentVersion v = new ContentVersion();
        try {
            body = pdf.getContent();
        } catch (VisualforceException e) {
            body = Blob.valueOf('Some Text');
            System.debug('### Exception savePDF: '+e);
        }
        v.versionData = body;
        v.title = 'Offer ('+Date.today().format()+').pdf';
        v.pathOnClient ='/Offer ('+Date.today().format()+').pdf';
        v.ProtectedFile__c = false;        

        insert v;

        ID cdId = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE ID =: v.Id].ContentDocumentId;

        ContentDocumentLink cdl = new ContentDocumentLink();
        cdl.ContentDocumentId = cdId;
        cdl.ShareType = 'V'; 
        cdl.LinkedEntityId = o.Id;
        cdl.Visibility = 'AllUsers'; 

        insert cdl;
    }
Btw. Im developing for Lightning Experience right now.
Can someone help me to save the second vf page as a pdf file. I need the same controller to keep the same view state..
As per Summer release 15, if someone calling this method will be treated as call out, Does it means that it has timeout of 2 minutes ?
What if customer using getContentAsPDF() method to generate the PDF , Did they get any impact (If they are generating large PDF's)?
Hi All, 

Need Help ! I have a requirement where I have to implement a search functionality. On enter a particular term it will search records and then return results. Also I have to add a "No item found" Option in a list of records. I have used StandardSetController for this , but when I try to add a dummy record named " No Item Found " in the List and then pass the list to the instance of StandardSetController, I am getting error "Modified rows exist in the records collection!" on the VF page. I understand the meaning of the error but can anybody suggest me how should this can be accomplished.

Thanks in advance !
Hello guys,

I have 2 visualforce pages and 1 apex controller extension for both. On the first page I capture some information that is displayed on the second page (renderAs PDF). 

There is no problem showing a pdf preview with the following code:
public PageReference showPDF()
{
  PageReference test = Page.Sales_Offer_PDF;
  test.setRedirect(false);
  test.getParameters().put('Id', o.Id);
  test.getParameters().put('pdf','true');

  return test;
}
Though its still the first page in the url.

But if I want to save the second page as a pdf file, it completely ignores the second page and tries to get the content from the first page.
Following Code:
public void savePDF()
    {
        PageReference pdf = new PageReference('/apex/Sales_Offer_PDF?id='+o.Id+'&pdf=true');
        
        //PageReference pdf = Page.Sales_Offer_PDF;               Both ways dont work...
        //pdf.getParameters().put('Id', o.Id);
        //pdf.getParameters().put('pdf','true');

        Blob body;

        ContentVersion v = new ContentVersion();
        try {
            body = pdf.getContent();
        } catch (VisualforceException e) {
            body = Blob.valueOf('Some Text');
            System.debug('### Exception savePDF: '+e);
        }
        v.versionData = body;
        v.title = 'Offer ('+Date.today().format()+').pdf';
        v.pathOnClient ='/Offer ('+Date.today().format()+').pdf';
        v.ProtectedFile__c = false;        

        insert v;

        ID cdId = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE ID =: v.Id].ContentDocumentId;

        ContentDocumentLink cdl = new ContentDocumentLink();
        cdl.ContentDocumentId = cdId;
        cdl.ShareType = 'V'; 
        cdl.LinkedEntityId = o.Id;
        cdl.Visibility = 'AllUsers'; 

        insert cdl;
    }
Btw. Im developing for Lightning Experience right now.
Can someone help me to save the second vf page as a pdf file. I need the same controller to keep the same view state..
Hi All, 

Need Help ! I have a requirement where I have to implement a search functionality. On enter a particular term it will search records and then return results. Also I have to add a "No item found" Option in a list of records. I have used StandardSetController for this , but when I try to add a dummy record named " No Item Found " in the List and then pass the list to the instance of StandardSetController, I am getting error "Modified rows exist in the records collection!" on the VF page. I understand the meaning of the error but can anybody suggest me how should this can be accomplished.

Thanks in advance !