Forum How do I...?

How to create a toc from several files?

barto
Hi,
I have tried to create a pdf from several html files and generate a table of contents. I have tried to follow the quick guide to creating Table of Contents in Prince 14, but I have failed to do so. Output pdf is created including all files, but table of contents is empty. I am using Prince 14.3. Can you help me?
Thanking you in advance

command
prince.exe ^
-s prince-style.css ^
toc.html ^
chapter-1.html ^
chapter-2.html ^
-o output.pdf

prince-style.css
html { font-family: sans-serif; }
#toc li { list-style-type: none }
#toc a:after { content: leader('. ') target-counter(attr(href), page) }

toc.html
<head>
  <meta charset="utf-8">
  <title>TOC</title>
  <script src="https://princexml.com/howcome/2020/guides/charms.js"></script>
</head>
<body onload="toc('h1, h2')">
  <h1>Table of contents</h1>
  <ol id="toc"></ol>
</body>

chapter-1.html
<head>
  <meta charset="utf-8">
  <title>Chapter 1</title>
</head>
<body>
  <h1>Chapter 1</h1>
  <p>Veniam eiusmod cillum esse eiusmod non.</p>
  <h2>First item</h2>
  <p>Enim dolor aliquip est laboris.</p>
  <h2>Second item</h2>
  <p>Culpa reprehenderit elit</p>
</body>

chapter-2.html is similar to chapter-1.html
  1. chapter-1.html0.3 kB
  2. chapter-2.html0.4 kB
  3. output.pdf38.6 kB
  4. prince-style.css0.1 kB
  5. toc.html0.3 kB
markbrown
Hi,

You need the '-j' option to enable JavaScript.

Cheers,
Mark
barto
Thank you for your help. I have included the -j option and now a table of contents is created, but just with one item (a link to the h1 in the toc.html itself). Links to 1 and h2 of the other documents are not included. I am attaching the output file.
Best regards,
Barto
  1. output.pdf42.3 kB
howcome
The script will only see content from the file where it is called from. Not sure if one can/should get around this restriction. But, an easy workaround is to concatenate the files. Using pipes, it's a one-liner:
cat toc.html chapter-1.html chapter-2.html | prince -j -s prince-style.css -o output.pdf -

Edited by howcome

barto
Thank you for your help. Concatenate the files would work with the simple example I sent, but if there were links between files, they would not be converted in links inside the pdf.
howcome
You could try using the "two-pass" solution where the first pass prints out elements to be included in the ToC

Edited by howcome

csant
There is an up-to-date multifile-ToC example:

https://www.princexml.com/doc/cookbook/#multifile-table-of-contents

Edited by csant

csant
There is an up-to-date multifile-ToC example:


Also, in the documentation for the next Prince release there is a step-by-step instruction to build your own documentation PDF - you can get a preview of that here. By studying how the documentation is built up, you can see how the links between pages are translated into one big PDF.

Please note that currently things might not completely work out-of-the-box - small adjustments were necessary to make all work smoothly, but you'll be able to test it once the docs go live!