0

In phpword, I see you used to be able to pass a styles array to the addHTML function, but not any more.

If my HTML doesn't have any inline styles set, is there a way to specify a fontStyle and paragraphStyle so it doesn't end up as plain 'Arial 10pt' in the Word file?

I've had some success by setting default styles before calling addHTML:

$phpWord->setDefaultFontName('...');
$phpWord->setDefaultFontSize('...');
$phpWord->setDefaultParagraphStyle('...');

However, this isn't a complete font style. For example, how would you change the text's color?

Nicolas Goosen
  • 583
  • 5
  • 14

3 Answers3

5

It seems the most versatile solution is actually to add the styles directly into your html, before calling addHTML(). Something like...

$html = str_replace("<p>", 
    "<p style='font-size: 10pt; font-family: Arial; color:#595959; line-height: 140%; margin-bottom: 160pt;'>", 
    $html);
Nicolas Goosen
  • 583
  • 5
  • 14
0

Follow the doc it would be something like:

// New document
$phpWord = new \PhpOffice\PhpWord\PhpWord();
// New empty section
$section = $phpWord->addSection();
// Set font-styles
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
// Adding text and font-styles to the section
$myTextElement = $section->addText("Bla bla bla");
$myTextElement->setFontStyle($fontStyle);

This way you could use $fontStyle as the default style for all the text you create.

  • Sure, but I need to run that `setFontStyle(...)` on incoming HTML. `addHTML()` doesn't return a reference to the element(s) it just added, so I CAN'T do something like this (which is what I'd like to do): `$myTextElement = $section->addTextRun();` `\PhpOffice\PhpWord\Shared\Html::addHtml($myTextElement, $html, false, false);` `$myTextElement->setFontStyle($fontStyle);` – Nicolas Goosen Oct 24 '19 at 08:50
0

Just add for example Default font example :

$phpWord->setDefaultFontName('Times New Roman');
$phpWord->setDefaultFontSize(12);
Xartec
  • 2,369
  • 11
  • 22