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
OzymandiasOzymandias 

Unexpected behavior when displaying data as PDF

Hi,

 

Hoping someone out there could shed some light on a problem I've been having on a VF page that displays data as pdf.

 

The VF page I'm working on is basically a query tool that retrieves data from the DB and displays them in a report. It should have 2 display modes: display on screen and display as pdf. I've tried to implement it by creating two VF pages using one controller. The 1st page has the form where users input the query criteria, and displays the query results in a pageBlock, and the 2nd page is just a page that displays the query results on a page with renderAs="pdf". The pdf page uses a different collection (a List of Lists) to display the results for pagination purposes.

 

The code looks somewhat like this:

 

 

public class MyController {
    
    public List<DataObject> queryResults {get; private set;}

    public List<List<DataObject>> queryResultsForPDF {get; private set;}

    public void runQuery() {
        ...
        // run SOQL query and marshall results into queryResults
        ...
    }
    
    public PageReference viewAsPDF() {
        runQuery();
        ...
        // split queryResults into separate batches and insert into queryResultsForPDF
        ...
        return Page.MyReportPDF;
    }
}
<!-- MyReport VF Page -->
<apex:page controller="MyController">
...
<apex:commandButton action="{!runQuery}" value="Display on screen" rerender="report_block"/>
<apex:commandLink action="{!viewAsPDF}" value="Display as PDF" target="_blank"/>

<apex:pageBlock id="report_block">
... displays report data from queryResults
</apex:pageBlock>

</apex:page>

<!-- MyReport PDF Page -->
<apex:page controller="MyController" renderAs="pdf">

<apex:pageBlock id="report_block">
... displays report data from pdfQueryResults
</apex:pageBlock>

</apex:page>

 

The report displays fine in the on screen display. However, when I try to do the view as PDF, the data always comes out as empty. I've gone through the debug logs and queryResultsForPDF does get initialized, but for some reason, when the pdf VF page retrieves it, it always comes out as empty (Hope this makes sense)

 

It seems like the controller instance variables I set in viewAsPDF() aren't reflected in the pdf VF page. Can anyone point out what I'm doing wrong?

 

 

chrismclarenchrismclaren

The ID of both page blocks is the same.  Change this:-

<apex:pageBlock id="report_block">
... displays report data from queryResults
</apex:pageBlock>

</apex:page>

<!-- MyReport PDF Page -->
<apex:page controller="MyController" renderAs="pdf">

<apex:pageBlock id="report_block">
... displays report data from pdfQueryResults
</apex:pageBlock>

 

to this :-

 

<apex:pageBlock id="report_block1">
... displays report data from queryResults
</apex:pageBlock>

</apex:page>

<!-- MyReport PDF Page -->
<apex:page controller="MyController" renderAs="pdf">

<apex:pageBlock id="report_block2">
... displays report data from pdfQueryResults
</apex:pageBlock>

 

 

 

 

 

OzymandiasOzymandias

Hi Chris,

 

Do you mean that two separate VF pages sharing the same controller need to have unique component ids between them? In any case, I tried your suggestion, but unfortunately it still showed the same behaviour.

 

I was able to make it work by changing the VF pdf page so that it didn't use an instance variable on the controller anymore. Instead, it called a new controller method that processed queryResults and returned List<List<DataObject>> instead.