简体   繁体   English

如何使用 JavaScript 在浏览器中呈现 Word 文档(.doc、.docx)?

[英]How do I render a Word document (.doc, .docx) in the browser using JavaScript?

I have successfully done code to display a PDF file in the browser instead of the "Open/Save" dialog.我已成功完成代码以在浏览器中显示 PDF 文件而不是“打开/保存”对话框。 Now, I'm stuck trying to display a Word document in the browser.现在,我无法尝试在浏览器中显示 Word 文档。 I want to display a Word document in Firefox, IE7+, Chrome etc.我想在Firefox、IE7+、Chrome等浏览器中显示一个Word文档。

Can any one help?任何人都可以帮忙吗? I am always getting the "Open/Save" dialog while displaying the Word doc in browser.在浏览器中显示 Word 文档时,我总是会收到“打开/保存”对话框。 I want to implement this functionality using JavaScript.我想使用 JavaScript 实现此功能。

No browsers currently have the code necessary to render Word Documents, and as far as I know, there are no client-side libraries that currently exist for rendering them either.目前没有浏览器具有呈现 Word 文档所需的代码,据我所知,目前也没有用于呈现它们的客户端库。

However, if you only need to display the Word Document, but don't need to edit it, you can use Google Documents' Viewer via an <iframe> to display a remotely hosted .doc / .docx .但是,如果您只需要显示 Word 文档而不需要对其进行编辑,则可以通过<iframe>使用 Google Documents 的查看器来显示远程托管的.doc / .docx

<iframe src="https://docs.google.com/gview?url=http://remote.url.tld/path/to/document.doc&embedded=true"></iframe>

Solution adapted from " How to display a word document using fancybox ".解决方案改编自“ How to display a word document using fancybox ”。

Example:例子:

JSFiddle JSFiddle

However, if you'd rather have native support, in most, if not all browsers, I'd recommend resaving the .doc / .docx as a PDF file Those can also be independently rendered using PDF.js by Mozilla.但是,如果您希望在大多数(如果不是所有)浏览器中获得本机支持,我建议将.doc / .docx重新保存为 PDF 文件。这些也可以由 Mozilla 使用PDF.js独立呈现。

Edit:编辑:

Huge thanks to cubeguerrero for posting the Microsoft Office 365 viewer in the comments.非常感谢cubeguerrero在评论中发布 Microsoft Office 365 查看器。

<iframe src='https://view.officeapps.live.com/op/embed.aspx?src=http://remote.url.tld/path/to/document.doc' width='1366px' height='623px' frameborder='0'>This is an embedded <a target='_blank' href='http://office.com'>Microsoft Office</a> document, powered by <a target='_blank' href='http://office.com/webapps'>Office Online</a>.</iframe>

One more important caveat to keep in mind, as pointed out by lightswitch05 , is that this will upload your document to a third-party server.正如lightswitch05所指出的,要记住的一个更重要的警告是,这会将您的文档上传到第三方服务器。 If this is unacceptable, then this method of display isn't the proper course of action.如果这是不可接受的,那么这种显示方法就不是正确的做法。

Live Examples:活生生的例子:

Google Docs Viewer谷歌文档查看器

Microsoft Office Viewer微软办公室查看器

The answers by Brandon and fatbotdesigns are both correct, but having implemented the Google docs preview, we found multiple .docx files that couldn't be handled by Google. Brandon 和 fatbotdesigns 的答案都是正确的,但是在实现了 Google 文档预览后,我们发现了多个 Google 无法处理的 .docx 文件。 Switched to the MS Office Online preview and works likes a charm.切换到 MS Office Online 预览版,效果非常棒。

My recommendation would be to use the MS Office Preview URL over Google's.我的建议是使用 MS Office Preview URL 而不是 Google 的。

https://view.officeapps.live.com/op/embed.aspx?src=http://remote.url.tld/path/to/document.doc' 

There are a few js libraries that can handle .docx (not .doc) to html conversion client-side (in no particular order):有一些 js 库可以在客户端处理 .docx(不是 .doc)到 html 转换(无特定顺序):

Note: If you are looking for the best way to convert a doc/docx file on the client side, then probably the answer is don't do it .注意:如果您正在寻找在客户端转换 doc/docx 文件的最佳方法,那么答案可能是不要这样做 If you really need to do it then do it server-side, ie with libreoffice in headless mode , apache-poi (java) , pandoc etc.如果你真的需要这样做,那么在服务器端进行,即在无头模式下使用 libreofficeapache-poi (java)pandoc等。

A great solution if your data is confidential如果您的数据是机密的,这是一个很好的解决方案

Since the documents are confidential, they should not be processed on third-party resources.由于这些文件是机密文件,因此不应在第三方资源上处理它们。
This solution is open-source :此解决方案是开源的

  1. On the server-side: use Gotenberg to convert word & excel files to PDF.在服务器端:使用Gotenberg将 word 和 excel 文件转换为 PDF。
    Note: Gotenberg works like a charm, it is based on the LibreOffice engine.注意:Gotenberg 就像一个魅力,它基于 LibreOffice 引擎。
  2. On the frontend: It's very easy to render the PDF file with javascript.在前端:使用 javascript 渲染 PDF 文件非常容易。 (You can use libraries like: pdf.js , react-pdf , etc.) (您可以使用以下库: pdf.jsreact-pdf等)

