【问题标题】:Error: write EPIPE错误:写入 EPIPE
【发布时间】:2016-04-25 06:04:41
【问题描述】:

我不断收到以下错误:

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,

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

【问题讨论】:

  • PDF 创建代码独立运行是否正常?如果没有,你应该从那里开始。

标签: javascript node.js epipe


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 2015-08-17
            • 2014-12-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多