javascript vbscript 从 rtf 转换为 html

发布于 2024-08-12 08:37:33 字数 157 浏览 1 评论 0原文

有谁知道 RTF 到 HTML 转换器的任何 vbscript / javascript 实现吗?

我在 VB / c# 中看到过一些,但找不到任何对脚本版本的引用。在我开始写一个之前 - 有谁知道现有的开源项目可以处理这个问题吗?

非常感谢,

弗兰科

Does anyone know of any vbscript / javascript implementations of a RTF to HTML converter?

I have seen some in VB / c#, but cannot find any reference to a scripted version. Before I start to write one - does anyone know of an existing open source project that deals with this?

Many thanks,

Franko

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

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

发布评论

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

评论(2

无可置疑 2024-08-19 08:37:33

弗兰克 -

您没有指定您的目标平台。然而,您提到 VBScript 和 Javascript 的事实表明您至少使用的是基于 Windows 的计算机。如果是这样,并且您可以访问某个版本的 Word,则可以使用脚本自动执行转换,并将 Word 用作进程外服务器。即使如此,您也没有真正说明这是通过 Windows 会话还是通过 Web 服务器完成的。

如果您想从 Windows 会话中执行此操作,您可以使用以下由 Windows 脚本主机运行的 VBScript:

[Rtf2Html.vbs]

Option Explicit

Private Sub ConvertToHtml(documentFileName)
Const wdFormatHTML = 8
Dim fso
Dim wordApplication
Dim newDocument
Dim htmlFileName

    On Error Resume Next

    Set fso = WScript.CreateObject("Scripting.FileSystemObject")

    documentFileName = fso.GetAbsolutePathName(documentFileName)

    If Not fso.FileExists(documentFileName) Then
        WScript.Echo "The file '" & documentFileName & "' does not exist."
        WScript.Quit
    End If

    Set wordApplication = WScript.CreateObject("Word.Application")

    If Err.Number <> 0 Then
        Select Case Err.Number
        Case &H80020009
            WScript.Echo "Word not installed properly."
        Case Else
            ShowDefaultErrorMsg
        End Select
        wordApplication.Quit
        WScript.Quit
    End If

    Set newDocument = wordApplication.Documents.Open(documentFileName, False)

    If Err.Number <> 0 Then
        Select Case Err.Number
        Case Else
            ShowDefaultErrorMsg
        End Select
        wordApplication.Quit
        WScript.Quit
    End If

    ' Construct a file name which is the same as the original file, but with a different extension.
    htmlFileName = Left(documentFileName, InStrRev(documentFileName, ".")) & "htm"

    newDocument.SaveAs htmlFileName, wdFormatHTML

    newDocument.Close

    wordApplication.Quit

End Sub

Private Sub Main
Dim arguments

    Set arguments = WScript.Arguments

    If arguments.Count = 0 Then
        WScript.Echo "Missing file argument."
    Else
        ConvertToHtml arguments(0)
    End If

End Sub
bad 
Private Sub ShowDefaultErrorMsg
    WScript.Echo "Error #" & CStr(Err.Number) & vbNewLine & vbNewLine & Err.Description
End Sub

Main

如果您想从 Web 服务器中使用它,事情就是有点不同。您可以针对 ASP 页面调整 VBScript,或将其转换为 ASP.NET 页面。无论如何,您都必须用适当的内部对象替换 WSH 对象。但是,请注意:虽然可以使用 IIS 中的进程外服务器,但这通常是一个坏主意,除非您知道这将是一个容量极小的服务器。即使如此,Word 可能使用 GUI 元素这一事实也带来了潜在的危险,因为 Word 可能会在某些错误情况下显示对话。

在这种情况下,最好通过从服务器脚本 shell 到 Windows 脚本主机代码来断开两个进程的连接,并在适当的延迟后返回一个执行客户端拉取的页面。

Frank -

You didn't specify which platform you are targeting. However, the fact you mentioned VBScript as well as Javascript suggests you are at the least using a Windows-based machine. If so, and you have access to a version of Word, you could use a script automating a conversion, using Word as an out of process server. Even then, you didn't really say whether this is meant to be done from a Windows session, or via a web server.

If you wanted to do this from a Windows session, you could use the following VBScript, run by the Windows Scripting Host:

[Rtf2Html.vbs]

Option Explicit

Private Sub ConvertToHtml(documentFileName)
Const wdFormatHTML = 8
Dim fso
Dim wordApplication
Dim newDocument
Dim htmlFileName

    On Error Resume Next

    Set fso = WScript.CreateObject("Scripting.FileSystemObject")

    documentFileName = fso.GetAbsolutePathName(documentFileName)

    If Not fso.FileExists(documentFileName) Then
        WScript.Echo "The file '" & documentFileName & "' does not exist."
        WScript.Quit
    End If

    Set wordApplication = WScript.CreateObject("Word.Application")

    If Err.Number <> 0 Then
        Select Case Err.Number
        Case &H80020009
            WScript.Echo "Word not installed properly."
        Case Else
            ShowDefaultErrorMsg
        End Select
        wordApplication.Quit
        WScript.Quit
    End If

    Set newDocument = wordApplication.Documents.Open(documentFileName, False)

    If Err.Number <> 0 Then
        Select Case Err.Number
        Case Else
            ShowDefaultErrorMsg
        End Select
        wordApplication.Quit
        WScript.Quit
    End If

    ' Construct a file name which is the same as the original file, but with a different extension.
    htmlFileName = Left(documentFileName, InStrRev(documentFileName, ".")) & "htm"

    newDocument.SaveAs htmlFileName, wdFormatHTML

    newDocument.Close

    wordApplication.Quit

End Sub

Private Sub Main
Dim arguments

    Set arguments = WScript.Arguments

    If arguments.Count = 0 Then
        WScript.Echo "Missing file argument."
    Else
        ConvertToHtml arguments(0)
    End If

End Sub
bad 
Private Sub ShowDefaultErrorMsg
    WScript.Echo "Error #" & CStr(Err.Number) & vbNewLine & vbNewLine & Err.Description
End Sub

Main

If you want to use this from a webserver, things are a little different. You could adapt the VBScript for an ASP page, or convert it to an ASP.NET page. In any case, you will have to replace the WSH objects with the appropriate internal objects. However, be warned: whilst it is possible to use an out of process server from IIS, it is generally a bad idea, unless you know this is going to be an extremely low volume server. Even then, the fact that Word potentially uses GUI elements makes this potentially dangerous, because it is possible that Word might show a dialogue in some error condition.

In this case, it may be better to disconnect the two processes by shelling out from the server script to the Windows scripting host code, and instead return a page that does a client side pull after an appropriate delay.

软糯酥胸 2024-08-19 08:37:33

您对 PHP 的熟悉程度如何?这个类似乎可以解决问题,因此您可以按原样使用它,或者转换为 Javascript,甚至可以用作指南。

http://pastebin.ca/1688799

How comfortable are you with PHP? This class seems to do the trick, so you could either use it as is, or convert to Javascript, or even use as a guideline.

http://pastebin.ca/1688799

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