Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to display an RTF file inside a web page using PHP?

Tags:

php

document

rtf

I have an RTF file that I want to display inside a web page after tags have been replaced with user input.

I would like to be able to display the RTF file without having to convert it to something before displaying it.

Every time I try it now it gives me the popup open/save box even though I am telling it to display it inline with:

header("Content-type: application/msword");
header("Content-Disposition: inline; filename=mark.rtf");
header("Content-length: " . strlen($output));

echo $output;
like image 790
Mark Avatar asked Mar 24 '09 16:03

Mark


2 Answers

Most browsers won't reliably display RTF content. It IS possible to parse the RTF into HTML, and display the HTML content on your web page however.

You need some kind of program to parse RTF and convert it to HTML. I'm assuming it has to be free. I do not know of any reliable free RTF parsing or RTF to HTML libraries in PHP.

I recommend you use a command-line conversion program like RTF2HTML: http://sageshome.net/?w=downloads/soft/RTF2HTML.html

You would need to download and install this program on your webserver, allow the user to upload the file to a temp directory, and then call the command line application from PHP with shell_exec():

$html_output_path = '/path/for/processing/files/'
$html_output_filename = $username . $timestamp;
if (is_uploaded_file($_FILES['userfile']['tmp_name'])
{
  shell_exec('rtf2html ' . 
    escapeshellarg($_FILES['userfile']['tmp_name']) . " " .
    $html_output_path . $html_output_filename);
}
$html_to_display = file_get_contents($html_output_path . 
  $html_output_filename);

Then, parse the results as HTML and display them. Not a bad strategy. Note that you will probably need to remove the head, body and possibly other tags if you're going to display the content inside another web page.

like image 155
danieltalsky Avatar answered Oct 06 '22 03:10

danieltalsky


You might want to check out https://github.com/tbluemel/rtf.js for client-side RTF rendering. It's still in its early stages but it renders even embedded graphics. Support for rendering embedded WMF artwork is still very very limited, though, and requires browser support for the tag.

like image 41
Tom Avatar answered Oct 06 '22 05:10

Tom