一
pull request
6月30日接受的建议如下。
循环一个文档的节并将其插入另一个文档。
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText('Lorem ipsum');
// Load another document
$phpWord2 = \PhpOffice\PhpWord\IOFactory::load('lorem.docx');
// Add sections to the first document
foreach ($phpWord2->getSections() as $section) {
$phpWord->addExistingSection($section);
}
所以基本上你只需要(未测试的代码)
$documents = ['doc1.docx', 'doc2.docx', 'doc3.docx', 'doc4.docx'];
$lastDoc = end($documents);
$mainDoc = new \PhpOffice\PhpWord\PhpWord();
foreach($documents as $document) {
$appendDoc = \PhpOffice\PhpWord\IOFactory::load($document);
foreach ($appendDoc->getSections() as $section) {
$mainDoc->addExistingSection($section);
}
// Add page break after every appended doc except the last one
if($document != $lastDoc) {
$section = $mainDoc->addSection();
$section->addPageBreak();
}
}
现在
$mainDoc
应该准备好保存。
另一个可能的解决方案是
DocxMerge
. 但不确定它是否支持添加分页符等。