2

I'm using XSL-FO to generate an account statement print out. The PDF is actually just a simple table with a simple header on every page. The difficulty is that I have to display transaction volumes per page, e.g.

Page 1

+------------------------------+-----------+-----------+---------------------+
| Text                         | Credit    | Debit     | Balance             |
+------------------------------+-----------+-----------+---------------------+
| Previous month               |           |           |           (*1) 1000 |
| abc                          |      1000 |           |                2000 |
| abc                          |           |       500 |                1500 |
| abc                          |           |       200 |                1300 |
| ...                          |           |           |                     |
| Carry over                   | (*2) 1000 |  (*3) 700 |           (*4) 1300 |
+------------------------------+-----------+-----------+---------------------+

Page 2

+------------------------------+-----------+-----------+---------------------+
| Text                         | Credit    | Debit     | Balance             |
+------------------------------+-----------+-----------+---------------------+
| Previous page                | (*2) 1000 |  (*3) 700 |           (*4) 1300 |
| abc                          |      1000 |           |                2300 |
| abc                          |           |       500 |                1800 |
| abc                          |           |       200 |                1600 |
| ...                          |           |           |                     |
| Carry over                   | (*2) 2000 | (*3) 1400 |           (*4) 1600 |
+------------------------------+-----------+-----------+---------------------+

Here are some explanations:

  1. This is the previous month's balance. It's pre-calculated and well-known as an XSL variable. No problem with that, that's a regular header (only on the first page)
  2. This value is calculated on a per-page basis. It sums up all credit amounts on the same page. I can't calculate that myself, as I don't know when XSL-FO will do the page break. So I imagine XSL-FO must do the calculation for me. The sum at the bottom of a page is the same as the value at the top of the subsequent page.
  3. This value is the same as 2, only for debit amounts.
  4. This value is just the last transaction's balance at the bottom of a page. That value is repeated at the top of the next page.

How can I do these calculations with XSL-FO?

See also this related question: How to display one or the other information depending on the page number in XSL-FO?

Community
  • 1
  • 1
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • Have you looked at XSLT yet? You can write an XSLT stylesheet that contains/generates your FO elements, and it allows you to conduct any reasonable calculations. – Dabbler Oct 28 '11 at 15:41
  • @Dabbler: I'm using XSLT, I'm just not sure how I can make XSL-FO use calculated values on a per-page basis. I guess Michael's answer is valuable – Lukas Eder Oct 29 '11 at 10:51

2 Answers2

1

Try "table markers": http://www.w3.org/TR/xsl/#fo_retrieve-table-marker.

In XSLT for each row inject a marker with the sum. Then let the engine select a marker to substitute for the fo:retrieve-table-marker in table header or footer. The idea is that proper marker will be selected at rendering time depending on the marker's position on the page and @retrieve-position and @retrieve-boundary on the fo:retrieve-table-marker.

1

Unfortunately, (at the time when I answered this question, it's no longer true) fop doesn't implement <fo:retrieve-table-marker/> from what I have found out. Instead, this solution here worked for me:

How to display one or the other information depending on the page number in XSL-FO?

It involves creating a separate table outside of the <fo:flow/> that displays the table header using <fo:retrieve-marker/> elements.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • This may work for table header if the table is the first thing in the flow, but not for the table footer: we do not know beforehand where the table footer appears on the page. – Mike Sulyaev Oct 31 '11 at 10:23
  • @MichaelSulyaev: In principle, you're right. But my layout is expected to contain exactly 37 rows of fixed height. So I'm lucky :-) I can do the same trick with the footer also, and move the table footer into the `` section – Lukas Eder Oct 31 '11 at 11:11
  • @MichaelSulyaev... Hmm, actually I just now realise that the whole question becomes somewhat obsolete, as with this sort of fixed layout, I can do some calculations in XSL already before letting FOP run the layout... – Lukas Eder Oct 31 '11 at 12:25
  • 2
    Apache FOP now _does_ implement `retrieve-table-markers` with some limitations. See here: https://stackoverflow.com/a/31012095/3000316. It worked for me really well! – Monkey Supersonic Nov 30 '22 at 11:32
  • @MonkeySupersonic: Thanks for the hint. I've added the link to my answer – Lukas Eder Nov 30 '22 at 11:37