This is a very small library that is capable of converting HTML documents to DOCX format that is used by Microsoft Word 2007 and onward. 这是一个非常小的库,能够将HTML文档转换为MicrosoftWord2007及以后使用的DOCX格式。It manages to perform the conversion in the browser by using a feature called 'altchunks'. 它通过使用名为“altchunks”的功能在浏览器中执行转换。In a nutshell, it allows embedding content in a different markup language. 简而言之,它允许在不同的标记语言中嵌入内容。We are using MHT document to ship the embedded content to Word as it allows to handle images. 我们使用MHT文档将嵌入的内容发送到Word,因为它允许处理图像。After Word opens such file, it converts the external content to Word Processing ML (this is how the markup language of DOCX files is called) and replaces the reference.Word打开此类文件后,会将外部内容转换为字处理ML(这是DOCX文件的标记语言的调用方式),并替换引用。
Altchunks were not supported by Microsoft Word for Mac 2008 and are not supported by LibreOffice and Google Docs.Microsoft Word for Mac 2008不支持AltChunk,LibreOffice和Google Docs也不支持AltChunk。
This library should work on any modern browser that supports 这个库应该适用于任何支持Blobs
(either natively or via Blob.js). Blob
的现代浏览器(原生支持或通过Blob.js支持)。It was tested on Google Chrome 36, Safari 7 and Internet Explorer 10.它在谷歌Chrome 36、Safari 7和Internet Explorer10上进行了测试。
It also works on Node.js (tested on v0.10.12) using 它还可以在Node.js(在v0.10.12上测试)上使用Buffer
instead of Blob
.Buffer
而不是Blob
。
This library supports only inlined base64 images (sourced via DATA URI). 此库仅支持内联base64映像(通过数据URI来源)。But it is easy to convert a regular image (sourced from static folder) on the fly. 但是,动态转换常规图像(来自静态文件夹)很容易。If you need an example of such conversion you can checkout a demo page source (see function 如果您需要此类转换的示例,可以查看演示页面源代码(请参阅函数convertImagesToBase64
).convertImagesToBase64
)。
Very minimal demo is available as 非常简单的演示可以在存储库和在线上以test/sample.html
in the repository and online. test/sample.html
的形式提供。Please note that saving files on Safari is a little bit convoluted and the only reliable method seems to be falling back to a Flash-based approach (such as Downloadify). 请注意,在Safari上保存文件有点复杂,唯一可靠的方法似乎是采用基于Flash的方法(如Downloadify)。Our demo does not include this workaround to keep things simple, so it will not work on Safari at this point of time.我们的演示没有包含这个使事情简单化的解决方法,因此目前它在Safari上不起作用。
You can also find a sample for using it in Node.js environment here.您还可以在Node.js环境中找到使用它的示例。
To generate DOCX, simply pass a HTML document (as string) to 要生成DOCX,只需将一个HTML文档(作为字符串)传递给asBlob
method to receive Blob
(or Buffer
) containing the output file.asBlob
方法,以接收包含输出文件的Blob
(或Buffer
)。
var converted = htmlDocx.asBlob(content);
saveAs(converted, 'test.docx');
asBlob
can take additional options for controlling page setup for the document:可以使用其他选项来控制文档的页面设置:
orientation
: landscape
or portrait
(default)margins
top
: number (default: 1440, i.e. 2.54 cm)right
: number (default: 1440)bottom
: number (default: 1440)left
: number (default: 1440)header
: number (default: 720)footer
: number (default: 720)gutter
: number (default: 0)For example:例如:
var _blob = htmlDocx.asBlob(content, {orientation: 'landscape', margins: {top: 720}});
saveAs(_blob, 'test.docx');
IMPORTANT: please pass a complete, valid HTML (including DOCTYPE, 请传递完整有效的HTML(包括DOCTYPE、html
and body
tags). html
和body
标记)。This may be less convenient, but gives you possibility of including CSS rules in 重要提示:这可能不太方便,但可以在style
tags.style
标记中包含CSS规则。
html-docx-js
is distributed as 'standalone' Browserify module (UMD). html-docx-js
作为“独立”浏览模块(UMD)分发。You can 您可以将它require
it as html-docx
. require
为html-docx
。If no module loader is available, it will register itself as 如果没有可用的模块加载程序,它将自己注册为window.htmlDocx
. window.htmlDocx
。See 有关详细信息,请参阅test/sample.html
for details.test/sample.html
。
Copyright (c) 2015 Evidence Prime, Inc. See the LICENSE file for license rights and limitations (MIT).