7

我编写了一个 NodeJS 类,它应该从 html 字符串中创建一个 PDF 文件。该类部署在 AWS Lambda 上。它基于 html-pdf ( https://www.npmjs.com/package/html-pdf )。

const {readFileSync} = require('fs');
const pdf = require('html-pdf');

process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'];

const options = { format: 'Letter' };

const createPDF = async (fileName, data) => {

    let text = readFileSync('./templates/' + fileName + '.html','utf8');

    data.forEach(element => {
        text = text.replace("{{{" + element[0] + "}}}",element[1]);
    });

    console.log(text);

    return new Promise(function(resolve, reject) {
        pdf.create(text, options).toBuffer(function (err, buffer) {
            if (!err)
                resolve(buffer);
            else
                console.log(buffer)
        });
    });
};

module.exports = {
    createPDF
};

但是一旦调用 createPDF 方法,就会出现以下错误:

ERROR   Error: write EPIPE
    at afterWriteDispatched (internal/stream_base_commons.js:154:25)
    at writeGeneric (internal/stream_base_commons.js:145:3)
    at Socket._writeGeneric (net.js:784:11)
    at Socket._write (net.js:796:8)
    at doWrite (_stream_writable.js:403:12)
    at writeOrBuffer (_stream_writable.js:387:5)
    at Socket.Writable.write (_stream_writable.js:318:11)
    at PDF.PdfExec [as exec] (/var/task/node_modules/html-pdf/lib/pdf.js:141:15)
    at PDF.PdfToBuffer [as toBuffer] (/var/task/node_modules/html-pdf/lib/pdf.js:44:8)
    at /var/task/src/helper/PDFHelper.js:19:35 {
  errno: 'EPIPE',
  code: 'EPIPE',
  syscall: 'write'
}

有谁知道如何解决它?

最好的问候朱利叶斯

4

0 回答 0