javascript - 将 docx 文件转换为 PDF (vue.js)
问题描述
我在我的 vue.js 项目中加载了一个 docx 文件并对其进行了更改,现在我尝试将其转换为 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>
解决方案
推荐阅读
- prestashop - 如何从模块中调用 FrontController
- perl - 使用 open 函数将 1 添加到文本文件中的列
- selenium-webdriver - webdriver-firefox 配置文件目录并选择配置文件
- nginx - 无法通过 nginx 传递未知标头
- visual-studio-code - 带有 VS Code 中最喜欢的操作的快速列表,例如 JetBrains IDE
- wordpress - 将 x-model Apline.js 属性添加到 Contact Form 7 字段
- excel - 测试后 VBA + Selenium 保持打开 Edge 浏览器
- java - @RequestBody 中的 Spring Enum 列表为空
- c# - 服务外部文件夹
- node.js - after nodejs update i get this error: Cast to ObjectId failed for value "600204c674086624b9de76e2" at path "classId" for model "Message"