在 PHPWord 中设置 Word 文档的页眉和页脚需要直接在 Section 对象上操作。以下是详细的实现方法和代码示例,包含多种场景的设置:
一、基础页眉页脚设置
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
public function createDocumentWithHeaderFooter()
{
$phpWord = new PhpWord();
// 创建带页眉页脚配置的节
$section = $phpWord->addSection([
'headerHeight' => 300, // 高度(单位:twip,1厘米=567twip)
'footerHeight' => 300,
'marginTop' => 600 // 预留页眉空间
]);
// ===== 添加页眉 =====
$header = $section->addHeader();
// 1. 简单文本页眉
$header->addText('公司机密文档', ['bold' => true]);
// 2. 带格式的页眉
$header->addText('文档版本:1.0', ['name' => 'Arial', 'size' => 9]);
// 3. 带边框的页眉
$header->addText(
'生成日期:' . date('Y-m-d'),
['size' => 10],
['borderBottomSize' => 3, 'borderBottomColor' => '000000']
);
// ===== 添加页脚 =====
$footer = $section->addFooter();
// 1. 页码 - 使用预设标签
$footer->addPreserveText('第 {PAGE} 页 / 共 {NUMPAGES} 页', null, ['alignment' => 'center']);
// 2. 公司信息页脚
$footer->addText('© 2023 My Company. All rights reserved', ['italic' => true]);
// 文档内容
$section->addText('文档正文内容...');
// 保存文档
$filename = 'document_with_header_footer.docx';
$tempFile = tempnam(sys_get_temp_dir(), $filename);
$writer = IOFactory::createWriter($phpWord);
$writer->save($tempFile);
return $tempFile;
}
二、高级页眉页脚功能
1. 奇偶页不同页眉/页脚
$section = $phpWord->addSection([
'differentOddEven' => true, // 启用奇偶页不同
]);
// 奇数页页眉
$header = $section->addHeader();
$header->addText('奇数页页眉');
// 偶数页页眉
$headerEven = $section->addHeader('even');
$headerEven->addText('偶数页页眉');
2. 首页不同页眉/页脚
$section = $phpWord->addSection([
'differentFirst' => true, // 启用首页不同
]);
// 首页页眉
$firstHeader = $section->addHeader('first');
$firstHeader->addText('首页专用页眉');
// 其他页页眉
$header = $section->addHeader();
$header->addText('文档正文页眉');
3. 页眉中添加图片和表格
$header = $section->addHeader();
// 添加图片
$header->addImage('path/to/logo.png', [
'width' => 80,
'height' => 30,
'alignment' => 'left'
]);
// 添加表格
$table = $header->addTable(['borderSize' => 1]);
$row = $table->addRow();
$row->addCell(5000)->addText('部门');
$row->addCell(5000)->addText('技术部');
三、模板文件中的页眉页脚处理
对于基于模板的文档,需要在模板文件中预设好页眉页脚,然后通过变量替换:
public function processTemplateWithHeaderFooter()
{
$template = new TemplateProcessor('template.docx');
// 替换页眉中的变量
$template->setValue('header.company_name', 'ABC 科技有限公司');
// 替换页脚中的页码格式
$template->setValue('footer.page_info', '第{PAGE}页/共{NUMPAGES}页');
// 替换页脚中的日期
$template->setValue('footer.current_date', date('Y年m月d日'));
$template->saveAs('output.docx');
}
四、完整示例(控制器中使用)
namespace App\Http\Controllers;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
class DocumentController extends Controller
{
public function generateDocument()
{
$phpWord = new PhpWord();
// 设置文档属性
$phpWord->getDocInfo()
->setCreator('Laravel App')
->setTitle('专业文档')
->setDescription('带页眉页脚的文档');
// 添加带页眉页脚的节
$section = $phpWord->addSection([
'headerHeight' => 400,
'footerHeight' => 400,
'differentFirst' => true
]);
// === 首页页眉 ===
$firstHeader = $section->addHeader('first');
$firstHeader->addText('首页标题', ['bold' => true, 'size' => 16], ['alignment' => 'center']);
// === 常规页眉 ===
$header = $section->addHeader();
$header->addText('文档正文标题', ['bold' => true]);
$header->addText(date('Y-m-d'), ['size' => 9], ['alignment' => 'right']);
// === 页脚 ===
$footer = $section->addFooter();
// 带格式的页码
$footer->addPreserveText(
'第 {PAGE} 页 / 共 {NUMPAGES} 页',
['bold' => true, 'color' => '0000FF'],
['alignment' => 'center']
);
// 文档内容
$section->addTitle('文档正文', 1);
$section->addText('这是文档的主要内容...');
// 保存并下载
$filename = 'professional_document.docx';
$tempFile = tempnam(sys_get_temp_dir(), $filename);
$writer = IOFactory::createWriter($phpWord);
$writer->save($tempFile);
return response()->download($tempFile, $filename)->deleteFileAfterSend(true);
}
}
注意事项:
- 单位系统:PHPWord 中使用 twip 作为单位(1 twip = 1/1440 英寸 ≈ 1/567 厘米)
- 页码标签:
{PAGE}
当前页码{NUMPAGES}
总页数- 必须使用
addPreserveText()
方法才能正确解析
- 布局技巧:
- 使用
marginTop
和marginBottom
为页眉页脚预留空间 - 通过嵌套表格实现复杂布局
- 使用
alignment
参数控制对齐方式(left/center/right)
- 使用
- 性能考虑:
- 处理大型文档时避免频繁创建临时文件
- 对于批量生成,考虑队列处理
这些方法涵盖了大多数业务场景下的页眉页脚需求,您可以根据实际业务需要组合使用这些技术。