ViewerJS is helpful to view/embed openoffice format like odt,odp,ods and also pdf. ViewerJS有助于查看/嵌入 openoffice 格式,如 odt、odp、ods 和 pdf。

For embed openoffice/pdf document用于嵌入 openoffice/pdf 文档

<iframe src = "/ViewerJS/#../demo/ohm2013.odp" width='700' height='550' allowfullscreen webkitallowfullscreen></iframe>

/ViewerJS/ is the path of ViewerJS /ViewerJS/是 ViewerJS 的路径

#../demo/ohm2013 is the path of your file want to embed #../demo/ohm2013是您要嵌入的文件的路径

I think I have an idea.我想我有个主意。 This has been doing my nut in too and I'm still having trouble getting it to display in Chrome.这也让我很疯狂,但我仍然无法让它在 Chrome 中显示。

Save document(name.docx) in word as single file webpage (name.mht) In your html use将 word 中的文档(name.docx)保存为单个文件网页(name.mht)在您的 html 中使用

<iframe src= "name.mht" width="100%" height="800"> </iframe>

Alter the heights and widths as you see fit.更改您认为合适的高度和宽度。

If you wanted to pre-process your DOCX files, rather than waiting until runtime you could convert them into HTML first by using a file conversion API such as Zamzar .如果您想预处理 DOCX 文件,而不是等到运行时,您可以先使用Zamzar等文件转换 API 将它们转换为 HTML。 You could use the API to programatically convert from DOCX to HMTL, save the output to your server and then serve that HTML up to your end users.您可以使用 API 以编程方式从 DOCX 转换为 HMTL,将输出保存到您的服务器,然后将该 HTML 提供给您的最终用户。

Conversion is pretty easy:转换非常简单:

curl https://api.zamzar.com/v1/jobs \
-u API_KEY: \
-X POST \
-F "source_file=@my.docx" \
-F "target_format=html5"

This would remove any runtime dependencies on Google & Microsoft's services (for example if they were down, or you were rate limited by them).这将消除对 Google 和 Microsoft 服务的任何运行时依赖项(例如,如果它们出现故障,或者您受到它们的速率限制)。

It also has the benefit that you could extend to other filetypes if you wanted (PPTX, XLS, DOC etc)它还有一个好处是您可以根据需要扩展到其他文件类型(PPTX、XLS、DOC 等)

Native Documents (in which I have an interest) makes a viewer (and editor) specifically for Word documents (both legacy binary .doc and modern docx formats). Native Documents(我对此感兴趣)专门为 Word 文档(传统二进制 .doc 和现代 docx 格式)制作了查看器(和编辑器)。 It does so without lossy conversion to HTML.它不会有损地转换为 HTML。 Here's how to get started https://github.com/NativeDocuments/nd-WordFileEditor/blob/master/README.md以下是如何开始https://github.com/NativeDocuments/nd-WordFileEditor/blob/master/README.md

PDFTron WebViewer supports rendering of Word (and other Office formats) directly in any browser and without any server side dependencies. PDFTron WebViewer 支持直接在任何浏览器中呈现 Word(和其他 Office 格式),而无需任何服务器端依赖项。 To test, try https://www.pdftron.com/webviewer/demo要测试,请尝试https://www.pdftron.com/webviewer/demo

您还可以使用一些现有的 API,例如 GroupDocs.Viewer,它可以将您的文档转换为图像或 html,然后您就可以在您自己的应用程序中显示它。

Based on some research I got it for IMAGES, PDF, Document and Excel Files preview in Iframe根据一些研究,我在 Iframe 中为IMAGES、PDF、文档和 Excel文件预览获得了它

<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="//code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>


<script type="text/javascript">
    $(document).ready(function() {
    $('#btnShow').click(function(){
        $("#dialog").dialog({
        height: 600,
        width: 1000,
        show: 'fold',
        modal: true
        });

        var toolbar = "#toolbar=0";

        //pdf
        //$("#frame").attr("src", "pdf_file_url.pdf"+toolbar);
        //image
        //$("#frame").attr("src", "image_file_url.jpeg"+toolbar);

        //document
        $("#frame").attr("src", "https://docs.google.com/gview?embedded=true&url=doc_file_url.docx");

        //excel

      // $("#frame").attr("src", "https://docs.google.com/gview?embedded=true&url=excel_file_url.xlsx");

    }); 
    });
</script>

<a href="javascript:void(0)" id="btnShow">Click to open</a>
<div id="dialog" style="display: none;">
    <div>
    <iframe id="frame" sandbox="allow-forms allow-modals allow-orientation-lock allow-pointer-lock allow-presentation allow-same-origin allow-scripts allow-top-navigation allow-top-navigation-by-user-activation" style="height:100%;width:100%;border: 0px;"></iframe>
    </div>
</div>

使用 Libre Office API 这是一个示例

libreoffice --headless --convert-to html docx-file-path --outdir html-dir-path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM