RTF to HTML converter
 Version 1.2 for PHP 4 and 5 (including 5.5)
requires ionCube Loader, Zend Optimiser / Zend Loaded or NuSphere PHP Express
 
Converting RTF Files to HTML Using RTF to HTML converter
RTF to HTML Converter is a PHP based utility which allows you to convert RTF documents into HTML code. The code produced can then be directly output to a web page or saved to a HTML file. The converter can also produce simple HTML from plain text (Plain text to HTML converter).
The converter supports conversion including fonts, colours (including background colours), special characters, hyper-links etc.

The RTF to HTML converter system comes in two types for both RTF and Plain text. These are basic functions and a class based system.
1. Functions
1.1 RTF functions
There are two functions associated with the conversion of RTF.

1.1.1 Converting a string holding the RTF source:
convert_rtf_to_html($rtfdata, $showerrors /*false*/, $showconversiontime /*false*/)

Where:
$rtfdata is a string containing the source RTF code.
$showerrors is an optional Boolean, to indicate if you want errors to be displayed at the end of the conversion.
$showconversiontime is an optional Boolean to add the time taken to convert the string, to the end of the output.

Example:
require("rtf.php");
$rtf = "{\rtf1\mac\ansicpg10000\cocoartf824\cocoasubrtf410\r\n";
$rtf .= "{\fonttbl\f0\fswiss\fcharset77 Helvetica;}\r\n";
$rtf .= "{\colortbl;\red255\green255\blue255;}\r\n";
$rtf .= "\paperw11900\paperh16840\margl1440\margr1440\vieww9000\viewh8400\viewkind0\r\n";
$rtf .= "\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\ql\qnatural\pardirnatural\r\n";
$rtf .= "\f0\fs24 \cf0 I am rich text}\r\n";
convert_rtf_to_html($rtf);

1.1.2 Loading the RTF file from the hard drive
require("rtf.php");
convert_rtf_file_to_html($file, $showerrors /*false*/, $showconversiontime /*false*/)

Where:
$file is a string containing the path and file name relative to the web servers root path.
$showerrors is an optional Boolean, to indicate if you want errors to be displayed at the end of the conversion.
$showconversiontime is an optional Boolean to add the time taken to convert the string, to the end of the output.

Example:
require("rtf.php");
convert_rtf_file_to_html("documentation/mydocument.rtf");

1.2 Plain text functions
There are two functions associated with the conversion of plain text.

1.2.1 Converting a string holding the plain text:
convert_txt_to_html($textdata, $showconvtime /*false*/)

Where:
$textdata is a string containing the plain text.
$showconversiontime is an optional Boolean to add the time taken to convert the string, to the end of the output.

Example:
require("rtf.php");
$text = "I am plain text\r\n";
convert_txt_to_html($text);

1.2.2 Loading the text file from the hard drive
convert_txt_file_to_html($file, $showconvtime /*false*/)

Where:
$file is a string containing the path and file name relative to the web servers root path.
$showconversiontime is an optional Boolean to add the time taken to convert the string, to the end of the output.

Example:
require("rtf.php");
convert_txt_file_to_html("documentation/mydocument.txt");

2. Classes
2.1 RTF class
The RTF class has a basic set of methods to control its use. These are listed below

2.1.1 The class constructor
RTFConv($rtfdata)

Where:
$rtfdata is a string containing the source RTF code.

2.1.2 The output type
SetOutput($type)

Where:
$type is either "dhtml" or "html", where 'dhtml' signifies a dynamic output i.e. the conversion is output directly using print. Note: Font styles are embedded into each font element and 'html' (recommended) signifies that the output is buffered into a string that can be output using ShowOutput or accessed directly.

2.1.3 The document format
UseDocumentFormat($use /*true*/)

Where:
$use is optional and can be either true for converting the document settings or false for ignoring them. The document settings are the width and margins set using applications such as Word.

2.1.4 Showing errors (used in conjunction with ShowOutput)
ShowErrors($show /*true*/)

Where:
$show is optional and can be either true for showing any errors when using ShowOutput, or false for ignoring them.

2.1.5 Setting the output level
SetOutputLevel($level)

Where:
$level is the amount of formatting required: 
 0 = no formatting 
 1 = basic formatting e.g. <i> and <b> 
 2 = full formatting such as fonts styles and tables 
 Defaults to 2 if not set.

2.1.6 Show the time taken to convert the source in the output
ShowConversionTime($showconvtime /*true*/)

Where:
$showconvtime is optional and can be either true for showing the conversion time, or false for ignoring it.

2.1.7 Converting the RTF data 
Convert()

The Convert method will (depending on the output type) either output to a buffer for later access or use by ShowOutput, or, it will output the conversion directly to the client using print.

2.1.7 Showing the output (i.e. printing it to the client)
ShowOutput($showstyles /*false*/, $file /*""*/)

Where:
$showstyles is optional and can be either true (recommended) for showing the styles used for the fonts, or false for ignoring them.
$file is optional and can be the path and name of a file to redirect the output to e.g. "documentation/mynewdocument.html".

Note: You can also use the class variables:
styles which holds the list of font styles needed for the converted RTF.
out which holds the buffered conversion.
err which contains an array of error messages e.g. err[0] holds the first error, err[1] the second etc..

Example:
require("rtf.php");
$r = new RTFConv($rtf);
$r->SetOutput("html");
$r->ShowConversionTime($showconvtime);
$r->ShowErrors(true);
$r->Convert();
$r->ShowOutput(true);

(where $rtf is the RTF data e.g. $rtf = file_get_contents("mydocument.rtf"); 

2.2 Plain text class
The plain text class has a basic set of methods to control its use. These are listed below

2.2.1 The class constructor
PlainTextConv($text)

Where:
$text is a string containing the plain text to be converted.

2.2.2 Setting the font to use
SetFont($font, $fontsize /*12*/)

Where:
$font is a standard web font such as 'Arial' to use for the entire conversion.
$fontsize is an optional size defining the font size to use for the entire conversion.

2.2.3 Show the time taken to convert the text in the output
ShowConversionTime($showconvtime /*true*/)

Where:
$showconvtime is optional and can be either true for showing the conversion time, or false for ignoring it.

2.2.4 Converting the plain text data 
Convert()

The Convert method will (depending on the output type) either output to a buffer for later access or use by ShowOutput, or, it will output the conversion directly to the client using print.

2.2.5 Showing the output (i.e. printing it to the client)
ShowOutput($file /*""*/)

Where:
$file is optional and can be the path and name of a file to redirect the output to e.g. "documentation/mynewdocument.html".

Note: You can also use the class variable:
out which holds the buffered conversion.

Example:
$r = new PlainTextConv($text);
$r->SetFont("Helvetica", 10);
$r->ShowConversionTime($showconvtime);
$r->Convert();
$r->ShowOutput();

License

THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

RTF to HTML converter is Copyright © 2007-2014 Pa-software. All rights reserved.