Byson
Byson

Reputation: 550

PHPWord - Title Styles not being applied

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 \n-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("\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 ###############################

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.

Upvotes: 2

Views: 7244

Answers (1)

Byson
Byson

Reputation: 550

--- 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.

Upvotes: 7

Related Questions