How to insert TOC section numbers into section headers with PDF export?

I'm sure everyone has faced this problem, and hopefully it's been solved. When exporting to pdf, confluence automatically creates section numbers in the table of contents. I want those auto generated section numbers to be inserted at the beginning of the actual section titles in the document. Surely this is possible? Any help is greatly appreciated! -Rick

1 answer

This must be defined in the PDF export stylesheet with the help of CSS counters. A quick example would look like this:

body {
counter-reset: levelA;
}
h2:before {
content: counter(levelA) ". ";
counter-increment: levelA;
}
h2 {
counter-reset:levelB;
}
h3:before {
content: counter(levelA) "." counter(levelB) ". ";
counter-increment: levelB;
}

This almost works, but still results in odd numbering. Thanks for the info though, this is a starting point.

Yeah, this fails in situations where h1-h6 markings are also used on the pages itself.

If so, just exclude those headings and include the page title instead.

How can those headers be detected (and thus excluded)?

Use .pagetitle h1 to only include the page title but not the page headings.

I have a working example of plain html with levels of headings that get numbered (try here https://jsfiddle.net/gudj/37pcs28x/ )

body {counter-reset: lvl1;}
.pagetitle>h1   {counter-reset: lvl2;}
.pagetitle>h2   {counter-reset: lvl3;}
.pagetitle>h3   {counter-reset: lvl4;}
.pagetitle>h1 {counter-increment: lvl1;}
.pagetitle>h2 {counter-increment: lvl2;}
.pagetitle>h3 {counter-increment: lvl3;}
.pagetitle>h4 {counter-increment: lvl4;}
.pagetitle>h1:before {content: counter(lvl1) ". "}
.pagetitle>h2:before {content: counter(lvl1) "." counter(lvl2) ". "}
.pagetitle>h3:before {
content: counter(lvl1) "." counter(lvl2) "." counter(lvl3) ". "
}
.pagetitle>h4:before {
content: counter(lvl1) "." counter(lvl2) "." counter(lvl3) "." counter(lvl4) ". "
}

But when I use that CSS in "pdf export CSS" I get a couple of problems: The counter increment only works on the h1 level. For the lower levels (h2-h4) there is only one increment so they all show "1" (unless the parent level is missing in which case it becomes "0"). This one is quite annoying since I know the header levels are there since the content gets inserted. Could this be caused by counter reset caused by additional h1 tags? But why would they be wrapped in pagetitle class if they are not themselves page titles?

The second problem is discussed above by Heller etc - but I am not able to use pagetitle to filter out the headings within each page, the class seems to get matched also for some "intra-page" headlines, not just the page title. I've tried using other classes/ids from the displayed HTML but it seems only pagetitle is used in the pdf export...

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Apr 13, 2018 in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

1,891 views 25 10
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you