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
Andreas Wolf 11Andreas Wolf 11 

Get PDF Visualforce Content for 2nd VF Page with same Controller from 1st VF Page

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..
Best Answer chosen by Andreas Wolf 11
Michael PaulsonMichael Paulson
Good news, Andreas - I got this to work! I might need to see more of your page code to see if it's the same thing that was happening to me. But I'll take a shot in the dark.

I was running my save function from an apex:actionFunction and needed to set rerender="" in order for some params to pass to the controller. It turns out that the rerender was getting in the way of the saving of the second page. Once I removed the rerender="" and moved the params to an apex:inputHidden attribute and set the value with some JS (maybe you won't have to worry about this part):

function confirm() {
   var answer = confirm('Yes or No?');
   jQuery('[id$=hiddenVal]').val(answer);
   doSave(); //this is the name of the actionFunction
}

Here's the hidden input in the page, if it helps:
<apex:inputHidden value="{!varForSave}" id="hiddenVal"/>

Everything is working as expected now - page state is maintained and the second page is saved. It turns out that the rerender doesn't just get in the way of returning new page references, but it also seems to prevent a page reference from being saved as an attachment.

Hopefully, this helps you get started down the right path. Let me know if I can attempt to offer any more assistance. Good luck!

All Answers

Narveer SinghNarveer Singh
Hi Andreas,

you can try folowing vf code for second page :

<apex:page controller="Controller" contentType="render as PDF" showHeader="true">
    Column Name i.e Type,Completion Date,Owner
    <apex:repeat value="{!List}" var="tskact">
          Column Values i.e {!tskact.type},{!tskact.completionDatestr},{!tskact.owner}
    </apex:repeat>
</apex:page>

and While on Click button like Genrate PDF from second page using same controller and List or data:

public PageReference exportToPDF() {
                        
        return Page.YourPageName;
    }

Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution.Let me know if anything else.

Best Regards
Narveer



 
Michael PaulsonMichael Paulson
I'm running into the same issue. Narveer was pointing out a known issue (https://success.salesforce.com/issues_view?id=a1p30000000T3HFAA0), but it looks as if that has been resolved. Regardless, the workaround didn't work for me and doesn't seem to have worked for Andreas either. Have you had any success on this yet?
Andreas Wolf 11Andreas Wolf 11
Hello guys,

sorry to disappoint you, but we had no success on this matter yet. Doesn't matter how I try, the controller always converts the 1st visualforce page, but I need the 2nd visualforce page converted.

Best regards and thanks for your answers so far!
Andreas 
Michael PaulsonMichael Paulson
Good news, Andreas - I got this to work! I might need to see more of your page code to see if it's the same thing that was happening to me. But I'll take a shot in the dark.

I was running my save function from an apex:actionFunction and needed to set rerender="" in order for some params to pass to the controller. It turns out that the rerender was getting in the way of the saving of the second page. Once I removed the rerender="" and moved the params to an apex:inputHidden attribute and set the value with some JS (maybe you won't have to worry about this part):

function confirm() {
   var answer = confirm('Yes or No?');
   jQuery('[id$=hiddenVal]').val(answer);
   doSave(); //this is the name of the actionFunction
}

Here's the hidden input in the page, if it helps:
<apex:inputHidden value="{!varForSave}" id="hiddenVal"/>

Everything is working as expected now - page state is maintained and the second page is saved. It turns out that the rerender doesn't just get in the way of returning new page references, but it also seems to prevent a page reference from being saved as an attachment.

Hopefully, this helps you get started down the right path. Let me know if I can attempt to offer any more assistance. Good luck!
This was selected as the best answer
Andreas Wolf 11Andreas Wolf 11
Micheal you are awesome! Thank you very much! I never would have thought that the rerender attribute in a button or an action function could be the problem.

I spent hours trying to figure out whats going wrong and I just had to remove one god damn attribute from my saving button.

 
Perfect MathenjwaPerfect Mathenjwa
Hi @Michael Paulson I have a similar requirement to your but I just can't get it to work.
  • I have  2 visualforce pages that are sharing a controller and controller extension. 
  • The first page I need to capture user input through apex:inputText.
  • On the second page I need to save the entered values as an excel document, so I have "contentType="application/vnd.ms-excel#SomeName.xls" .
  • I am using apex:actionFunction that I trigger on javascript the pageReference action on the controller extension.
For the life of me I can't get it to work, the excel page is not capturing the entered input on the first page, please help and show me how you solved yours with some code examples.