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

发布于 2024-07-15 12:11:43 字数 321 浏览 4 评论 0原文

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

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

现在每次我尝试它时,它都会弹出打开/保存框,即使我告诉它内联显示它:

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

echo $output;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

挽手叙旧 2024-07-22 12:11:43

大多数浏览器无法可靠地显示 RTF 内容。 但是,可以将 RTF 解析为 HTML,并在网页上显示 HTML 内容。

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

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

您需要在网络服务器上下载并安装此程序,允许用户将文件上传到临时目录,然后从 PHP 调用命令行应用程序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 和可能的其他标签。

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.

一抹微笑 2024-07-22 12:11:43

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

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.

美羊羊 2024-07-22 12:11:43

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

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

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/

ま柒月 2024-07-22 12:11:43

首先:您的内容类型错误。 对于 RTF,它是 text/rtf

其次:您只能显示内联这种类型的内容,这些内容可以由网络浏览器呈现。 RTF 不是其中之一。 因此,如果不转换它,或者没有浏览器的某些插件,您将无法内嵌显示它。 当然,转换可能是即时的。

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.

天涯沦落人 2024-07-22 12:11:43

网页只能包含 HTML。 您需要一个浏览器插件(例如 flash)来显示其他文件类型。 例如,请参阅 Scribd

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

御守 2024-07-22 12:11:43

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

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

峩卟喜欢 2024-07-22 12:11:43

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

php 函数“file_get_contents”可以满足您的需要。 函数手册在这里: https://www.php.net/filegetcontents

示例用法在这里:

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

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: https://www.php.net/filegetcontents

A sample usage is here:

$contents = file_get_contents('yourfile.rtf');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文