【发布时间】:2019-06-16 07:52:00
【问题描述】:
我只是在学习如何编码。我正在使用一个文本生成器,它允许用户编辑文本并将其导出为.DOCX,我在这个网站npmjs 上找到了这个脚本jsfiddl,这个脚本允许将 HTML 导出到 .DOCX,但我不知道如何让这个脚本以特定的 div 导出 HTML 文本,例如在 <div id="exportContent"> 中导出内容?
注意:当我尝试将 jsfiddl 中的脚本组织为 html 和 javascript 时,它不起作用。
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/docx@4.0.0/build/index.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.js"></script>
</head>
<body>
<h1>DOCX browser Word document generation</h1>
<button type="button" onclick="generate()">Click to generate document</button>
<script>
function generate() {
const doc = new Document();
const paragraph = new Paragraph("Hello World");
const institutionText = new TextRun("Foo Bar").bold();
const dateText = new TextRun("Github is the best").tab().bold();
paragraph.addRun(institutionText);
paragraph.addRun(dateText);
doc.addParagraph(paragraph);
const packer = new Packer();
packer.toBlob(doc).then(blob => {
console.log(blob);
saveAs(blob, "example.docx");
console.log("Document created successfully");
});
}
</script>
<div id="exportContent">
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
<span class="a" contenteditable="true" >
-----------------YOUR TEXT HERE--------------------
</span> took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</body>
</html>
【问题讨论】:
标签: javascript html css npm ms-word