Error: write EPIPE
Asked Answered
D

6

8

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!

Discord answered 19/1, 2016 at 16:45 Comment(1)
Does the PDF creation code run properly standalone? If not, you should start there.Cestoid
T
3

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
};
Tasset answered 22/1, 2016 at 8:43 Comment(1)
The phantomPath looks like /data/APPS/e-commerce/backend/server/helpers/../../node_modules/phantomjs-prebuilt/bin/phantomjs-prebuilt but it is saying that directory is not found. Do you have an idea why it's failing?Grimsley
A
4

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
};
Alabama answered 23/11, 2019 at 4:40 Comment(0)
R
4

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

Rev answered 21/5, 2020 at 23:31 Comment(0)
T
3

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
};
Tasset answered 22/1, 2016 at 8:43 Comment(1)
The phantomPath looks like /data/APPS/e-commerce/backend/server/helpers/../../node_modules/phantomjs-prebuilt/bin/phantomjs-prebuilt but it is saying that directory is not found. Do you have an idea why it's failing?Grimsley
C
2

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

Coontie answered 18/8, 2020 at 7:7 Comment(0)
C
1

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.

Cannell answered 9/2, 2023 at 8:28 Comment(1)
This has been also the solution for me (the childProcessOptions element). Thanks!Dowser
P
0

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.

Payne answered 18/6, 2020 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.