是否可以使用PHP在网页内显示RTF文件? [英] Is it possible to display an RTF file inside a web page using PHP?

查看:153
本文介绍了是否可以使用PHP在网页内显示RTF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将标签替换为用户输入后,我有一个RTF文件,我想在网页内显示它.

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

我希望能够显示RTF文件,而无需在显示之前将其转换为某些内容.

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;

推荐答案

大多数浏览器无法可靠地显示RTF内容.可以将RTF解析为HTML,然后将HTML内容显示在您的网页上.

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.

您需要某种程序来解析RTF并将其转换为HTML.我假设它必须是免费的.我不知道有任何可靠的免费RTF解析或RTF到PHP中的HTML库.

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.

我建议您使用类似RTF2HTML的命令行转换程序: http://sageshome.net/?w=downloads/soft/RTF2HTML.html

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

您需要在网络服务器上下载并安装此程序,允许用户将文件上传到temp目录,然后使用shell_exec()从PHP调用命令行应用程序:

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);

然后,将结果解析为HTML并显示.不错的策略.请注意,如果要在另一个网页中显示内容,则可能需要删除head,body和其他标签.

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.

这篇关于是否可以使用PHP在网页内显示RTF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