Skip to content

Instantly share code, notes, and snippets.

@theredstapler
Last active December 19, 2017 19:15
Show Gist options
  • Save theredstapler/af74652fd68eb7f522ac6b42aa85752d to your computer and use it in GitHub Desktop.
Save theredstapler/af74652fd68eb7f522ac6b42aa85752d to your computer and use it in GitHub Desktop.
Source code of PHPWord Tutorial by Red Stapler. View Tutorial at: https://redstapler.co/tutorials/phpword-intro/
<?php
require_once 'vendor/autoload.php';
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $_POST['htmlstring']);
$fancyTableStyleName = 'Fancy Table';
$fancyTableStyle = array('borderSize' => 1, 'borderColor' => 'd2d2d2', 'cellMargin' => 40, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER);
$fancyTableFirstRowStyle = array('bgColor' => 'DDDDDD');
$fancyTableCellStyle = array('valign' => 'center');
$fancyTableFontStyle = array('bold' => true);
$phpWord->addTableStyle($fancyTableStyleName, $fancyTableStyle, $fancyTableFirstRowStyle);
$table = $section->addTable($fancyTableStyleName);
$table->addRow(900);
$table->addCell(2000, $fancyTableCellStyle)->addText('Row 1', $fancyTableFontStyle);
$table->addCell(2000, $fancyTableCellStyle)->addText('Row 2', $fancyTableFontStyle);
$table->addCell(2000, $fancyTableCellStyle)->addText('Row 3', $fancyTableFontStyle);
$table->addCell(2000, $fancyTableCellStyle)->addText('Row 4', $fancyTableFontStyle);
for ($i = 1; $i <= 8; $i++) {
$table->addRow();
$table->addCell(2000)->addText("Cell {$i}");
$table->addCell(2000)->addText("Cell {$i}");
$table->addCell(2000)->addText("Cell {$i}");
$table->addCell(2000)->addText("Cell {$i}");
}
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;filename="test.docx"');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('php://output');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment