简体   繁体   English

AWS Lambda创建PDF错误流

[英]AWS Lambda create PDF error stream

i am trying to create a pdf and storing it from an HTML template stored in S3 using html-pdf module. 我正在尝试创建一个pdf,并使用html-pdf模块从S3中存储的HTML模板中存储它。 I get this exception when try to lunch pdF.create 当尝试午餐pdF.create时出现此异常

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

Here below there is my pdf.create function 下面是我的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");
                }
            });
        });
    }
});

Could please help me understand how to solve it? 能帮我了解如何解决吗?

Many thanks 非常感谢

In order to get the binaries from phantomjs running on AWS Lambda, I had to copy them to the /tmp folder and update the PATH for Lambda and phantomPath for html-pdf : 为了从在AWS Lambda上运行的phantomjs获取二进制文件,我不得不将它们复制到/tmp文件夹,并更新Lambda的PATHhtml-pdf phantomPath

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')});
                });
            });
        }
    }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM