使用纯 PHP 代码创建/编写 MS RTF Word 文档
使用这个免费的库:https://github.com/phprtflite/PHPRtfLite
适用于 Windows、Linux 和 Mac OS
在此处查看文档:http://sigma-scripts.de/phprtflite/docs/
如果您现在想了解更多关于 Office 文件格式的信息,也请阅读此内容:
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');