can someone tell me how to do this...

I have a page that contains reports and information.
For this reason, I thought of putting an rtf generator that converts HTML to RTF.
I've googled it, found some codes, but I cant understand...

Any one here know how to do it?

I also found this:

I downloaded it, try to run it in my localhost. I generates rtf but I can see the connection of html here...

help me pls.
thankyou!

Dani AI

Generated

Brief expert note: reported that the generated RTF still showed HTML markup, and 's suggestion later resolved the thread. The underlying issue is common: HTML must be translated into RTF control words — RTF is a different format, it does not render HTML/CSS, and a naive dump of HTML will show tags inside the .rtf file. Below is a minimal, practical approach and some troubleshooting pointers to make the conversion deterministic.

A simple, pragmatic PHP workflow:

  • decode HTML entities, unescape any encoded characters;
  • escape RTF-reserved characters (backslash and braces);
  • map a small set of HTML tags to RTF control words (b, i, u, br, p);
  • strip any remaining tags and wrap the result in a minimal RTF header.

Example (very small starter implementation — not a full parser):

<?php
function html_to_rtf($html) {
    $html = html_entity_decode($html, ENT_QUOTES, 'UTF-8');

    // escape RTF special chars: backslash and braces
    $html = str_replace(array('\\', '{', '}'), array('\\\\', '\\{', '\\}'), $html);

    // basic tag map (case-insensitive replacements)
    $map = array(
        '<br>'   => '\line ',
        '<br />' => '\line ',
        '<p>'    => '\par ',
        '</p>'   => '\par ',
        '<b>'    => '\b ',
        '</b>'   => '\b0 ',
        '<i>'    => '\i ',
        '</i>'   => '\i0 ',
        '<u>'    => '\ul ',
        '</u>'   => '\ul0 ',
    );
    $html = str_ireplace(array_keys($map), array_values($map), $html);

    $text = trim(strip_tags($html));
    $rtf  = '{\rtf1\ansi\deff0{\fonttbl{\f0 Times New Roman;}}\f0\fs24 ' . $text . '}';
    return $rtf;
}
?>

Cautions & troubleshooting:

  • The example is intentionally simple; nested tags, tables, images, fonts, and CSS are not supported. For complex reports use a dedicated converter or call a headless office tool (LibreOffice/pandoc/unoconv) to produce native RTF.
  • If raw tags still appear, check whether input was already HTML-escaped (e.g., &lt;b&gt;) or whether the library expects a different input format.
  • Inspect the generated .rtf in a plain-text editor to confirm whether tags are present or whether RTF control words are present but not rendered—different viewers (WordPad vs Word) may show differences.

Recommended Answers

All 2 Replies

COOL! Solved my problem!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.