douzhengzuo7283 2014-06-30 09:35
浏览 126
已采纳

PHPWord - 标题样式未应用

I'm having a bit of trouble with PHPWord. In general the module is fairly straightforward and easy to use, but there's one thing I just can't seem to do: adding a style to a title (or adding a title at all, really).

Background:
My document is generating correctly, I'm loading variables and text from a database, it's generating exactly as I want it to (after implementing a little -replacement workaround), but my titles all remain Arial, 10pt, and not a title style.

Here's my code (I removed a little chunk in the middle which generates my introduction, but it contains no titles):

//############################### 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("
", $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 ###############################

I'm probably just missing something stupidly obvious, but I've been staring myself blind on this, and I can find absolutely 0 PHPWord support mentioning titles. Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dprxj1995 2014-07-01 08:32
    关注

    --- ALL CREDIT GOES TO XATENEV ---

    Xatenev posted the answer: I was mistakenly passing a predefined font to the addTitleStyle) function.

    You should pass only the options array used to create a font.

    Wrong:

    $phpWord->addFontStyle('font_h1', array('name'=>'HelveticaNeueLT Std Med', 'size'=>16, 'color'=>'990000'));
    $phpWord->addTitleStyle(1, "font_h1"); //h1
    

    Right:

    $phpWord->addTitleStyle(1, array('name'=>'HelveticaNeueLT Std Med', 'size'=>16, 'color'=>'990000')); //h1
    

    Not on the whole a difficult problem, but if you, like me, misunderstood the addTitleStyle and addTitle functions in PHPWord, then hopefully it's now a bit clearer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog