Marc Rasmussen
Marc Rasmussen

Reputation: 20545

Error: write EPIPE

I keep getting the following error:

Error: write EPIPE
at errnoException (net.js:901:11)
at Object.afterWrite (net.js:718:19)

When i run the following function:

    router.route('/certificationService')
    .post(function (req, res) {
        var html = null,
            certificate = req.body.certificate,
            lang = req.body.lang,
            now = new Date(),
            dd = now.getDate(),
            mm = now.getMonth() + 1,
            yyyy = now.getFullYear();

        if (dd < 10) dd = '0' + dd;
        if (mm < 10) mm = '0' + mm;

        switch (lang) {
            case 'da':
                var text = {
                    title: 'Certifikat',
                    first_line: 'Dette certifikat er givet til',
                    second_line: 'for gennemførelsen af certificeringen',
                    score: 'Score',
                    date: 'D.',
                    organization: 'Organisation'
                };
                break;
            case 'en':
                var text = {
                    title: 'Certificate',
                    first_line: 'This certificate was given to',
                    second_line: 'for the completion of certification',
                    score: 'Score',
                    date: 'The',
                    organization: 'Organization'
                };
                break;
            case 'no':
                var text = {
                    title: 'Sertifikat',
                    first_line: 'Dette sertifikatet er gitt til',
                    second_line: 'gjennomføring av sertifisering',
                    score: 'Score',
                    date: 'D.',
                    organization: 'Organisation'
                };
                break;
            case 'de':
                var text = {
                    title: 'Zertifikat',
                    first_line: 'Dieses zertifikat wird eine für gegebene',
                    second_line: 'die Umsetzung der zertifizierung',
                    score: 'Ergebnis',
                    date: 'D.',
                    organization: 'Unternehmen'
                };
                break;
            default:
        }

        var data = {
            firstname: certificate.user.profile.firstname,
            lastname: certificate.user.profile.lastname,
            organization: certificate.user.organization.name,
            module_name: certificate.name,
            medal: env + certificate.medal.image_path,
            score: certificate.score,
            date: dd + '-' + mm + '-' + yyyy,
            show_score: certificate.show_score,
            description: certificate.text,
            company_logo: env + req.body.organization.logo_file_name,
            company_name: req.body.organization.name,
            text: text
        };

        // rendering the ejs file
        ejs.renderFile('./templates/certificate.ejs', {data: data}, function (err, result) {
            if (result) {
                html = result;
            } else {
                res.end('An error occurred: ' + err);
                console.log(err);
            }
        });

        var options = {
            filename: './user_resources/certificate/' + certificate.user.id + '/' + certificate.name.replace(/ +?/g, '_') + '.pdf',
            format: 'A4',
            orientation: 'portrait',
            type: "pdf",
            timeout: 30000
        };

        pdf.create(html, options).toFile(function (err, res) {
            if (err) return console.log("This is where it goes wrong"+ err);
            console.log(res);
        });

        var file = {
            originalname: certificate.name.replace(/ +?/g, '_') + '.pdf',
            path: './user_resources/certificate/' + certificate.user.id + '/'
        };

        var media = new Media(file, './user_resources/certificate/' + certificate.user.id + '/');

        var token = jwt.encode({
            mediaObject: media
        }, require('../secret')());


        res.status(200).json(token);
    });

So i tried to look around to find a solution and someone said:

Make sure both imagemagick and graphicsmagick are installed on your machine.

So ive installed it using the following:

$ sudo add-apt-repository ppa:dhor/myway
$ sudo apt-get update
$ sudo apt-get install graphicsmagick

However without any luck.

The following are the dependencies of my module:

    var fs = require('fs'),
    jwt = require('jwt-simple'),
    pdf = require('html-pdf'),
    path = require('path'),
    ejs = require('ejs'),
    async = require('async'),
    DataTypes = require("sequelize"),
    PDFKit = require('pdfkitjs'),
    gm = require('gm').subClass({imageMagick: true}),
    ffmpeg = require('fluent-ffmpeg'),
    sys = require('util'),
    exec = require('child_process').exec,

I really hope some of you are able to help me out!

Upvotes: 8

Views: 48459

Answers (6)

Haithem SMIRI
Haithem SMIRI

Reputation: 44

in case nothing of the previous solution works try to update your 'options'

var options = {
 childProcessOptions: {
        env: {
            OPENSSL_CONF: "/dev/null",
        },
    },
filename: './user_resources/certificate/' + certificate.user.id + '/' + certificate.name.replace(/ +?/g, '_') + '.pdf',
format: 'A4',
orientation: 'portrait',
type: "pdf",
timeout: 30000};

this work for me.

Upvotes: 1

eitann
eitann

Reputation: 1260

Do this In case you are running the app inside docker alpine image, because phantomjs-prebuilt doesn't work on alpine

Upvotes: 2

jinal kapatel
jinal kapatel

Reputation: 1

Answer from @Vishvendra Singh might work fine in case you are using linux. But if you are using windows then you need to perform one additional step. i.e. Compile binaries on ec2 instance with amazon linux2 ( make them executable using command chmod 777 * or chmod 777 binary_file_name), zip them there only. And then deploy it. Then it will work fine.

Upvotes: 0

Vishvendra Singh
Vishvendra Singh

Reputation: 494

I faced this problem with aws Lambda today and landed here while searching. Also I found and successfully solved this problem so I think I should contribute to community by answering this here.

First we need some project to test fast on lambda here is one.

An easy to deploy implementation of html-pdf for AWS Lambda

But any phantom code throws Error: write EPIPE Next on this link naeemshaikh27 has posted all the required dependencies which I think should be listed somewhere but aren't except this one. Also configuration is clearly explained

Aws Lambda PhontomJS dependencies for amazon linux 2

Upvotes: 4

Anatole ABE
Anatole ABE

Reputation: 585

You will get this error in some Os. To fix this for any Os, just specify the phantomjs:

var phantomjs = require('phantomjs');
var options = {
    phantomPath: phantomjs.path,
    filename: './user_resources/certificate/' + certificate.user.id + '/' + certificate.name.replace(/ +?/g, '_') + '.pdf',
    format: 'A4',
    orientation: 'portrait',
    type: "pdf",
    timeout: 30000
};

Upvotes: 4

Jacob
Jacob

Reputation: 1004

add path to phantomjs in you options object

var options = {
    phantomPath: __dirname + "/pathToNodeModules/phantomjs/bin/phantomjs",
    filename: './user_resources/certificate/' + certificate.user.id + '/' + certificate.name.replace(/ +?/g, '_') + '.pdf',
    format: 'A4',
    orientation: 'portrait',
    type: "pdf",
    timeout: 30000
};

Upvotes: 3

Related Questions