skip to Main Content

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 PhpOfficePhpWordTemplateProcessor('modules/admin/views/customers/resume_tpl.docx');

$phpWord = new PhpOfficePhpWordPhpWord();
$phpWord->setDefaultFontName('Arial');
$phpWord->setDefaultFontSize(10);

$section = $phpWord->addSection();
$wordTable = $section->addTable();
$wordTable->addRow();
$cell = $wordTable->addCell();                                
PhpOfficePhpWordSharedHtml::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.

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. I tested with the addText() method with your code and it does change the font-name and font-size of the table, by reference post here Formatting a text in a table cell with PHPWord e.g. bold, font, size e.t.c

    $templateProcessor = new PhpOfficePhpWordTemplateProcessor('test.docx');
    
    $phpWord = new PhpOfficePhpWordPhpWord();
    
    $section = $phpWord->addSection();
    $wordTable = $section->addTable();
    
    $wordTable->addRow();
    $wordTable
      ->addCell()
      ->addText(
        str_replace("&","&amp;", "HTML Texting"),
        array(
          'name' => 'Arial',
          'size' => 10,
        );
      );
                      
    $templateProcessor->setComplexBlock('assess', $wordTable);
    
    $templateProcessor->saveAs('result.docx');
    

    Hope it helps!


    And I think this sample might help out for you too
    https://github.com/PHPOffice/PHPWord/blob/master/samples/Sample_40_TemplateSetComplexValue.php

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search