7

So I am using html-pdf to convert my html and here is my code:

var pdf = require('html-pdf')
var html = 'somehtmlfile.html'

pdf.create(html).toBuffer(function (err, buffer) {
        if (err) {
          console.log(err)
        } else {
          console.log(buffer)
          var pdfBuffer = new Buffer(buffer)
          res.setHeader('Content-disposition', 'inline; filename="test.pdf"');
          res.setHeader('Content-type', 'application/pdf');
          res.send(pdfBuffer)
        }
}

I am not getting any PDF file to be downloader nor any output of a pdf file in the browser. The console.log(buffer) is this:

<Buffer 25 50 44 46 2d 31 2e 34 0a 31 20 30 20 6f 62 6a 0a 3c 3c 0a 2f 54 69 74 6c 65 20 28 fe ff 29 0a 2f 43 72 65 61 74 6f 72 20 28 fe ff 29 0a 2f 50 72 6f ... >

Is there a simple way of doing this? Or am I doing it wrong?

I just want to output the buffer in a pdf form in the browser.

wobsoriano
  • 12,348
  • 24
  • 92
  • 162

5 Answers5

6

Change to:

pdf.create(html).toStream(function(err, stream) {
    if (err) {
        console.log(err)
    } else {
        res.set('Content-type', 'application/pdf');
        stream.pipe(res)
    }
});
Diego ZoracKy
  • 2,227
  • 15
  • 14
  • Success, but nothing happens – wobsoriano Mar 29 '17 at 16:07
  • Nothing happens where? Which http framework are you using that gives you `res`? – Diego ZoracKy Mar 29 '17 at 16:09
  • I am using express – wobsoriano Mar 29 '17 at 16:10
  • Tell me what you mean by "nothing happens". What is expected and what happened? And a note, if you want to send to the client a file to be downloaded, not to be shown as a content, got with `res.download` (https://expressjs.com/en/api.html#res.download) – Diego ZoracKy Mar 29 '17 at 16:12
  • I am using express and I am not getting any errors. The code was successful but there's no pdf output in the browser – wobsoriano Mar 29 '17 at 16:13
  • Check again my answer. I changed `res.setHeader` to `res.set` and threw away `Content-disposition` as it is not needed if is not a case for download. – Diego ZoracKy Mar 29 '17 at 16:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139392/discussion-between-fewflyby-and-diego-zoracky). – wobsoriano Mar 29 '17 at 16:18
  • 1
    If you want to download the file in the browser try this: `res.contentType('application/pdf') res.attachment() stream.pipe(res)` – mkupiniak Aug 08 '20 at 09:55
1

you can use the below function or html.create(somehtmlfile.html).toStream()

function to convert a buffer to stream

 function bufferToStream(buffer) {  
     let stream = new Duplex();
     stream.push(buffer);
     stream.push(null);
     return stream;
   }

download or view a pdf in browser if it's a stream

var pdf = require('html-pdf')
var html = 'somehtmlfile.html'
exports.generatePdf = (req, res) =>{
    pdf.create(html).toBuffer(function (err, buffer) {
            if (err) {
              console.log(err)
            } else {
              console.log(buffer)

           bufferToStream(buffer).pipe(res)
            }
    }
}
1
pdf.create(html).toBuffer(function (err, buffer) {
    if (err) {
        console.log(err)
    } else {
        console.log(buffer)
        res.header('Content-type', 'application/pdf')
        res.send(buffer)
    }
}
  • Hi, Is there a way to make this process asynchronous? I am generating an S3 link inside the callback but it does not wait for link – Ehsan Nissar Nov 04 '21 at 06:48
1
function bufferToStream(buffer) {
          let stream = new Duplex();
          stream.push(buffer);
          stream.push(null);
          return stream;
        }

        pdf.create(doctoPdf).toBuffer(function (err, buffer) {
          if (err) {
            console.log(err)
          } else {
            console.log("buffer");
            console.log(buffer)
            res.contentType('application/pdf')
            res.attachment()
            return bufferToStream(buffer).pipe(res)

          }
        })

if you want to download it on the client-side then this will be help full.

0

Try this in client side:

var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";

oReq.onload = function (oEvent) {
    console.log(oReq.response);
    var blob = new Blob([oReq.response], {type: "application/pdf"});
    var win = window.open('', '_blank');
    var URL = window.URL || window.webkitURL;
    var dataUrl = URL.createObjectURL(blob);
    win.location = dataUrl;
};
oReq.send();

In server Side:

pdf.create(htmlContent).toStream(function(err, stream){
    console.log("in post method"+stream.length);
        res.header('Content-type', 'application/pdf');

        stream.pipe(res);
});
Zoe
  • 27,060
  • 21
  • 118
  • 148
Ram pravin
  • 21
  • 3