Forum


Replies: 5   Views: 3102
Generating toc using templates and embedhtml
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by admin@wotbot.com.au  · 30-10-2017 - 09:24

Is it possible to generate a TOC by using EmbedHTML while using Templates?
 

Posted by admin  · 30-10-2017 - 10:35

Hello,

You can include contents in the TOC using heading tags or custom MS Word styles, but embedHTML can't generate a TOC. We recommend you to use the addTableContents method or add the TOC into the template.

Regards.

Posted by admin@wotbot.com.au  · 30-10-2017 - 13:42

Here's what I did,

On creating the template:

1.) Added the toc placeholder by going to Words' References>Add table of contents on page 1.
2.) Added the variable $CONTENTS$ on page 2

On generating the document:

1.) I loaded the template created above.
2.) I used `replaceVariableByHTML` to substitute the $CONTENTS$, the html has h1 tags in them.
 

But the generated document didnt update the TOC section.
Am I doing something wrong ?

Posted by admin  · 30-10-2017 - 15:00

Hello,

Maybe you didn't set the correct heading levels that must appear in the TOC? On this page you can find more information about TOCs:

https://www.phpdocx.com/documentation/cookbook/tables-of-contents

This is a simple script that generate a DOCX from scratch adding a TOC and two headings (note the displayLevels option):

<?php

require_once 'classes/CreateDocx.inc';

$docx = new CreateDocx();

$docx->addText('Table of Contents', array('bold' => true, 'fontSize' => 14));
$legend = array(
    'text' => 'Click here to update the TOC', 
    'color' => 'B70000', 
    'bold' => true, 
    'fontSize' => 12
);
$docx->addTableContents(array('autoUpdate' => true, 'displayLevels' => '1-3'), $legend);

$html = '<h1>Heading 1</h1>';

$html .= '<p>Other text</p>';

$html .= '<h2>Heading 2</h2>';

$docx->embedHTML($html);

$docx->createDocx('output');

If you open the DOCX with MS Word or LibreOffice and then update the TOC, both heading appear in the TOC.

Regards.

Posted by admin@wotbot.com.au  · 31-10-2017 - 03:13

How do I do that when using `CreateDocxFromTemplate` ?

Posted by admin  · 31-10-2017 - 05:50

Hello,

The CreateDocxFromTemplate class inheritances CreateDocx, so you can use the same methods.

There's a documentation page about the ways to generate DOCX files with phpdocx:

https://www.phpdocx.com/documentation/cookbook/add-contents-document-types

Anyway if you are using a template, the easiest approach is adding the TOC using MS Word and the fill it using phpdocx (adding contents that are supported in the display levels). The previous script was just an example to illustrate how headings are added to the TOC.

Regards.