PHP code example of clank-ai / rtf-converter

1. Go to this page and download the library: Download clank-ai/rtf-converter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

clank-ai / rtf-converter example snippets


from rtf_converter import rtf_to_txt

# Sample RTF text
rtf_text = r"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Helvetica;}}{\colortbl ;\red255\green0\blue0;}\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural\f0\fs24 \cf0 Hello, World!}"

# Convert RTF to plain text
plain_text = rtf_to_txt(rtf_text)
print(plain_text)  # Output: Hello, World!


error_reporting(E_ALL);
ini_set('display_errors', 1);

ansicpg1252\\deff0\\deflang1033
    {\\fonttbl{\\f0\\fswiss\\fcharset0 Helvetica;}{\\f1\\fswiss\\fcharset0 Arial;}}
    {\\colortbl ;\\red255\\green0\\blue0;\\red0\\green0\\blue255;}
    Hello, {\\f1\\b World}! This is {\\i italic} text. The — dash and the – dash are different.
    ‘ Quotes ’ are special too. Unicode: \\u8364 is the Euro symbol.
    Nested {\\b\\i bold and italic} text. Here's a list:
    \\list\\listtemplateid1\\listhybrid
    {\\listlevel\\levelnfc0\\leveljc0\\levelfollow0\\levelstartat1\\levelspace0\\levelindent0
    {\\*\\levelmarker\\fi-360\\li720\\jclisttab\\tx720 }{\\leveltext\\leveltemplateid1'\\uc0\\u8226 ;}
    {\\levelnumbers\\leveltemplateid2'\\u-3999 \\u8197 ;}}Hello from nested list!
    }";
    


echo "Before conversion\n";
$plainText = RtfConverter::rtfToTxt($rtfText);
echo "After conversion:\n";
echo $plainText . "\n";
echo "Type of output: " . gettype($plainText) . "\n";
echo "Length of output: " . strlen($plainText) . "\n";


// Include the RtfConverter class
d RTF file
$rtfContent = file_get_contents('sample.rtf');

// Convert RTF content to plain text
$plainText = RtfConverter::rtfToTxt($rtfContent);

// Save plain text to a new file
file_put_contents('output.txt', $plainText);

echo "RTF has been successfully converted to plain text and saved as output.txt.";


// Include the RtfConverter class
ple RTF text (potentially incorrect format)
$rtfText = "{\\rtf1\\ansi\\Hello, World!}";

try {
    // Attempt to convert RTF to plain text
    $plainText = RtfConverter::rtfToTxt($rtfText);
    echo $plainText;
} catch (Exception $e) {
    echo "An error occurred during the conversion: " . $e->getMessage();
}