marcbachmann / node-html-pdf

This repo isn't maintained anymore as phantomjs got dreprecated a long time ago. Please migrate to headless chrome/puppeteer.
MIT License
3.56k stars 545 forks source link

Error: write EPIPE #35

Open grimurd opened 9 years ago

grimurd commented 9 years ago

I'm using nunjucks to render a template into a string, i then try to convert that html to a pdf file however everytime i get this error:

Error: write EPIPE
    at errnoException (net.js:905:11)
    at Object.afterWrite (net.js:721:19)

Here is the code i use to create the pdf file:

                        nunjucks.render(path.join(__dirname, '../views/mail/receipt.html'), {
                                __: res.__,
                                query: req.query,
                                payment: payment
                            }, function(err, html) {
                                if (err) {
                                    next(err);
                                }
                                htmlPdf.create(html, {
                                    type: 'pdf'
                                }).toBuffer(function(erro, fileBuffer) {
                                    // Code never gets this far, errors out before it happens and the error object is still null.
                                    if (erro) {
                                        next(erro);
                                    }

                                    var transporter = nodemailer.createTransport({
                                        host: config.email.host,
                                        port: config.email.port,
                                        secure: config.email.secure
                                    });

                                    transporter.sendMail({
                                        from: config.email.from,
                                        to: payment.receipt_email,
                                        subject: res.__({
                                            phrase: 'Receipt_Email_Subject',
                                            locale: payment.lang
                                        }),
                                        attachments: [{
                                            filename: 'receipt.pdf',
                                            content: fileBuffer,
                                            contentType: 'application/pdf'
                                        }],
                                        text: 'test'
                                    }, function(error, info) {
                                        if (error) {
                                            next(error);
                                        }

                                        res.json(info);
                                    });
                                });
                            });

Here is the HTML string that is generated by nunjucks, not the most readable i know but figured i should post it like it is instead of trying to make it prettier

<!DOCTYPE html>\n<html lang="en">\n    <head>\n        <meta charset="utf-8">\n        <meta content="IE=edge" http-equiv="X-UA-Compatible">\n        <meta content="width=device-width, initial-scale=1" name="viewport">\n        <title>Snerpa WiFi Receipt</title>\n        <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"\n            rel="stylesheet">\n    </head>\n    <body>\n        <div class="container">\n            <div class="row">\n                <div class="col-xs-6 col-sm-5 col-md-4 col-lg-3">\n                    <img src="http://wifi.snerpa.is/assets/images/wifi_logo.png" class="img-responsive" alt="WiFi Logo">\n                </div>\n            </div>\n            <div class="row">\n                <div class="col-xs-12">\n                    <div class="invoice-title">\n                        <h2>Invoice</h2>\n                        <h3 class="pull-right">Pöntun # 0575741</h3>\n                    </div>\n                    <hr>\n                        <div class="row">\n                            <div class="col-xs-6">\n                                <address>\n                                    <strong>Greiðslumáti:</strong><br>\n                                    VISA 999999******9999<br>\n                                    grimurd@snerpa.is\n                                </address>\n                            </div>\n                            <div class="col-xs-6 text-right">\n                                <address>\n                                    <strong>Dagsetning:</strong><br>\n                                    03.06.2015<br><br>\n                                </address>\n                            </div>\n                        </div>\n                    </div>\n                </div>\n                <div class="row">\n                    <div class="col-md-12">\n                        <div class="panel panel-default">\n                            <div class="panel-heading">\n                                <h3 class="panel-title"><strong>Samantekt</strong></h3>\n                            </div>\n                            <div class="panel-body">\n                                <div class="table-responsive">\n                                    <table class="table table-condensed">\n                                        <thead>\n                                            <tr>\n                                                <td><strong>Vara</strong></td>\n                                                <td class="text-center"><strong>Verð</strong></td>\n                                                <td class="text-center"><strong>Magn</strong></td>\n                                                <td class="text-right"><strong>Samtals</strong></td>\n                                            </tr>\n                                        </thead>\n                                        <tbody>\n                                            \n                                            <tr>\n                                                <td>Snerpa WiFi 1 Klst Aðgangur</td>\n                                                <td class="text-center">€1</td>\n                                                <td class="text-center">1</td>\n                                                <td class="text-right">€1</td>\n                                            </tr>\n                                            \n                                        </tbody>\n                                    </table>\n                                </div>\n                            </div>\n                        </div>\n                    </div>\n                </div>\n                <style>\n                    .invoice-title h2,\n                    .invoice-title h3 {\n                        display: inline-block;\n                    }\n\n                    .table> tbody> tr> .no-line {\n                        border-top: none;\n                    }\n\n                    .table> thead> tr> .no-line {\n                        border-bottom: none;\n                    }\n\n                    .table> tbody> tr> .thick-line {\n                        border-top: 2px solid;\n                    }\n                </style>\n            </body>\n        </html>\n

I've been stuck at this for hours and can't see that im doing anything wrong, any ideas?

theart84 commented 2 years ago

Found a solution! If you use docker, avoid using the alphine versions. I switched from node:14-alphine to node:14 And it was all fine

Unfortunately it didn't help me. I found a solution here

ghost commented 2 years ago

I used Docker to replicate the error that was not possible to replicate on localhost, and I could fix it by adding the following command on my Dockerfile:

RUN apk add --no-cache --virtual .build-deps ca-certificates openssl \
  && wget -qO- "https://github.com/dustinblackman/phantomized/releases/download/2.1.1/dockerized-phantomjs.tar.gz" | tar xz -C / \
  && apk del .build-deps

I got it from here.

Jiteshchoudhary commented 2 years ago

i am facing same issue but still not able to get the answer Error: write EPIPE 0|hb Api | at afterWrite Dispatched (internal/stream_base_commons.js:156:25) 0|hb Api | at writeGeneric (internal/stream_base_commons.js:147:3) 0|hb Api | at Socket._writeGeneric (net.js:787:11) 0|hb Api | at Socket._write (net.js:799:8) 0|hb Api | at writeOrBuffer (internal/streams/writable.js:358:12) 0|hb Api | at Socket.Writable.write (internal/streams/writable.js:303:10) 0|hb Api | at PDF.PdfExec [as exec] (/var/www/html/hbv2Api/node_modules/html-pdf/lib/pdf.js:141:15) 0|hb Api | at PDF.PdfToFile [as toFile] (/var/www/html/hbv2Api/node_modules/html-pdf/lib/pdf.js:83:8) 0|hb Api | at /var/www/html/hbv2Api/app/helper/pdfHelper.js:6:39 0|hb Api | at new Promise () 0|hb Api | at generatePdf (/var/www/html/hbv2Api/app/helper/pdfHelper.js:5:16) 0|hb Api | at exports.getInvoice (/var/www/html/hbv2Api/app/modules/controllers/orders.controller.js:1465:13) 0|hb Api | at processTicksAndRejections (internal/process/task_queues.js:95:5) { 0|hb Api | errno: -32, 0|hb Api | code: 'EPIPE', 0|hb Api | syscall: 'write' 0|hb Api | }