错误:写EPIPE

时间:2016-01-19 16:45:44

标签: javascript node.js epipe

我一直收到以下错误:

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

当我运行以下功能时:

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

所以我试着四处寻找解决方案,有人说:

  

确保您的计算机上安装了imagemagick和graphicsmagick。

所以我使用以下方法安装它:

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

然而没有任何运气。

以下是我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,

我真的希望有些人能够帮助我!

5 个答案:

答案 0 :(得分:3)

我今天遇到aws Lambda这个问题,并在搜索时降落在这里。我也找到并成功解决了这个问题,因此我认为我应该在这里回答这个问题。

首先,我们需要一些项目来对lambda进行快速测试,这是一个项目。

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

但是任何幻影代码都将抛出Error: write EPIPE接下来,naeemshaikh27在此链接上发布了所有必需的依赖项,我认为这些依赖项应在某处列出,但除了此之外,不要列出其他所有依赖项。还要清楚地说明配置

Aws Lambda PhontomJS dependencies for amazon linux 2

答案 1 :(得分:1)

在您的选项对象中添加phantomjs的路径

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

答案 2 :(得分:1)

您将在某些操作系统中收到此错误。要针对任何操作系统修复此问题,只需指定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
};

答案 3 :(得分:1)

执行this,如果您在docker alpine镜像中运行应用程序,因为phantomjs-prebuilt在alpine上不起作用

答案 4 :(得分:0)

如果您使用的是Linux,@ Vishvendra Singh的回答可能会正常工作。但是,如果您使用的是Windows,则需要执行另一步骤。即使用Amazon linux2在ec2实例上编译二进制文件(使用命令chmod 777 *或chmod 777 binary_file_name使它们可执行),仅将它们压缩在那里。然后部署它。这样就可以了。