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
cmarkcmark 

apex to pdf - page headers

Hello.  I am currently doing pdf generation using an external web service that i wrote - but i'm looking to move the functionality into sfdc using the new apex to pdf.  The pdf that i generate has multiple pages - all with a common header (with images).  I've seen from other threads that this isn't quite supported in any automated fashion (http://community.salesforce.com/sforce/board/message?board.id=Visualforce&thread.id=3205).  So I guess the alternative is to foce page breaks and re-draw the header. 

My pdf will have either one or two dynamic pages (based on the number of line items), and 4 static pages (terms of sale, etc).  So the static pages are easy, but my question is, can I conditionally draw the second dynamic page?  That is, if I have more than 6 line items, draw the second dynamic page.  Otherwise, go straight to the static pages.  Is this possible using the VF markup?  Are there any samples with forced page breaks?  I found one thread where a page break is forced, but there's no conditional logic around it (http://community.salesforce.com/sforce/board/message?board.id=Visualforce&message.id=3880).  Do I just throw all of the necessary logic into the apex:repeat block?

Thanks
Chris
Best Answer chosen by Admin (Salesforce Developers) 
JeremyKraybillJeremyKraybill
I know this is an old thread, but I just was looking for a solution to the same issue, and was able to come up with a solution. It's a bit hacky but it works, and in looking through the threads I couldn't come up with any workable alternative.

I have a PDF which is built based on a StandardSetController from a list view, where the user is able to select any number of items to print.

Luckily, these items all render in about the same height, so just like you, we wanted to paginate for every 5 items, with a repeated header using standard pageBlock elements.

Steps to get your PDF to do this:

1. In your list which your page is looping through, you need to get each element in the list to know what rank it is in the list. The hacky part: for us, we just temporarily use a numeric field on the custom object we're looping through, and assign it its place in the loop. Controller code for this:

Code:
for (Integer i = 0; i < list.size(); i++) {
 list[i].Some_Numeric_Field__c = i;
}

(Obviously you don't want to call an "update" on the list after doing this!!)

2. In your page code, have your <apex:repeat> tag iterate over header / item / footer for EVERY item. Put conditional "rendered" attributes on your header and footer. Unfortunately, since apex doesn't appear to support the modulo (%) operator, the only way I found to do this was to check for every possible iteration that the header and footer need to be rendered on:

Code:
<apex:repeat value="{!list}" var="item">
 <apex:outputPanel rendered="{!item.Some_Numeric_Field__c == 0 || item.Some_Numeric_Field__c == 5 || item.Some_Numeric_Field__c == 10}">
  (your HTML header here -- you could use a pageBlock instead of outputPanel if it was a full pageBlock header, etc.)
 </apex:outputPanel>

 {!item.Whatever_You_Want_To_Output__c} (obviously could be anything, for us it was pageBlockSections)
 
 <apex:outputPanel rendered="{!item.Some_Numeric_Field__c == 4 || item.Some_Numeric_Field__c == 9 || item.Some_Numeric_Field__c == 14}">
  (your HTML footer here, if any)
  <div style="page-break-after: always;">&nbsp;</div>
 </apex:outputPanel>
</apex:repeat>

 I hope that helps someone else facing the same issue I was!
 
Jeremy Kraybill
Austin, TX

All Answers

p1_dfsp1_dfs
we are waiting for the same capability. If someone can provide some help, it be greatly appreciated.
JeremyKraybillJeremyKraybill
I know this is an old thread, but I just was looking for a solution to the same issue, and was able to come up with a solution. It's a bit hacky but it works, and in looking through the threads I couldn't come up with any workable alternative.

I have a PDF which is built based on a StandardSetController from a list view, where the user is able to select any number of items to print.

Luckily, these items all render in about the same height, so just like you, we wanted to paginate for every 5 items, with a repeated header using standard pageBlock elements.

Steps to get your PDF to do this:

1. In your list which your page is looping through, you need to get each element in the list to know what rank it is in the list. The hacky part: for us, we just temporarily use a numeric field on the custom object we're looping through, and assign it its place in the loop. Controller code for this:

Code:
for (Integer i = 0; i < list.size(); i++) {
 list[i].Some_Numeric_Field__c = i;
}

(Obviously you don't want to call an "update" on the list after doing this!!)

2. In your page code, have your <apex:repeat> tag iterate over header / item / footer for EVERY item. Put conditional "rendered" attributes on your header and footer. Unfortunately, since apex doesn't appear to support the modulo (%) operator, the only way I found to do this was to check for every possible iteration that the header and footer need to be rendered on:

Code:
<apex:repeat value="{!list}" var="item">
 <apex:outputPanel rendered="{!item.Some_Numeric_Field__c == 0 || item.Some_Numeric_Field__c == 5 || item.Some_Numeric_Field__c == 10}">
  (your HTML header here -- you could use a pageBlock instead of outputPanel if it was a full pageBlock header, etc.)
 </apex:outputPanel>

 {!item.Whatever_You_Want_To_Output__c} (obviously could be anything, for us it was pageBlockSections)
 
 <apex:outputPanel rendered="{!item.Some_Numeric_Field__c == 4 || item.Some_Numeric_Field__c == 9 || item.Some_Numeric_Field__c == 14}">
  (your HTML footer here, if any)
  <div style="page-break-after: always;">&nbsp;</div>
 </apex:outputPanel>
</apex:repeat>

 I hope that helps someone else facing the same issue I was!
 
Jeremy Kraybill
Austin, TX
This was selected as the best answer
SFDCDev7SFDCDev7

Hi Jeremy,

 

I had a similiar kind of requirement. Your post helped me to achieve the requirement.


Thanks a Lot.

 

Regards,

AJ

Bodhtree1Bodhtree1

Hi,,,

 

Can u give me suggestions ,how to add the same header and footers in mutiple pages that is generated as a pdf.

 

Thanking you,

Vamsi Krishna

SFDCDev7SFDCDev7

Hi Vamsi,

 

Create a temporary Integer variable in the List which you iterate and increment it for each record.

 

In the visualforce put the header content in the output panel and render it by using the count variable.

 

Check the above sample code which is posted.

 

Regards,

AJP

Virendra NarukaVirendra Naruka

Use following style 

<style>

@page

{ size:landscape;

  @top-right { /* page numbers */

      content: "Page " counter(page);

  }

 @top-left { /* Header contents*/

      content: "Page Header";

  }

 

 @bottom-left { /* Footer contents*/

      content: "Page Footer";

  }

}

 

.bold{ font-weight: bold; }

</style>

DevNVDevNV

Does this work for you when you add the style details into the VF page directly? 

 

I can only get it to work if I put it into a css file as a Static Resource, however I want to include some dynamic data like record name in the header, so including it in the page itself would let me do that.

SIVASASNKARSIVASASNKAR

Hi, every one thanks for solution, but i have one problem, if we are displaying the quote line item values in table it  has more values then the values will present next page right, but i want the table header for next page values also please help me urgent.