1

我正在使用带有 nodejs 的 docxtemplater,并已从以下链接中阅读了文档: https ://docxtemplater.readthedocs.io/en/latest/generate.html#node

与提供的文档不同,我正在尝试从我的 firebase 存储中加载一个名为

'标签-example.docx'

并让 docxtemplater 在那里的标签上运行。然后将生成的文档保存回我的 firebase 存储。简单的说:

  1. 从 Firebase 存储中加载“tag-example.docx”;
  2. docxtemplater 在文档上做它的事情;
  3. 修改后的输出保存到 firebase 存储。

我的问题是我不断收到以下错误消息:

Unhandled error TypeError: Cannot read property 'toLowerCase' of undefined
at Object.exports.checkSupport (/workspace/node_modules/pizzip/js/utils.js:293:32)
at ZipEntries.prepareReader (/workspace/node_modules/pizzip/js/zipEntries.js:275:11)
at ZipEntries.load (/workspace/node_modules/pizzip/js/zipEntries.js:295:10)
at new ZipEntries (/workspace/node_modules/pizzip/js/zipEntries.js:32:10)
at PizZip.module.exports [as load] (/workspace/node_modules/pizzip/js/load.js:25:20)
at new PizZip (/workspace/node_modules/pizzip/js/index.js:41:10)
at /workspace/index.js:66:11
at func (/workspace/node_modules/firebase-functions/lib/providers/https.js:336:32)
at processTicksAndRejections (internal/process/task_queues.js:95:5)

有没有办法解决这个问题?这是因为我没有像示例中那样将文档作为二进制文件加载吗?这甚至可以通过 Firebase 存储来完成吗?

    const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {Storage} = require('@google-cloud/storage');
var PizZip = require('pizzip');
var Docxtemplater = require('docxtemplater');
admin.initializeApp();
const BUCKET = 'gs://myapp.appspot.com';

    const https = require('https');
    
    const storage = new Storage({
    projectId: 'myapp' });
    const cors = require('cors')({origin: true});


exports.test2 = functions.https.onCall((data, context) => {
// 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
let file_name = 'tag-example.docx';
const myFile =storage.bucket(BUCKET).file(file_name);
var content =  myFile.createReadStream();

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 and then save to firebase storage.
buf.pipe(myFile.createWriteStream());

});
4

1 回答 1

0

此错误消息来自 pizzip,而不是直接来自 docxtemplater。

当提供给 pizzip 的参数无效时,就会发生这种情况。

在你的情况下,你做了:

var content =  myFile.createReadStream();
var zip = new PizZip(content);

问题是我认为内容是一个特定的对象,一个流,而不是已经读完的东西。

您需要首先将内容解析为字符串或缓冲区,然后您可以执行以下操作:

var zip = new PizZip(buffer);
于 2021-06-23T21:20:12.193 回答