使用 Angular、docxtemplater、pizzip、file-saver 在预定义的 Word 文档中写入

问题描述 投票:0回答:1

我想使用 Angular 操作 .docx 文件。 我编写了一个程序并实现了 docxtemplater,但在 .docx 中它写的是 undefined。

这是file.docx的最终输出 我的名字未定义 我的地址未定义 我的电子邮件地址未定义

预计出炉 我的名字是开发者 我的地址是印度 我的电子邮件地址是 [电子邮件受保护]

模板:- docx

My name is {data.name}
my address is {data.address}
My email address is {data.email}

我的代码:

filesaver.js docxtemplater pizzip
1个回答
0
投票

为了使用点语法,{data.name},...,您需要启用角度解析器:

你可以这样做:

import Docxtemplater from "docxtemplater";
import PizZip from "pizzip";
import expressionParser from "docxtemplater/expressions.js";

expressionParser.filters.upper = function (input) {
    // Make sure that if your input is undefined, your
    // output will be undefined as well and will not
    // throw an error
    if (!input) return input;
    return input.toUpperCase();
};

const zip = new PizZip(content);

const doc = new Docxtemplater(zip, {
    paragraphLoop: true,
    linebreaks: true,
    parser: expressionParser
});
doc.render({
   first_name: "John",
   last_name: "Doe",
   phone: "0652455478",
   description: "New Website",
});

请参阅此处有关角度解析器的文档:

https://docxtemplater.com/docs/angular-parse/

© www.soinside.com 2019 - 2024. All rights reserved.