Skip to content

Instantly share code, notes, and snippets.

@gaffling
Last active August 26, 2020 06:30
Show Gist options
  • Save gaffling/dcfe1dbb3da1f761488c26969b6f8545 to your computer and use it in GitHub Desktop.
Save gaffling/dcfe1dbb3da1f761488c26969b6f8545 to your computer and use it in GitHub Desktop.
[Create MS Word RTF] Text to Microsoft Word #php #class #word
<?php
/* ------------------------------------------------------------- */
/* [Create MS Word RTF] Text to Microsoft Word #php #class #word */
/* ------------------------------------------------------------- */
/**
* @uses https://github.com/phprtflite/PHPRtfLite
* @see http://sigma-scripts.de/phprtflite/docs/
* @see https://joelonsoftware.com/2008/02/19/why-are-the-microsoft-office-file-formats-so-complicated-and-some-workarounds/
*/
// SET HEADLINE & CONTENT (H1-H6 TAGS WILL NOT WORK)
$headline = 'A Microsoft Word File created with PHP';
// TAGS THAT WILL WORK: <bullet>, <br>, <strong>, <b>, <i>, <em> and <u>
$content = '
<bullet> Text <br><bullet> Text<br><bullet> Text <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>.';
// 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
$sect->writeText($headline,
new PHPRtfLite_Font(24, 'Verdana'),
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');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment