Forum


Replies: 7   Views: 4776
Create toc when use a template
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 Sven May  · 11-02-2015 - 14:09

I would like ask that how I can make a TOC when I am using a template. ...because I have to use a word template I make a docx object with new CreateDocxFromTemplate(); I would like to add TOC on the 3rd page so I use varibale. To replace this variable I tried to use WordFragment class to change the variable for a real TOC...but it is not work :( part of my code: $docx = new CreateDocxFromTemplate($template); $wf = new WordFragment($docx); $wf->addText('Table of Contents', array('bold'=>true, 'fontSize'=>16, 'font'=>'Arial')); $wf->addTableContents(array('autoUpdate'=>true)); $wf->addText('Table of Contents', array('bold'=>true, 'fontSize'=>16, 'font'=>'Arial')); /* with WordFragment not work to add option like: array('autoUpdate'=>true) got this error msg: PHP Fatal error: Call to a member function getElementsByTagName() on a non-object in ...................../Symfony/vendor/phpdocx/src/Phpdocx/src/Classes/Phpdocx/Create/CreateDocx.inc on line 6104 */ $wf->addTableContents(); $wf->addText('Chapter 1', array('pStyle' => 'Heading1PHPDOCX')); $wf->addText('Section', array('pStyle' => 'Heading2PHPDOCX')); $wf->addText('Another TOC entry', array('pStyle' => 'Heading3PHPDOCX')); $docx->replaceVariableByWordFragment(array('TOC'=>$wf), array('type'=>'block')); Can I get any help what's wrong? The result is a non linked TOC without page number.

Posted by admin  · 11-02-2015 - 15:08

Hello, The method addTableContents doesn't support WordFragment class in the current version of phpdocx. Could not you add the TOC in the template with Word before using it with phpdocx? Regards.

Posted by Sven May  · 11-02-2015 - 17:28

Hello! Unfortunately not because the main content of the document will be generated and the length of the content depend on several outside parameters. If it is possible I could try to resolve it with macro if it is a possible way. Let m e one more question: if I generate a HTML content with TWIG or with write the HTML content and it includes table but the table TAG has not any property like border. The implemented HTML code with embedHTML() method display borders for every td TAGs. Around with paddings :( Do I have to set the border property to zero? Thank you!

Posted by Sven May  · 11-02-2015 - 18:07

...just one more think... I meant in your answer that I insert a TOC part into the template and add "special" style those titles, text which has to display in TOC?

Posted by admin  · 11-02-2015 - 19:28

Hello, You need to hide the border for each td with a style inline. About the TOC, yes, you can insert headings in the document and they should appear in the TOC. Regards.

Posted by Sven May  · 12-02-2015 - 09:37

Hello Admin! Thank you for your help! I think I can resolve what I want :) Could you help me or tell me how can I get any information about Word stlye. Exxample: I create my tamplate, use Phpdocx addText() function and want to add Word stlye like Heading 1 to be able refresh my TOC. Up to now I did not find a name of the style what I can use in my Php code. $docx->addText('My Maintitle text', array('pStyle'=>?????????)); Thanks!

Posted by admin  · 12-02-2015 - 10:35

Hello, You could: · Use the method addHeading to add a new heading · Import the existing styles in a DOCX with importStyles · Get the ID of your styles with the method parseStyles, and use it with the pStyle parameter of addText · Use the headingLevel parameter of addText to set the heading level Regards.

Posted by Sven May  · 12-02-2015 - 13:46

Thank you for help! The importStyles() works and I am reach the Word styles! Thx.