Rtf Html Php is a PHP library that converts RTF documents to HTML using a pure-PHP parser, installed with Composer as henck/rtf-to-html.
What is Rtf Html Php?
Rtf Html Php is a pure PHP library that parses RTF (Rich Text Format) documents and converts them to HTML output. It reads an RTF string or file and produces an HTML representation through a HtmlFormatter class, running on any PHP installation with the mbstring extension enabled. The library was written by developer Henck (based on the Composer vendor name "henck") after existing RTF-to-HTML solutions failed to work or required external executables.
Key Features
- Pure PHP implementation — The converter works without shelling out to external executables like RTF2HTML; it parses RTF entirely with PHP code, using
mb_convert_encodingfor character handling. - Subset of RTF tags — The parser implements the RTF control words needed for HTML conversion and ignores the rest, keeping the code short and focused.
- Composer installation — Install with
composer require henck/rtf-to-html, then use namespacesRtfHtmlPhp\DocumentandRtfHtmlPhp\Html\HtmlFormatter. - Flexible output encoding — By default output uses
HTML-ENTITIES, and you can pass any encoding supported bymb_list_encodings()(for exampleUTF-8) to theHtmlFormatterconstructor. - Debug output — Echoing the
Documentobject prints the parser's parse tree, which helps developers inspect how the RTF was read. - Exception handling — The
Documentconstructor throws an exception if the RTF cannot be parsed; parse errors also generate PHP notices.
Who is it for?
- PHP developers building document converters — They can embed RTF-to-HTML conversion directly in their applications without relying on system-level RTF tools.
- Maintainers of legacy systems — Teams that handle Word-generated RTF exports can convert those documents to web-friendly HTML for display.
- Tool builders — Developers creating online document previewers or importers can use this library as a conversion step in a larger PHP pipeline.
What can you do with it?
- Convert RTF strings — Pass a raw RTF string to
new Document($rtf)and format it withHtmlFormatterto get HTML output. - Process RTF files — Read a
.rtffile withfile_get_contents()and feed the content toDocument, then echo the formatted HTML. - Control character encoding — Instantiate
HtmlFormatter('UTF-8')to receive UTF-8 HTML instead of the default HTML entities, matching your site's charset.
How does it work?
The library works in two steps: first, Document parses the RTF input into a parse tree; second, HtmlFormatter walks that tree and returns HTML. If the RTF is invalid, Document throws an exception. The conversion relies on PHP's mb_convert_encoding to handle Unicode RTF characters.
Pros and cons
- Pro: No external executables are invoked, so deployment is simpler and safer.
- Pro: The package is installable via Composer and exposes a small, documented API.
- Con: Requires the
php-mbstringmodule; without it the converter will not work.
FAQ
Does Rtf Html Php require any PHP extensions?
Yes, it depends on the mbstring extension because it uses mb_convert_encoding for character conversion. Most fresh PHP installations include this module, but you should verify your environment before using the library.
How do I install Rtf Html Php?
Run composer require henck/rtf-to-html in your project directory. After that, you can use the RtfHtmlPhp\Document and RtfHtmlPhp\Html\HtmlFormatter classes in your code.
Can I get UTF-8 HTML output?
Yes. Pass 'UTF-8' (or any encoding returned by mb_list_encodings()) to the HtmlFormatter constructor. The default output encoding is HTML-ENTITIES.
What happens if the RTF file is malformed?
The Document constructor throws an exception if the RTF cannot be parsed. During parsing, issues with specific control words may also generate PHP notices, which you can inspect for debugging.







