Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create TOC or index using the Flying Saucer project?

I convert HTML files to PDF format using The Flying Saucer Project. This are documents containing repetitive information - premises and their addresses, let's call them elements. At the end of a document I need to create an index. Each index entry should have a page number referring to the page where element was added. The number of elements that can fit on one page will vary.

How can I create a document index? Or how can I get notified while library adds certain type of HTML element to the PDF document?

like image 343
Daniel Stefaniuk Avatar asked Oct 11 '10 10:10

Daniel Stefaniuk


2 Answers

Try this:

In CSS

ol.toc a::after {  content: leader('.') target-counter(attr(href), page);}

In HTML

<h1>Table of Contents</h1>
<ol class='toc'>
<li><a href=\"#chapter1\">Loomings</a></li>
<li><a href=\"#chapter2\">The Carpet-Bag</a></li>
<li><a href=\"#chapter3\">The Spouter-Inn</a></li>
</ol>

<div id="chapter1">Loomings</div>
like image 139
Rodonako Avatar answered Sep 30 '22 02:09

Rodonako


I found possible answer. You have to start playing with org.xhtmlrenderer.render.BlockBox class. A method public void layout(LayoutContext c, int contentStart) is used to place properly any HTML element in the PDF document. This method iterates through an element a few times. After the last iteration a valid page number is set.

If you mark an element you want to index, by for example using a class attribute, then you can get a page number using following code:

String cssClass = getElement().getAttribute("class");
if(!cssClass.equals("index")) {
    int pageNumber = c.getRootLayer().getPages().size();
    /* ... */
}
like image 35
Daniel Stefaniuk Avatar answered Sep 30 '22 04:09

Daniel Stefaniuk