Why are page-breaks not generating bookmarks in PDF on export - as expected.
Asked by Frank Frank:

I've successfully used both user macros and CSS to force page-breaks at various points of the confluence page - when exported to PDF.

I'm not seeing any bookmarks generated, however, following each 'page-break' ?

Why is this so difficult...

Please see my image attachments..

Thanks


Answers:
author: Frank Frank
body: OK... This seems to be - the only way to do it...

1) Confluence does not directly support - inserting markup - into the page \<head\>

This is most unfortunate...

2) However, it is possible to insert \<script\> which in theory could lookup the head element and insert the bookmark tags when the page is loaded.

Printing the PDF then - should have the bookmarks automatically generated...

This should work...

I got the idea from here:

[![](https://confluence.atlassian.com/images/logo/confluence_48_trans.png)](https://confluence.atlassian.com/display/DISC)RE: \[CONF-user\] Custom macro \& head/page top/bottom problem {#toc-hId-1617866592}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | |----------|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | Subject: |   | **RE: \[CONF-user\] Custom macro \& head/page top/bottom problem** ([find more](https://confluence.atlassian.com/dosearchsite.action?key=DISC&type=mail&queryString=%22%5BCONF-user%5D+Custom+macro+%26+head%2Fpage+top%2Fbottom+problem%22 "Find similar mail by subject")) | | From:    |   | Dan Hardiker \<hidden\> ([find more](https://confluence.atlassian.com/dosearchsite.action?key=DISC&type=mail&queryString=%22Dan+Hardiker%22%20%22dhardiker@eorigen.com%22 "Find similar mail by author"))                                                                    | | Date:    |   | Mar 04, 2005 05:50                                                                                                                                                                                                                                                           | | |

If you must load a script tag into head, you can use the following:   

\<script\>   
var script = document.createElement("script");   
script.src="path/to/file.js";   
document.childNodes\[0\].childNodes\[0\].appendChild( script );   
\</script\>   

I have found that to be reliable during testing in various browsers,   
this assumes that the first nodes in the DOM are \<html\> and \<head\> ... a   
more resilient option might utilise a function like:   

\<script\>   
document.getElementByNodeName = function (name, recurse, parent) {   
if (typeof(recurse) == "undefined" \|\| recurse == null) recurse = -1;   
if (typeof(parent) == "undefined" \|\| parent == null) parent =   
document;   
for (var i=0; i\<parent.childNodes.length; i++) {   
if (parent.childNodes\[i\].nodeName == name.toUpperCase()) return   
parent.childNodes\[i\];   
if (recurse != 0) {   
document.getElementByNodeName(name, (recurse-1),   
parent.childNodes\[i\]);   
}   
}   
return null;   
}   
var script = document.createElement("script");   
script.src="path/to/file.js";   
script.loading = false;   
var head = document.getElementByNodeName("HEAD", 3);   
if (head != null) {   
script.loading = true;   
head.appendChild( script );   
}   
\</script\>   

Please note - I've not tested that code so it may well have a few bugs   
...  

Dan Hardiker   


author: Ron Elazar
body: Was there any progress on this issue in the last 5 years , anybody successfully implemented a solution for this ?


author: Steffen Heller
body: Hi,

I had similiar questions and what I learned is that "flying saucer" is the library used by confluence. Hence what happens or does not happen when you export to PDF lies within flying saucer.

On their page there is some explanation about how to create PDF bookmarks:  
http://code.google.com/p/flying-saucer/wiki/HowTo_PDF_bookmarks_from_xhtml_markup

If there is a simpler method with page breaks I would be happy to know it but I rather doubt it.

But let us know if you found something.

Cheers

comments: author: Frank Frank
body: Ahhh... I'm silly...

I knew flying saucer was the backing library - doing the work - and have come across the project before. That's where I should have been directing my energies !

Thanks for the kick...

I'll look into it - and get back to you...

Cheers

F


author: Steffen Heller
body: Found something else:

If CSS bookmarks did work it should probably do so as described [here](http://www.w3.org/TR/css3-gcpm/#bookmarks). I quickly tried, couldn't get it to work and suppose it doesn't because it is CSS3 and not 2.1.


author: Frank Frank
body: Hi Steffen

I'm back on this issue... and would like to solve today.

I've researched the google-code FlyingSaucer docs - and have a solution.

See here:

<http://code.google.com/p/flying-saucer/wiki/HowTo_PDF_bookmarks_from_xhtml_markup>

It will only work - if you're programatically generating your confluence pages... AND inserting the custom bookmark "markup" into the \<head\> of the confluence page....

I won't be writing any user macros for this...

The question now remains - is there a way to insert content into the \<head\>\</head\> sections of pages that confluence generates ?

Does confluence provide some custom HTMLMacro ? or UserMacro ? that allows insertion of custom markup - into the html \<head\> of a page ?

I suspect the technique - defined at the above link - will work..... but I'm having trouble finding mechanisms that would allow me to control where in a page - my markup is placed ?

\<head\> \<body\> \<script\> etc. ?

Hope you may have some ideas ?


author: Frank Frank
body: Hi Steffen....

I mis-spoke.... this will work - without dynamic/programatically generating pages as well...

Nothing is stopping you from taking the code - from referenced website:

```
     <head>  
         <bookmarks>  
             <bookmarkname="A bookmark"href="#bm"/>  
             <bookmarkname="A bookmark 2"href="#bm2">  
                 <bookmarkname="A bookmark 3"href="#bm3"/>  
             </bookmark>  
             <bookmarkname="A bookmark invalid"href="#bm99"/>  
         </bookmarks>  
     </head>  
```

And just statically.... a) Click page EDIT b) insert markup.... when page is printed - bookmarks will be generated and work as long as referenced href #bm, #bm2 etc. anchors exist...

```
     <body>  
         <aname="bm1">Jumpmark 1</a>  
         <aname="bm2">Jumpmark 2</a>  
         <aname="bm3">Jumpmark 3</a>  
     </body>
```

Do you know - is it possible in confluence - with some macro perhaps - to add content to the \<head\> of a document - not unlike the SytleMacros - that allow you to inject custom CSS styles into a page.... Could we do same with the above \<bookmark\> code ?


author: Steffen Heller
body: Hi,

I had similiar questions and what I learned is that "flying saucer" is the library used by confluence. Hence what happens or does not happen when you export to PDF lies within flying saucer.

On their page there is some explanation about how to create PDF bookmarks:  
http://code.google.com/p/flying-saucer/wiki/HowTo_PDF_bookmarks_from_xhtml_markup

If there is a simpler method with page breaks I would be happy to know it but I rather doubt it.

But let us know if you found something.

Cheers


author: Steffen Heller
body: Hi fmisa,

I am afraid I don't have an answer but yet another question:

I would also like to be able to generate bookmarks in the left panel of my PDf file and I don't have a clue how this is done. Do I understand you correctly that you assume that page breaks on the confluence page should create bookmarks on the PDF?

If so, is this documented somewhere or where did you get that information from?

Steffen

comments: author: Frank Frank
body: Hi Steffen...

Yes - this is my understanding - though given it's not working - maybe I'm wrong ;)

I base my thoughts on these links:

[https://answers.atlassian.com/questions/30169/confluence-export-to-pdf-generate-bookmark-index](/questions/30169/confluence-export-to-pdf-generate-bookmark-index)

[https://answers.atlassian.com/questions/4625/pdf-export-creates-bookmarks-at-end-of-prior-section](/questions/4625/pdf-export-creates-bookmarks-at-end-of-prior-section)

See my previous question:

[https://answers.atlassian.com/questions/68373/pdf-stylesheet-customization-generating-bookmarks-page-breaks-for-h1-and-h2-headings](/questions/68373/pdf-stylesheet-customization-generating-bookmarks-page-breaks-for-h1-and-h2-headings)

Check back next week - if I figure this out - I will post my answer here...

Cheers

F


author: Frank Frank
body: Home these images are visible...

![](../../upfiles/exportedPDF_2_2.png)![](../../upfiles/exportedPDF_1_3.png)


Tags: export

format

pdf