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

后端 未结 10 760
难免孤独
难免孤独 2020-11-22 07:32

I have successfully done code to display a PDF file in the browser instead of the \"Open/Save\" dialog. Now, I\'m stuck trying to display a Word document in the browser. I w

相关标签:
10条回答
  • 2020-11-22 08:07

    PDFTron WebViewer supports rendering of Word (and other Office formats) directly in any browser and without any server side dependencies. To test, try https://www.pdftron.com/webviewer/demo

    0 讨论(0)
  • 2020-11-22 08:10

    ViewerJS is helpful to view/embed openoffice format like odt,odp,ods and also pdf.

    For embed openoffice/pdf document

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

    /ViewerJS/ is the path of ViewerJS

    #../demo/ohm2013 is the path of your file want to embed

    0 讨论(0)
  • 2020-11-22 08:15

    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.

    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.

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

    Example:

    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.

    Edit:

    Huge thanks to fatbotdesigns for posting the Microsoft Office 365 viewer in the comments.

    <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. If this is unacceptable, then this method of display isn't the proper course of action.

    Live Examples:

    Google Docs Viewer

    Microsoft Office Viewer

    0 讨论(0)
  • 2020-11-22 08:16

    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.

    Save document(name.docx) in word as single file webpage (name.mht) In your html use

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

    Alter the heights and widths as you see fit.

    0 讨论(0)
提交回复
热议问题