This is a proof of concept for generating text documents (Open Document (odt), Word (docx) and Portable Document Format (pdf)) files from HTML and table data using the PhpWord library. Edit the HTML and table data below and press one of the document type button at the bottom to create a document.
Some well formed HTML snippet needs to be used
With for example some1 inline formatting1
Without paragraph tag some1 inline formatting1Unordered (bulleted) list:
Ordered (numbered) list:
<?php
require_once 'vendor/phpoffice/phpword/src/PhpWord/Autoloader.php';
PhpWord\Autoloader::register();
$phpWord = new PhpWord\PhpWord();
$section = $phpWord->addSection();
PhpWord\Shared\Html::addHtml($section, $html);
if ($data) {
$phpWord->addTableStyle('tableStyle', array(
'borderSize' => 1,
'borderColor' => 'EEEEEE',
));
$table = $phpWord->addSection()->addTable('tableStyle');
if (!empty($columns)) {
$row = $table->addRow(null, array());
foreach ($columns as $name) {
$row->addCell(2500, array(
'bgColor' => 'DDDDDD',
'valign' => 'center'))
->addText(htmlspecialchars($name), array(
'bold' => true,
'align' => 'center'));
}
}
foreach ($data as $set) {
$row = $table->addRow();
foreach ($set as $item) {
$row->addCell()->addText(htmlspecialchars($item));
}
}
}
switch ($extension) {
case 'pdf':
$writer = PhpWord\IOFactory::createWriter($phpWord, 'PDF');
break;
case 'docx':
$writer = PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
break;
case 'odt':
$writer = PhpWord\IOFactory::createWriter($phpWord, 'ODText');
break;
}
$writer->save($file);
?>