Pdfkit: Can't add image downloaded from web using request and pipe

Created on 18 Mar 2015  路  4Comments  路  Source: foliojs/pdfkit

Hi

I want to download a .jpg file from Web and add it to pdf, bug got Error: stream.push() after EOF error.

Here is the code:

  var out = fs.createWriteStream(filepath);
  request('some-url.jpg').pipe(out);
  out.on('finish', function(){
    var stats = fs.statSync(filepath);
    console.log(stats);
    doc.addPage().image(filepath, 33, 34, {fit: [100, 100]});
  });

Here is the error:

{ dev: 206529591,
  mode: 33206,
  nlink: 1,
  uid: 0,
  gid: 0,
  rdev: 0,
  blksize: undefined,
  ino: 3377699720980692,
  size: 132953,
  blocks: undefined,
  atime: Wed Mar 18 2015 16:28:03 GMT+0800 (涓浗鏍囧噯鏃堕棿),
  mtime: Wed Mar 18 2015 17:14:18 GMT+0800 (涓浗鏍囧噯鏃堕棿),
  ctime: Wed Mar 18 2015 17:14:18 GMT+0800 (涓浗鏍囧噯鏃堕棿),
  birthtime: Wed Mar 18 2015 16:28:03 GMT+0800 (涓浗鏍囧噯鏃堕棿) }
data length: 132953
events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: stream.push() after EOF
    at readableAddChunk (_stream_readable.js:149:15)
    at PDFDocument.Readable.push (_stream_readable.js:126:10)
    at PDFDocument._write (E:\2yi\ss4cn\node_modules\pdfkit\js\document.js:153:12)
    at PDFReference.finalize (E:\2yi\ss4cn\node_modules\pdfkit\js\reference.js:73:21)
    at PDFReference.finalize (E:\2yi\ss4cn\node_modules\pdfkit\js\reference.js:10:61)
    at PDFReference.end (E:\2yi\ss4cn\node_modules\pdfkit\js\reference.js:66:21)
    at JPEG.embed (E:\2yi\ss4cn\node_modules\pdfkit\js\image\jpeg.js:68:16)
    at PDFDocument.module.exports.image (E:\2yi\ss4cn\node_modules\pdfkit\js\mixins\images.js:28:15)
    at WriteStream.<anonymous> (E:\2yi\ss4cn\index.js:111:19)
    at WriteStream.emit (events.js:129:20)

From the stats, I think the file is well downloaded.

Node.js v0.12.0 on Wdinwos 7, and the pdfkit 0.7.0.

Thanks

Most helpful comment

  1. You don't need to same(sic) your image to a temp file in order to embed it. If you download it to a buffer in memory, you can embed the image that way as well. Just pass the buffer to doc.image.

For anyone else that comes across this post like I did and didn't know how to download an image to a buffer in memory here's how I did it using axios:

axios.get(someUrlThatPointsToAnImage, {responseType: 'arraybuffer'}).then(response => {
        const pngBuffer = Buffer.from(response.data);
        doc.image(pngBuffer);
}

All 4 comments

  1. You don't need to same your image to a temp file in order to embed it. If you download it to a buffer in memory, you can embed the image that way as well. Just pass the buffer to doc.image.
  2. Did you call doc.end() before you added the image? That seems like probably what the problem is here.

Thank your @devongovett ,I have resolved the problem by calling doc.end() in a synchronous way after doc.image().

  1. You don't need to same(sic) your image to a temp file in order to embed it. If you download it to a buffer in memory, you can embed the image that way as well. Just pass the buffer to doc.image.

For anyone else that comes across this post like I did and didn't know how to download an image to a buffer in memory here's how I did it using axios:

axios.get(someUrlThatPointsToAnImage, {responseType: 'arraybuffer'}).then(response => {
        const pngBuffer = Buffer.from(response.data);
        doc.image(pngBuffer);
}

not working for me

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alFReD-NSH picture alFReD-NSH  路  4Comments

oknoorap picture oknoorap  路  4Comments

raesche picture raesche  路  4Comments

ehaubold picture ehaubold  路  5Comments

durango picture durango  路  4Comments