PHPWord table column span (gridSpan) not work as expected

124 Views Asked by At

I'm using PHPWord to create a table like this:

Expected Table

But I got this: Result Table

My Code:

    $phpWord = new \PhpOffice\PhpWord\PhpWord();
    $section = $phpWord->addSection();

    $fullWidth = 11906.85;

    $cellRowSpan = ['vMerge' => 'restart'];
    $cellRowContinue = ['vMerge' => 'continue'];

    $cellColSpan2 = ['gridSpan' => 2];
    $cellColSpan3 = ['gridSpan' => 3];
    $cellColSpan4 = ['gridSpan' => 4];
    $cellColSpan5 = ['gridSpan' => 5];
    $cellColSpan6 = ['gridSpan' => 6];

    $cell1Width = $fullWidth * 0.25;
    $cell2Width = $fullWidth * 0.095;
    $cell3Width = $fullWidth * 0.1725;
    $cell4Width = $fullWidth * 0.0475;
    $cell5Width = $fullWidth * 0.18;
    $cell6Width = $fullWidth * 0.255;

    $cell23Width = $cell2Width + $cell3Width;
    $cell34Width = $cell3Width + $cell4Width;
    $cell45Width = $cell4Width + $cell5Width;
    $cell56Width = $cell5Width + $cell6Width;
    $cell2to6Width = $fullWidth - $cell1Width;

    $table = $section->addTable([
        'borderColor' => 'cccccc',
        'borderSize'  => 6,
    ]);
    $table->addRow();
    $table->addCell($fullWidth, $cellColSpan6)->addText('Part A: Principal Investigator');

    $table->addRow();
    $table->addCell($cell1Width)->addText('Name:');
    $table->addCell($cell2to6Width, $cellColSpan5);

    $table->addRow();
    $table->addCell($cell1Width)->addText('School/Dept at HKUMed or Faculty of Dentistry:');
    $table->addCell($cell23Width, $cellColSpan2);
    $table->addCell($cell45Width, $cellColSpan2)->addText('Title at HKUMed or Faculty of Dentistry:');
    $table->addCell($cell6Width);
    
    $table->addRow();
    $table->addCell($cell1Width)->addText('Remark:');
    $table->addCell($cell2to6Width, $cellColSpan5);

    $table->addRow();
    $table->addCell($fullWidth, $cellColSpan6)->addText('Part B: Study Particulars');

    $table->addRow();
    $table->addCell($cell1Width)->addText('Protocol Title:');
    $table->addCell($cell2to6Width, $cellColSpan5);

    $table->addRow();
    $table->addCell($cell1Width)->addText('Disease/Medical Condition to be Studied:');
    $table->addCell($cell2to6Width, $cellColSpan5);

    $table->addRow();
    $table->addCell($cell1Width, $cellRowSpan)->addText('Target No. of Participants:');
    $table->addCell($cell2Width, $cellRowSpan);
    $table->addCell($cell34Width, $cellColSpan2)->addText('Participant Type:');
    $table->addCell($cell56Width, $cellColSpan2)->addText('Patients        Healthy volunteers');

    $table->addRow();
    $table->addCell(null, $cellRowContinue);
    $table->addCell(null, $cellRowContinue);
    $table->addCell($cell34Width, $cellColSpan2)->addText('Participant Age Range:');
    $table->addCell($cell56Width, $cellColSpan2)->addText('Lowest:  Highest:');

    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
    // $objWriter->save('helloWorld.docx');

    $tempFile = tempnam(sys_get_temp_dir(), 'PHPWord');
    $objWriter->save($tempFile);

    header("Content-Disposition: attachment; filename=test.docx");
    readfile($tempFile);
    unlink($tempFile); 

Problems:

  1. The table should have 6 columns, but currently it only has 5.
  2. The heading "Part A: Principal Investigator" should span 6 columns, but it currently spans only 4 columns.
  3. The column width seems not correct.

Thank you in advance!

1

There are 1 best solutions below

2
Mit Kathrotia On
  1. It has 6 columns I have checked it in Google Docs
  2. The heading also spans 6 columns
  3. The Columns' widths are not correct due to null values in the last row and default margin

