gpt4 book ai didi

PHPWord - 未应用标题样式

转载 作者:行者123 更新时间:2023-12-02 04:29:13 24 4
gpt4 key购买 nike

我在使用 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 支持提及标题。任何帮助将不胜感激。

最佳答案

--- 所有功劳归于 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 函数,那么希望现在更清楚一些。

关于PHPWord - 未应用标题样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24487079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com