Error: write EPIPE

Error: write 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);
    });

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

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

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

$ 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,

我真的希望你们中的一些人能够帮助我!

在您的选项对象中添加 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
};

您会在某些 Os 中遇到此错误。要为任何 Os 修复此问题,只需指定 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
};

我今天遇到了 aws Lambda 的这个问题,在搜索的时候来到这里。我也发现并成功解决了这个问题,所以我认为我应该通过在这里回答这个问题来为社区做出贡献。

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

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

但是任何幻象代码都会抛出 Error: write EPIPE 接下来 link naeemshaikh27 已经发布了所有必需的依赖项,我认为这些依赖项应该列在某个地方,但除了这个之外没有。配置也解释得很清楚

Aws Lambda PhontomJS dependencies for amazon linux 2

@Vishvendra Singh 的回答在您使用 linux 的情况下可能工作正常。但是,如果您使用 windows,那么您需要执行一个额外的步骤。即使用 amazon linux2 在 ec2 实例上编译二进制文件(使用命令 chmod 777 * 或 chmod 777 binary_file_name 使它们可执行),仅将它们压缩到那里。然后部署它。然后就可以正常工作了。

this 如果你是 运行 在 docker alpine 图像中的应用程序,因为 phantomjs-prebuilt 在 alpine

上不起作用