8

I am creating a dynamic PDF in ColdFusion and having an issue with "pagebreak". The page in question could have 1 record, or up to 60+ records. Each record is displayed in 2 rows of a table. Some of the returned records are being split between pages (first row is at the end of page one, the second row is the top row of the next).

A sample record in displayed HTML:

<tr>
  <td>Title</td><td>Price</td>
  <td colspan="2">Description</td>
</tr>

Per client request, I am trying to display =< 9 records per page.

Here is a dumbed down sample of something I have tried:

<cfdocument format="PDF">
<cfoutput query = "sqllookup">
<cfset loopcount = loopcount + 1>
<cfif loopcount EQ '9'>
 <cfdocumentitem type="pagebreak" />
<cfelse>
<tr>
  <td>#Title#</td><td>#Price#</td>
  <td colspan="2">#Description#</td>
</tr>
</cfif>
</cfoutput>
</cfdocument>

This does not work, (it only hides the 9th record). I have tried several different ideas, and I am currently stumped. Am I over looking something?

Thanks in advance.

ColdFusion MX 7. (I also ran the hot fix for text cut-off issue. http://kb2.adobe.com/cps/402/kb402093.html)

nope_four
  • 492
  • 1
  • 4
  • 16
  • Not really an answer, but a guess. Have you tried to close the table tag after 9 records, and create the new table for new page? The only issue is that for the long text you'll need less rows, this can require some Len()-trickery. – Sergey Galashyn Jan 21 '10 at 23:15

4 Answers4

11

You are hiding the 9th record because you are choosing between displaying it and showing it:

if 9th record
    break page
else
    show record
end if

What you want is more like:

<cfoutput query = "sqllookup">
    <!--- this is the 9th row, because 9 mod 9 is 0 --->
    <cfif not sqllookup.currentrow mod 9>
        <cfdocumentitem type="pagebreak" />
    </cfif>
    <tr>
        <td>#Title#</td><td>#Price#</td>
        <td colspan="2">#Description#</td>
    </tr>
</cfoutput>
Ben Doom
  • 7,865
  • 1
  • 27
  • 30
1

After wrestling with this issue on and off for several months, I've discovered that wrapping the contents of a td with a div (ie.<tr><td><div>Cell Contents</div></td></tr>) will prevent a page-break inside the row. With this setup, a page-break that would normally split the row between pages will instead fall before the row, creating a little extra whitespace at the end of the first page and placing the row at the beginning of the next page.

Note about rows with multiple cells: A single td-nested div is sufficient to cause the above behavior for the whole row.

<tr>
    <td>Blah blah blah blah blah</td>
    <td>Gnar gnar gnar gnar gnar</td>
    <td><div>Soda POP soda POP soda POP</div></td>    <!--- the fix --->
    <td>Stellar!</td>
</tr>
Jonathan Wilson
  • 4,138
  • 1
  • 24
  • 36
  • In my tests with CF10 (although i believe the CFdocument engine hasn't changed since CF8), the renderer seems to keep rows together if they are three or four lines tall, and splits them at some point. This DIV solution worked for slightly taller rows - maybe the algorithm weights block-level elements and tries to keep them together, so nesting tables inside your tables might discourage page breaks (ouch!) – andrew lorien May 20 '13 at 05:20
0

Try adding a style="page-break-inside: avoid;" to any element that you don't want to be split between 2 pages.

For instance,

<tr style="page-break-inside: avoid;">
   <td>#Title#</td><td>#Price#</td>
   <td colspan="2">#Description#</td>
</tr>
Eddie
  • 5,050
  • 8
  • 37
  • 46
0

For me, after trying all the tricks and forum hints etc. - the only thing that worked in cf8 for larger blocks of code (including images, tables, div block etc.) is:

  • wrap the non page breaking part with

    <div>...</div>

  • but a super simple <br> after it in a new line (in code) e.g.

    <div> ... your stuff ... </div> <br>

that worked, hell knows why...

Raffael Meier
  • 189
  • 1
  • 4