最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error: write EPIPE - Stack Overflow

programmeradmin3浏览0评论

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.useranization.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.bodyanization.logo_file_name,
            company_name: req.bodyanization.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!

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!

Share Improve this question asked Jan 19, 2016 at 16:45 Marc RasmussenMarc Rasmussen 20.6k83 gold badges221 silver badges382 bronze badges 1
  • Does the PDF creation code run properly standalone? If not, you should start there. – robertklep Commented Jan 22, 2016 at 8:50
Add a comment  | 

6 Answers 6

Reset to default 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
};

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

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

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

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.

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.

发布评论

评论列表(0)

  1. 暂无评论