16

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;
Ólafur Waage
  • 68,817
  • 22
  • 142
  • 198
Mark
  • 1,368
  • 5
  • 13
  • 26
  • Everyone is suggesting that this isn't possible, but I understand what Mark is after. Sometimes the browser will show a PDF or Word doc inline, embedded inside the browser. Not sure why it sometimes does it. I thought inline/attachment was the only thing that controlled this behavior. – Sarel Botha Mar 24 '09 at 16:53
  • Will the user have MS Word installed always? – Sarel Botha Mar 24 '09 at 16:53
  • EXACTLY!!!! You understand what I am after. I can't guarantee that the user will have Word installed. That's why I wanted to display it inline. I guess I am going to have to convert it to something else like JPG. Ultimately I will need to convert it to PDF, but that is a different problem. – Mark Mar 24 '09 at 16:58

7 Answers7

9

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.

danieltalsky
  • 7,752
  • 5
  • 39
  • 60
  • 2
    What about the (crazy I know) requirement: "I would like to be able to display the RTF file without having to convert it to something before displaying it."? – Milen A. Radev Mar 24 '09 at 17:00
  • 4
    Yeah, I got the requirement. Rather than just tell him he's SOL, like some other did, I thought I'd tell him he was SOL while providing a solution that has a chance of working. I assumed he just doesn't want to have to implement an RTF parser himself. This way he doesn't have to. – danieltalsky Mar 24 '09 at 17:36
5

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.

Tom
  • 653
  • 1
  • 8
  • 15
3

You needed an RTF to HTML converter written in PHP. I think this page contains your solution:

http://www.websofia.com/2014/05/a-working-rtf-to-html-converter-in-php/

Nuri Akman
  • 792
  • 3
  • 18
  • 41
2

First: you've got your content-type wrong. for RTF it's text/rtf

Second: you'll only be able to display in-line this type of content, which can be rendered by the web browser. RTF is not one of these. So you won't be able to display it in-line without converting it, or without some plug-in for the browser. Of course conversion might be on-the-fly.

vartec
  • 131,205
  • 36
  • 218
  • 244
0

Web pages can only contain HTML. You would need a browser plugin like flash to display other file types. See Scribd for example.

matpie
  • 17,033
  • 9
  • 61
  • 82
0

This isn't exactly an answer to your question, but one thing you may want to do is remove the "filename=mark.rtf" from the header. I've had browsers treat something as a download if I include "filename" in the header, even if the "Content-Disposition" is "inline".

Josh Mouch
  • 3,480
  • 1
  • 37
  • 34
-3

You can't just output a file from within PHP code. You need to extract the data from it, then print the contents inline.

The php function 'file_get_contents' may do what you need. The functions manual is here: http://us2.php.net/filegetcontents

A sample usage is here:

$contents = file_get_contents('yourfile.rtf');
Jon Winstanley
  • 23,010
  • 22
  • 73
  • 116
  • doing this does the same thing as using fread(); it outputs the all of the rtf code instead of the actual rtf document. can I even display an RTF document in a webpage or do I need to convert it to something else first? – Mark Mar 24 '09 at 16:45
  • As far as I know, you need to get the content out of it first. Otherwise the browser does not know what to do with it. – Jon Winstanley Mar 24 '09 at 16:50
  • Yeah, this just straight won't work. If you print the results of this call to the screen, the user will see a bunch of RTF escape sequences, which won't look like the document they're expecting. – danieltalsky Mar 24 '09 at 16:59
  • My answer is pretty similar to the guys above, I wonder why I got down voted? It's a hard life for us low-reputation users... – Jon Winstanley Mar 27 '09 at 09:39
  • Maybe because he has the extra step to call rtf2html. Without that your solution will just print a bunch of rtf text, which isn't readable (by non-programmers). – jm. Nov 23 '10 at 18:34