itext7pdfhtml

iText7 html to pdf conversion with target-counter having dynamic target value not working


I have been looking into why the page numbers in my generated toc are not working on the latest (3.0.3) version of htmlpdf - even though the release note state that this is now supported.

The devil is in the detail, as noted in the example (https://kb.itextpdf.com/home/it7kb/examples/pdfhtml-support-for-generating-cross-references-for-toc-creation-with-target-counter-target-counters-css-properties) the following works very well:

.preface::before {
       content: target-counter(url('#id1'), page) ' ';
}

But as soon as you change this to something more dynamic, in order to not have to not have to specify an entry for each topic you would like to have in the TOC it fails.

.preface::before {
       content: target-counter(attr(href), page) ' ';
}

As also indicated in the logging by the following line:

WARN com.itextpdf.html2pdf.attach.impl.layout.PageTargetCountRenderer - Cannot resolve target-counter value with given target "attr(href)"

It looks like the "attr(href)" is taken as a literal and not resolved to the context it is being used in, eg. in this case extracting the tag's href value and using that to find the right page number.

Could this be fixed somehow - or can this be covered by some custom coding?

Thanks.


Solution

  • Using the latest version of the renderer (3.0.5 at the time of writing) effectively supports this properly!