Search code examples
phpphpword

Set font style to table with addHtml in PHPWord


I am using a table with addHtml method to export HTML to docx file in phpWord. But the font of text is Times New Roman (font-size 12), meanwhile, the font should be Arial (font-size 10) by default. I did not set anything in code. I also tried something but it seems not to work. Here is my code:

$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('modules/admin/views/customers/resume_tpl.docx');

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->setDefaultFontName('Arial');
$phpWord->setDefaultFontSize(10);

$section = $phpWord->addSection();
$wordTable = $section->addTable();
$wordTable->addRow();
$cell = $wordTable->addCell();                                
\PhpOffice\PhpWord\Shared\Html::addHtml($cell, str_replace("&","&", $model[self_assessment]));  // $model[self_assessment] is the HTML text

// $templateProcessor->setDefaultFontName('Arial');
// $templateProcessor->setDefaultFontSize(10);
                  
$templateProcessor->setComplexBlock('assess', $wordTable);

All I want is to set font to 'Arial' and font-size 10.


Solution

  • I kind of figured it out, although I don't like this way :v. Because my text is from CK Editor, so it has HTML format. I just add css style to HTML and it works. Something like this:

    $model[self_assessment] = str_replace("&","&", $model[self_assessment]);
    
    $assess = str_replace("<p>", "<p style='font-size: 10pt; font-family: Arial;'>", $model[self_assessment]);
    $assess = str_replace("<ul>", "<ul style='font-size: 10pt; font-family: Arial;'>", $assess);
    $assess = str_replace("<ol>", "<ol style='font-size: 10pt; font-family: Arial;'>", $assess);
    

    Thank you all.