Is it possible to generate a TOC by using EmbedHTML while using Templates?
Is it possible to generate a TOC by using EmbedHTML while using Templates?
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.
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 ?
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.
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.