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 PIPE in AWS using buffer #576

Open MatheusLuiz2000 opened 4 years ago

MatheusLuiz2000 commented 4 years ago

Hey guys,

I having this error when i get the buffer of content of pdf and save in S3.

The error:

errors: {error: {message: "write EPIPE", name: "Error",…}}
error: {message: "write EPIPE", name: "Error",…}
frames: [{file: "internal/stream_base_commons.js", filePath: "internal/stream_base_commons.js",…},…]
0: {file: "internal/stream_base_commons.js", filePath: "internal/stream_base_commons.js",…}
1: {file: "internal/stream_base_commons.js", filePath: "internal/stream_base_commons.js",…}
2: {file: "net.js", filePath: "net.js", method: "Socket._writeGeneric", line: 776, column: 11,…}
3: {file: "net.js", filePath: "net.js", method: "Socket._write", line: 788, column: 8, context: {},…}
4: {file: "_stream_writable.js", filePath: "_stream_writable.js", method: "doWrite", line: 435,…}
5: {file: "_stream_writable.js", filePath: "_stream_writable.js", method: "writeOrBuffer", line: 419,…}
6: {file: "_stream_writable.js", filePath: "_stream_writable.js", method: "Socket.Writable.write",…}
7: {file: "node_modules/html-pdf/lib/pdf.js",…}
8: {file: "node_modules/html-pdf/lib/pdf.js",…}
9: {file: "src/app/services/ClienteService.ts",…}
10: {file: "src/app/services/ClienteService.ts",…}
11: {file: "internal/process/task_queues.js", filePath: "internal/process/task_queues.js",…}
12: {file: "src/app/controllers/ClienteController.ts",…}
message: "write EPIPE"
name: "Error"

How i doing the generate PDF

let html = fs.readFileSync('contrato.html', 'utf8');

return new Promise((resolve, reject) => {
  pdf.create(html).toBuffer(async function(err, buffer) {
    const saveS3 = await salvaS3(buffer, dados.documento);

    if (saveS3.status !== 200) {
      resolve({
        status: 400,
        data: 'Erro'
      });
    }

    resolve({
      status: 200
    });
  });
});
ghost commented 4 years ago

You could use toStream() instead.

Example:

pdf.create(html).toStream((error, stream) => {
    stream.pipe(() => {
        let pass = new stream.PassThrough();
        s3.upload({
            Bucket: "",
            Key: "",
            ContentType: "application/pdf",
            Body: pass
        }, (error, data) => {
            // Finished uploading or error
        });
        return pass;
    });
});
JuanM04 commented 4 years ago

Have you tried adding phantomPath?

pdf.create(html, {
  phantomPath: path.resolve(
    process.cwd(),
    "node_modules/phantomjs-prebuilt/bin/phantomjs"
  ),
})
Jiale-Li commented 3 years ago

您是否尝试过添加 ?phantomPath

pdf.create(html, {
  phantomPath: path.resolve(
    process.cwd(),
    "node_modules/phantomjs-prebuilt/bin/phantomjs"
  ),
})

ReferenceError: path is not defined, What's the path here?

ghost commented 3 years ago

@Jiale-Li path is a node module. You will need to require it like so:

const path = require('path')

In your code what the resolve method does is basically combines your current working directory and the path for phantomjs into an absolute path.

Jiale-Li commented 3 years ago

@Jiale-Li path is a node module. You will need to require it like so:

const path = require('path')

In your code what the resolve method does is basically combines your current working directory and the path for phantomjs into an absolute path.

Ah, thank you. I see, but it didn't work. I tried to convert HTML directly to PDF, Error reporting looks like a problem with the pipeline, I should learn something about it.

Error: write EPIPE at afterWriteDispatched (internal/stream_base_commons.js:154:25) at writeGeneric (internal/stream_base_commons.js:145:3) at Socket._writeGeneric (net.js:784:11) at Socket._write (net.js:796:8) at doWrite (_stream_writable.js:442:12) at writeOrBuffer (_stream_writable.js:426:5) at Socket.Writable.write (_stream_writable.js:317:11) at PDF.PdfExec [as exec] (D:\Workspace\dpdf\node_modules\html-pdf\lib\pdf.js:154:15) at PDF.PdfToFile [as toFile] (D:\Workspace\dpdf\node_modules\html-pdf\lib\pdf.js:83:8) at D:\Workspace\dpdf\app.js:53:31

ghost commented 3 years ago

So I believe that write EPIPE errors usually mean that the writing failed because the receiving end closed the connection.

Which bit of your code is on line 53 in app.js?

Jiale-Li commented 3 years ago

So I believe that write EPIPE errors usually mean that the writing failed because the receiving end closed the connection.

Which bit of your code is on line 53 in app.js?

I found that I did not install phantomjs prebuild and am trying to install it now

pdf.create(html, options).toFile('./tmp/test.pdf', (err, data) => {
        if (err) {
            res.send({
                code: 1,
                msg: 'err!'
            })
        } else {
            res.sendFile(data)
        }
});
Jiale-Li commented 3 years ago

So I believe that write EPIPE errors usually mean that the writing failed because the receiving end closed the connection.

Which bit of your code is on line 53 in app.js?

Thank you. I've solved it. Your suggestion is valid only if phantomjs prebuild is installed; After the correct phantompath is configured, everything is normal; Although a "referenceerror: can't find variable: $" error occurred later, I removed the script through regular expressions and everything went back to normal.

ghost commented 3 years ago

Glad to hear you got it working! :)

fg0611 commented 1 year ago

So I believe that write EPIPE errors usually mean that the writing failed because the receiving end closed the connection. Which bit of your code is on line 53 in app.js?

Thank you. I've solved it. Your suggestion is valid only if phantomjs prebuild is installed; After the correct phantompath is configured, everything is normal; Although a "referenceerror: can't find variable: $" error occurred later, I removed the script through regular expressions and everything went back to normal.

Hi, I think I'm having a very similar issue. I'm not an experienced dev, could you please share the steps you did to fix it¿?

This is my error, i'm using pdf-creator- node which uses phantom.js

events.js:292 throw er; // Unhandled 'error' event ^

Error: write EPIPE at afterWriteDispatched (internal/stream_base_commons.js:156:25) at writeGeneric (internal/stream_base_commons.js:147:3) at Socket._writeGeneric (net.js:785:11) at Socket._write (net.js:797:8) at writeOrBuffer (internal/streams/writable.js:358:12) at Socket.Writable.write (internal/streams/writable.js:303:10) at PDF.PdfExec [as exec] (/app/node_modules/html-pdf/lib/pdf.js:156:15) at PDF.PdfToFile [as toFile] (/app/node_modules/html-pdf/lib/pdf.js:85:8) at /app/node_modules/pdf-creator-node/index.js:69:20 at new Promise () Emitted 'error' event on Socket instance at: at emitErrorNT (internal/streams/destroy.js:106:8) at emitErrorCloseNT (internal/streams/destroy.js:74:3) at processTicksAndRejections (internal/process/task_queues.js:80:21) { errno: -32, code: 'EPIPE', syscall: 'write' }