docxtemplater api在electronjs中的应用

docxtemplater api在electronjs中的应用,electron,buffer,docx,deprecated,docxtemplater,Electron,Buffer,Docx,Deprecated,Docxtemplater,我正在尝试使用docxtemplater在electronjs应用程序中,我使用了示例代码: var PizZip = require('pizzip'); var Docxtemplater = require('docxtemplater'); var fs = require('fs'); var path = require('path'); // The error object contains additional information when logged with JS

我正在尝试使用
docxtemplater
electronjs
应用程序中,我使用了示例代码:

var PizZip = require('pizzip');
var Docxtemplater = require('docxtemplater');

var fs = require('fs');
var path = require('path');

// The error object contains additional information when logged with JSON.stringify (it contains a properties object containing all suberrors).
function replaceErrors(key, value) {
    if (value instanceof Error) {
        return Object.getOwnPropertyNames(value).reduce(function(error, key) {
            error[key] = value[key];
            return error;
        }, {});
    }
    return value;
}

function errorHandler(error) {
    console.log(JSON.stringify({error: error}, replaceErrors));

    if (error.properties && error.properties.errors instanceof Array) {
        const errorMessages = error.properties.errors.map(function (error) {
            return error.properties.explanation;
        }).join("\n");
        console.log('errorMessages', errorMessages);
        // errorMessages is a humanly readable message looking like this :
        // 'The tag beginning with "foobar" is unopened'
    }
    throw error;
}

//Load the docx file as a binary
var content = fs
    .readFileSync(path.resolve(__dirname, 'input.docx'), 'binary');

var zip = new PizZip(content);
var doc;
try {
    doc = new Docxtemplater(zip);
} catch(error) {
    // Catch compilation errors (errors caused by the compilation of the template : misplaced tags)
    errorHandler(error);
}

//set the templateVariables
doc.setData({
    first_name: 'John',
    last_name: 'Doe',
    phone: '0652455478',
    description: 'New Website'
});

try {
    // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
    doc.render()
}
catch (error) {
    // Catch rendering errors (errors relating to the rendering of the template : angularParser throws an error)
    errorHandler(error);
}

var buf = doc.getZip()
             .generate({type: 'nodebuffer'});

// buf is a nodejs buffer, you can either write it to a file or do anything else with it.
fs.writeFileSync(path.resolve(__dirname, 'output.docx'), buf);
我面对着这个错误:

(node:8896) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

因此,我试图搜索
fs
docxtemplater
模块中的代码部分,以将
Buffer
更改为
Buffer.from()
,但我没有找到任何东西,有什么想法吗?,非常感谢您的帮助。

您好,您收到的错误消息是一条警告,它仍然可以与消息一起工作。此外,信息来自Pizzip本身,而不是docxtemplater。您的应用程序中是否有任何不工作的内容?