0

i am trying to create a pdf and storing it from an HTML template stored in S3 using html-pdf module. I get this exception when try to lunch 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(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

Alexis N-o
  • 3,954
  • 26
  • 34
Paride Letizia
  • 330
  • 1
  • 4
  • 23
  • You're overwriting your `stream` response from `pdf.create` with the `var stream = stream.pipe(fs.createWriteStream(filename));`. Try changing that variable name to something else. – dzm Nov 06 '17 at 22:17
  • Now I get this error: Error: html-pdf: Received the exit code '126' /var/task/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs: /var/task/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs: cannot execute binary file I really do not what happens – Paride Letizia Nov 07 '17 at 09:06

1 Answers1

1

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:

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')});
                });
            });
        }
    }
);
pgeimer
  • 103
  • 1
  • 6