How to use CSS to create space pages as numbered headings on PDF export
Asked by Jim Blinkhorn:

When exporting to PDF I am trying to number my exported pages as headings in a manner which will match a ToC. I have added the following CSS to the Global PDF StyleSheet:  
body

{ counter-reset: h2counter; }

h1

{ counter-reset: h2counter; }

h2:before

{ content: counter(h2counter) ".\\0000a0\\0000a0"; counter-increment: h2counter; counter-reset: h3counter; }

h3:before

{ content: counter(h2counter) "." counter(h3counter) ".\\0000a0\\0000a0"; counter-increment: h3counter; }

This is not functioning correctly on anything other than H2 titles (H1 are excluded to prevent the master page being numbered). Currently I receive export similar to the below:

Confluence Space-

Master page

Page 1

Nested page second level 1

Nested page second level 2

Nested page third level

Nested page fourth level

Page 2

PDF Export-

{ToC}

Master page title

1. First level heading one

1.1 Second level heading one

1.1 Second level heading two  
Third level heading one

Fourth level heading one

2. First level heading two

I understand I have only implemented CSS for numbering of effectively 2 tiers of headings at this stage, I wanted to confirm functionality and it failed. The main issue is the lack of incremental numbering on the second level headings, if I can resolve this I can work on implementing the same fix for third tier onwards, any suggestions would be greatly appreciated.


Answers:
author: Steffen Heller
body: @Jim:

I now fully understand your question, I can totally reproduce it and I get exactly the same result ;-(   
In other words: It can't get that second level to increment either.

The code I posted before is the code used in the confluencedefaultpdf.css, is the basis for the default toc of a space export and seems to work everywhere except for what you want.

No idea, if I overlooked something real simple but I am at my wits end.

comments: author: Jim Blinkhorn
body: Thanks for all your help Steffen. It boggles my mind a little that the ToC is able to successfully generate it's numbering based on the provided CSS but the numbering will not directly apply to the headings themselves. Presumably there is a difference in the way the CSS is handled within the body of the export, but I can't for the life of me tell what it is.

I have a support request lodged with Atlassian, part of their suggestion for resolution was to post a question here; hopefully someone happens upon it who has an idea why the two areas behave differently with the same code. Otherwise, if support get back to me with a resolution I will post the result here.


author: Jim Blinkhorn
body: Just a quick update - I'm still in discussion with Atlassian support who have been unable to provide me we a resolution thus far. They have acknowledged that there is an issue with the CSS/CSS handling, however provided answers to a non-issue; I am waiting on further comment or a resolution.


author: Jim Blinkhorn
body: As suspected, this is a fault with Confluence. If you are interested in seeing this corercted, please vote on the issue [here](https://jira.atlassian.com/browse/CONF-33594).


author: Steffen Heller
body: As I said your question is quite complicated but

* if I create a single page with a number of h1 and h2 headings and then do a page export of this page the headings of the exported file have a numbering in the form 1., 1.1., 1.2., 2., 2.1., 2.2. ....
* if I do a space export of a number of different pages then again the automatically created toc has this form of correct numbering

I tried both before I answered.

But there is quite a lot of points where things can go wrong. For example I haven't fully understood

* if you do a space export or a page export,
* if you are talking about the toc of a space export or the {toc} macro
* what you exactly mean with "Master page"
* where do you want that numbering, in the toc, before the headings or both
* if you are aware that heading numbers are incremented when exported (h2 becomes h3 and so on)

In short, I think it will work but it is complicated.

comments: author: Steffen Heller
body: I just recognized you are using OnDemand. Not sure if it is the same there or not...


author: Jim Blinkhorn
body: Thanks for your effort thus far Steffen, I'm unsure myself if the export to PDF operates in the same manner for the different instance types.

1. This is for a space export, none of the pages include headings, I am reliant on the page titles to generate headings in the document and have segmented my document into pages for this purpose.

2. I am referring to the space export ToC.

3. I am referring to the page used as a container for my 'document', ie the highest level page that all others sit under as sub pages.

4. The ToC numbers automatically, I am attempting to have the numbering added to the headings that generate from the page titles, based on their level in the space structure.

5. I was not aware that headings increment on export... Seemingly this isn't the case though as my exclusion of h1 results in the 'master' page not being numbered (unless it is incremented from 'title' to h1).

You're not wrong when you say this is complicated!


author: Steffen Heller
body: I don't fully understand your question but, as regards, the lack of incremental numbering try the following.

```
body { counter-reset: h2counter; }h2:before { content: counter(h2counter) ".\0000a0\0000a0"; counter-increment: h2counter; }h2 { counter-reset: h3counter;}h3:before { content: counter(h2counter) "." counter(h3counter) ".\0000a0\0000a0"; counter-increment: h3counter;}h3 { counter-reset: h4counter;}
```

comments: author: Jim Blinkhorn
body: Hi Steffen,

The output remains the same; no numbering past x.1 and no counter incremement on the second numbering tier.

Thanks,

Jim


Tags: cloud

confluence-cloud

css

export

pdf

stylesheet