0

我正在尝试创建一个 pdf 并使用 html-pdf 模块从存储在 S3 中的 HTML 模板存储它。尝试午餐 pdf.create 时出现此异常

Error: write EPIPE
at exports._errnoException (util.js:1018:11)
at WriteWrap.afterWrite (net.js:800:14)

下面是我的 pdf.create 函数

pdf.create(templateHtml, options).toStream(function (err, stream) {
    console.log("stream :" + stream);
    if (err) {
        console.log('pdf err : ', err);
    } else {
        var stream = stream.pipe(fs.createWriteStream(filename));
        stream.on('finish', function () {
            let params_out = {
                Bucket : "partecipants-report",
                Key : "template_upload.html",
                Body : fs.createReadStream(filename),
                ContentType : "application/pdf"
            };
            s3.putObject(params_out, function(err, data) {
                if (err) console.log(err, err.stack); // an error occurred
                else{
                    console.log("upload ended :" + data);
                    context.succeed("upload ended");
                }
            });
        });
    }
});

请帮我理解如何解决它?

非常感谢

4

1 回答 1

1

为了从在 AWS Lambda 上运行的 phantomjs 获取二进制文件,我必须将它们复制到/tmp文件夹并更新PATHfor Lambda 和phantomPathfor html-pdf

process.env['PATH'] = process.env['PATH'] + ':/tmp/:' + process.env['LAMBDA_TASK_ROOT'];
var options = { phantomPath: '/tmp/phantomjs' };

require('child_process').exec(
    'cp /var/task/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs /tmp/.; chmod 755 /tmp/phantomjs;',
    function (error, stdout, stderr) {
        if (error) {
            //handle error
            console.log(error);
        } else {
            pdf.create(html, options).toBuffer(function (err, buffer) {
                if (err) return console.log(err);

                const s3PutParams = {
                    Bucket: 'my-bucket',
                    Key: Date.now().toString() + '.pdf',
                    Body: buffer,
                    ContentType: 'application/pdf',
                };

                s3.putObject(s3PutParams, function (error, data) {
                    if (error) {
                        console.error('s3:putObject failed!');
                        callback(error);
                        return;
                    }

                    callback(null, {statusCode: 200, body: JSON.stringify('Success')});
                });
            });
        }
    }
);
于 2018-12-29T21:50:39.580 回答