是否可以使用 PHP 在网页中显示 RTF 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/678192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:28:28  来源:igfitidea点击:

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

phpdocumentrtf

提问by Mark

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.

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

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;

采纳答案by danieltalsky

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 内容。但是,可以将 RTF 解析为 HTML,然后在您的网页上显示 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.

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

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

我建议你使用像 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():

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

$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.

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

回答by Tom

You might want to check out https://github.com/tbluemel/rtf.jsfor 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.

您可能想查看https://github.com/tbluemel/rtf.js以获取客户端 RTF 渲染。它仍处于早期阶段,但它甚至可以渲染嵌入式图形。但是,对渲染嵌入的 WMF 图稿的支持仍然非常有限,并且需要浏览器支持标签。

回答by Nuri Akman

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

您需要一个用 PHP 编写的 RTF 到 HTML 转换器。我认为此页面包含您的解决方案:

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

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

回答by vartec

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

第一:您的内容类型错误。对于 RTF,它是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.

第二:您将只能在线显示这种类型的内容,这些内容可以由 Web 浏览器呈现。RTF 不是其中之一。因此,如果不进行转换,或者没有浏览器的一些插件,您将无法在线显示它。当然,转换可能是即时的。

回答by matpie

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

网页只能包含 HTML。您需要一个像 flash 这样的浏览器插件来显示其他文件类型。例如,参见Scribd

回答by Josh Mouch

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".

这不完全是您问题的答案,但您可能想要做的一件事是从标题中删除“filename=mark.rtf”。如果我在标题中包含“文件名”,即使“内容处理”是“内联”,浏览器也会将某些内容视为下载。

回答by Jon Winstanley

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

您不能只从 PHP 代码中输出文件。您需要从中提取数据,然后内联打印内容。

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

php 函数'file_get_contents' 可以满足您的需求。功能手册在这里:http: //us2.php.net/filegetcontents

A sample usage is here:

示例用法在这里:

$contents = file_get_contents('yourfile.rtf');