Create / Write a MS RTF Word Document by using this pure PHP Code
Use this free Lib: https://github.com/phprtflite/PHPRtfLite
Works on Windows, Linux and Mac OS
Take a look at the Documentation over here: http://sigma-scripts.de/phprtflite/docs/
Also read this if you what to now more about Office File Formats:
https://joelonsoftware.com/2008/02/19/why-are-the-microsoft-office-file-formats-so-complicated-and-some-workarounds/
<?php
/* ------------------------------------------------------------- */
/* [Create MS Word RTF] Text to Microsoft Word #php #class #word */
/* ------------------------------------------------------------- */
// SET HEADLINE & CONTENT
$headline = 'A Microsoft Word File created with PHP'; // H1-H6 TAGS WILL NOT WORK
$content = '
<bullet> Text <br><bullet> Text <br><bullet> Text <br><br>
<u>This is Lorem Ipsum Dolore Text</u>.
<strong>This is Lorem Ipsum Dolore Text</strong>.
<em>This is Lorem Ipsum Dolore Text</em>.
<b>This is Lorem Ipsum Dolore Text</b>.
<i>This is just another paragraph</i>. ';
// TAGS THAT WILL WORK: <bullet>, <br>, <strong>, <b>, <i>, <em> and <u>
// SET VAR
$dir = dirname(__FILE__);
// PHPRtfLite
require_once $dir . '/PHPRtfLite/lib/PHPRtfLite.php';
PHPRtfLite::registerAutoloader();
$rtf = new PHPRtfLite();
$sect = $rtf->addSection();
// ADD CONTENT AS HTML
// 'Arial' 'Verdana' 'Tahoma' Times New Roman' etc.
// TEXT_ALIGN_LEFT, TEXT_ALIGN_RIGHT, TEXT_ALIGN_CENTER, TEXT_ALIGN_JUSTIFY
$sect->writeText(
$headline,
new PHPRtfLite_Font(24, 'Arial'),
new PHPRtfLite_ParFormat(PHPRtfLite_ParFormat::TEXT_ALIGN_CENTER)
);
$sect->writeText(
$content,
new PHPRtfLite_Font(12, 'Arial'),
new PHPRtfLite_ParFormat(PHPRtfLite_ParFormat::TEXT_ALIGN_LEFT)
);
// SAVE RTF DOCUMENT
$rtf->save($dir . '/' . basename(__FILE__, '.php') . '.rtf');