可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Not got a lot of experience with phpWord and coming across the following error:
Fatal error: Uncaught exception 'BadMethodCallException' with message 'Cannot add PreserveText in Section.'
Its happening when using the below line:
$phpWord = \PhpOffice\PhpWord\IOFactory::load($document_location . $document_name . '.docx');
The document definitely exists and I can save the document and readfile as well.
Any insight would be greatly appreciated.
回答1:
I had the same Problem (also asked here) and for me loading the document worked after I removed all mail merge fields
from it. Maybe this works for you, too
回答2:
For using preserve text in Section add to method checkValidity()
in file PhpOffice/PhpWord/Element/AbstractContainer.php
new rules.
For example :
in line 214: 'PreserveText' => array('Header', 'Footer', 'Cell')
add Section
to array
in line 224: 'PreserveText' => array(array('Cell'), array('Header', 'Footer'))
add Section
too for sub containers.
Now, you should write
$section->addPreserveText("Total {PAGE} pages.");
回答3:
Anyone needing to load a document and keep the mail merge fields can use the following:
new \PhpOffice\PhpWord\TemplateProcessor($documentPath);
Loading it as a template instead and allowing use of those merge fields.
Sorry missed out the rest of this answer - with the following customized version https://github.com/dlwatersuk/PHPWord
Have requested these changes be pulled to the official repo