NodeJS 错误:写入EPIPE

vyu0f0g1  于 2023-02-12  发布在  Node.js
关注(0)|答案(6)|浏览(434)

我一直收到以下错误:

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,

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

nx7onnlm

nx7onnlm1#

你会在一些操作系统中得到这个错误。要修复任何操作系统的这个错误,只需指定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
};
t3irkdon

t3irkdon2#

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

bn31dyow3#

我今天面对了这个问题,并在搜索时登陆了这里。我也发现并成功地解决了这个问题,所以我认为我应该在这里回答这个问题,为社区做出贡献。
首先,我们需要一些项目来快速测试lambda,这里有一个。
An easy to deploy implementation of html-pdf for AWS Lambda
但是任何幻像代码都会抛出Error: write EPIPE Next on this link naeemshaikh27已经发布了所有需要的依赖项,我认为应该在某个地方列出,但除了这一个。
Aws Lambda PhontomJS dependencies for amazon linux 2

0s0u357o

0s0u357o4#

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

fbcarpbf

fbcarpbf5#

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

qcuzuvrc

qcuzuvrc6#

如果前面的解决方案不起作用,请尝试更新您的“选项”

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

这对我有用。

相关问题