我在使用 PHPWord 时遇到了一些麻烦。一般来说,该模块相当简单且易于使用,但有一件事我似乎无法做到:为标题添加样式(或根本添加标题,真的)。
背景:
我的文档生成正确,我正在从数据库中加载变量和文本,它完全按照我想要的方式生成(在实施了一些 \n-replacement 解决方法之后),但我的标题仍然是 Arial、10pt,而不是标题样式。
这是我的代码(我在中间删除了一小块生成我的介绍,但它不包含标题):
//############################### STARTING DOCUMENT AND DEFINING STYLES ###############################
$phpWord = new \PhpOffice\PhpWord\PhpWord(); //start new document
$file = $document->titel . '.docx';
// adding the necessary font styles
$phpWord->addFontStyle('font_default', array('name'=>'HelveticaNeueLT Std Lt', 'size'=>11, 'color'=>'000000'));
$phpWord->addFontStyle('font_h1', array('name'=>'HelveticaNeueLT Std Med', 'size'=>16, 'color'=>'990000'));
$phpWord->addFontStyle('font_h2', array('name'=>'HelveticaNeueLT Std Med', 'size'=>14, 'color'=>'6D6D6D'));
$phpWord->addFontStyle('font_h3', array('name'=>'HelveticaNeueLT Std Med', 'size'=>12, 'color'=>'949494'));
//adding the necessary header/title styles
$phpWord->addTitleStyle(1, "font_h1"); //h1
$phpWord->addTitleStyle(2, "font_h2"); //h2
$phpWord->addTitleStyle(3, "font_h3"); //h3
//adding the necessary paragraph styles
$phpWord->addParagraphStyle('paragraph_default', array('spaceBefore' => 0, 'spaceAfter' => 0));
//############################### STARTING DOCUMENT AND DEFINING STYLES ###############################
//############################### INTRODUCTION ###############################
$introduction = $phpWord->addSection();
//cut out some code that adds a bunch of text here
//############################### INTRODUCTION ###############################
//############################### CHAPTERS ###############################
//prepping the textblocks (to get rid of newlines, but still have their functionality)
for ($i = 0; $i < count($documentTextblocks); $i++) //split each textblock into its paragraphs
{
$documentTextblocks[$i]->inhoud = explode("\n", $documentTextblocks[$i]->inhoud);
}
for ($i = 0; $i < count($documentChapters); $i++) //for each chapter
{
$chapter = $phpWord->addSection();
$chapter->addTitle($documentChapters[$i]->koptekst, 1); //add the chapter (h1) header
for ($x = 0; $x < count($documentTextblocks); $x++) //for each textblock
{
if ($documentTextblocks[$x]->offerte_hoofdstuk == $documentChapters[$i]->id)
{
//$block = $phpWord->addSection(); //this places every textblock on a new page, so lets not
$chapter->addTitle($documentTextblocks[$x]->koptekst, 2); //add the textblock (h2) header
for ($y = 0; $y < count($documentTextblocks[$x]->inhoud); $y++) //add all paragraphs to the chapter/textblock
{
$chapter->addText($documentTextblocks[$x]->inhoud[$y], "font_default", "paragraph_default");
}
$chapter->addTextBreak(2, null, "paragraph_default"); //add an empty line between adjacent textblocks
}
}
}
//############################### CHAPTERS ###############################
我可能只是遗漏了一些非常明显的东西,但我一直盯着自己看,我可以找到绝对 0 的 PHPWord 支持提及标题。任何帮助将不胜感激。