我在PHPWord上遇到了一些麻烦。一般来说,该模块相当简单易用,但我似乎无法做到这一点:为标题添加一种风格(或者根本添加一个标题)。
背景:
我的文档生成正确,我正在从数据库中加载变量和文本,它正在按照我的意愿生成(在实现一些小的替换解决方法之后),但我的标题都是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 ###############################
我可能只是错过了一些愚蠢明显的东西,但我一直盯着自己,我完全可以找到提及标题的PHPWord支持。 任何帮助将不胜感激。
答案 0 :(得分:4)
---所有信用都归XATENEV ---
Xatenev发布了答案:我错误地将预定义的字体传递给了addTitleStyle)。
您应该只传递用于创建字体的选项数组。
<强>错误:强>
$phpWord->addFontStyle('font_h1', array('name'=>'HelveticaNeueLT Std Med', 'size'=>16, 'color'=>'990000'));
$phpWord->addTitleStyle(1, "font_h1"); //h1
从右:强>
$phpWord->addTitleStyle(1, array('name'=>'HelveticaNeueLT Std Med', 'size'=>16, 'color'=>'990000')); //h1
总的来说不是一个困难的问题,但是如果你和我一样误解了PHPWord中的addTitleStyle和addTitle函数,那么现在希望它现在更清晰了。