I have modified your code and added one extra row with 6 columns to show that the span is working.

I have also set Margin Left to 0 because you are taking the width of the table as the width of the whole document.

Here is the modified code,

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection(['marginLeft' => 0]);

$fullWidth = 11906.85;

$cellRowSpan = ['vMerge' => 'restart'];
$cellRowContinue = ['vMerge' => 'continue'];

$cellColSpan2 = ['gridSpan' => 2];
$cellColSpan3 = ['gridSpan' => 3];
$cellColSpan4 = ['gridSpan' => 4];
$cellColSpan5 = ['gridSpan' => 5];
$cellColSpan6 = ['gridSpan' => 6];

$cell1Width = $fullWidth * 0.25;
$cell2Width = $fullWidth * 0.095;
$cell3Width = $fullWidth * 0.1725;
$cell4Width = $fullWidth * 0.0475;
$cell5Width = $fullWidth * 0.18;
$cell6Width = $fullWidth * 0.255;

$cell23Width = $cell2Width + $cell3Width;
$cell34Width = $cell3Width + $cell4Width;
$cell45Width = $cell4Width + $cell5Width;
$cell56Width = $cell5Width + $cell6Width;
$cell2to6Width = $fullWidth - $cell1Width;

$bgColor = [ 'bgColor' => 'aabbcc'];

$table = $section->addTable([
    'borderColor' => 'cccccc',
    'borderSize'  => 6,
]);
$table->addRow();
$table->addCell($fullWidth, $cellColSpan6)->addText('Part A: Principal Investigator');

$table->addRow();
$table->addCell($cell1Width)->addText('Name:');
$table->addCell($cell2to6Width, $cellColSpan5);

$table->addRow();
$table->addCell($cell1Width)->addText('School/Dept at HKUMed or Faculty of Dentistry:');
$table->addCell($cell23Width, $cellColSpan2);
$table->addCell($cell45Width, $cellColSpan2)->addText('Title at HKUMed or Faculty of Dentistry:');
$table->addCell($cell6Width);

$table->addRow();
$table->addCell($cell1Width)->addText('Remark:');
$table->addCell($cell2to6Width, $cellColSpan5);

$table->addRow();
$table->addCell($fullWidth, $cellColSpan6)->addText('Part B: Study Particulars');

$table->addRow();
$table->addCell($cell1Width)->addText('Protocol Title:');
$table->addCell($cell2to6Width, $cellColSpan5);

$table->addRow();
$table->addCell($cell1Width)->addText('Disease/Medical Condition to be Studied:');
$table->addCell($cell2to6Width, $cellColSpan5);

$table->addRow();
$table->addCell($cell1Width, $cellRowSpan)->addText('Target No. of Participants:');
$table->addCell($cell2Width, $cellRowSpan);
$table->addCell($cell34Width, $cellColSpan2)->addText('Participant Type:');
$table->addCell($cell56Width, $cellColSpan2)->addText('Patients        Healthy volunteers');

$table->addRow();
$table->addCell($cell1Width, $cellRowContinue);
$table->addCell($cell2Width, $cellRowContinue);
$table->addCell($cell34Width, $cellColSpan2)->addText('Participant Age Range:');
$table->addCell($cell56Width, $cellColSpan2)->addText('Lowest:  Highest:');

$table->addRow();
$table->addCell($cell1Width, $bgColor)->addText('Col 1');
$table->addCell($cell2Width, $bgColor)->addText('Col 2');
$table->addCell($cell3Width, $bgColor)->addText('Col 3');
$table->addCell($cell4Width, $bgColor)->addText('Col 4');
$table->addCell($cell5Width, $bgColor)->addText('Col 5');
$table->addCell($cell6Width, $bgColor)->addText('Col 6');


$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
// $objWriter->save('helloWorld.docx');

$tempFile = tempnam(sys_get_temp_dir(), 'PHPWord');
$objWriter->save($tempFile);

header("Content-Disposition: attachment; filename=test.docx");
readfile($tempFile);
unlink($tempFile);

Here is screenshot of output, enter image description here