我加载了一个docx文件,在vue.js项目中对其进行了更改,现在我尝试将其转换为PFD文件并将其下载到用户的计算机。 我该怎么办?
(我由docxtemplater更改的新docx文件未保存在项目中,而是另存为变量,如代码中所示)。
<template>
<div>
<button @click="renderDoc">
Render docx template
</button>
</div>
</template>
<script>
import Docxtemplater from "docxtemplater";
import PizZip from "pizzip";
import PizZipUtils from "pizzip/utils/index.js";
import { saveAs } from "file-saver";
function loadFile(url, callback) {
PizZipUtils.getBinaryContent(url, callback);
}
export default {
methods: {
renderDoc() {
loadFile("../templates/template.docx", function(
error,
content
) {
var zip = new PizZip(content);
var doc = new Docxtemplater().loadZip(zip);
doc.setData({
first_name: "John",
last_name: "Doe",
phone: "0652455478",
});
try {
//change the doc file content
doc.render();
}
catch (error) {
throw error;
}
var out = doc.getZip().generate({
type: "blob",
mimeType:
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
}); //Output the document using Data-URI
saveAs(out, "output.docx");
});
}
},
};
</script>