PHPWord addTOC is not displaying page numbers

2019-03-14 03:32发布

问题:

I'm using https://github.com/PHPOffice/PHPWord to generate a word document. The function addTOC generates an index of pages with titles, but for some reason the page numbers do not display.

// add table of contents
$section->addTOC();

Results in:

If I echo the page numbers on the pages where the titles exist, they display fine. But they don't display in the TOC. I checked to confirm the text isn't white.

回答1:

From PHPWord example in samples dir (https://github.com/PHPOffice/PHPWord/blob/develop/samples/Sample_17_TitleTOC.php), I would tell it is not possible:

echo date('H:i:s'), ' Note: Please refresh TOC manually.', EOL;

I also tried to add the TOC at the end of the document, after titles declarations (when we already know page numbers). But with no success.

EDIT: seems to be a bug: https://github.com/PHPOffice/PHPWord/issues/707



回答2:

Phpword version 0.14.0 fixed this issue. (0.15.0 also released on Jul 15)

You can download it from here https://github.com/PHPOffice/PHPWord/releases

Anyway it still needs update. You can do it with this code.

$phpWord->getSettings()->setUpdateFields(true);

When setting the updateFields to true Word will request to update the fields when opening the document.

Related articles here

https://github.com/PHPOffice/PHPWord/issues/707

https://github.com/PHPOffice/PHPWord/issues/951

https://github.com/PHPOffice/PHPWord/pull/1205