question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
ironicbrewcommented, Apr 6, 2019
  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);
}
4reactions
wankhedeyakash9commented, Dec 4, 2020

not working for me

Read more comments on GitHub >

github_iconTop Results From Across the Web

Downloading images with node.js [closed] - Stack Overflow
I'd suggest using the request module. Downloading a file is as simple as the following code: var fs = require('fs'), request = require('request');...
Read more >
download an image with get request from web URL in python
In this tutorial u will learn1. how to download an image from url with get requests using python.2. downloading with get requests in ......
Read more >
How To Web Scrape & Download Images With Python
Thanks to Smartroxy for sponsoring this video! Smartproxy provides the best proxies for data scraping and countless other use cases.
Read more >
How to download images with NodeJS? - ScrapingAnt
This tutorial will show you how to download images and files with NodeJS. There are libraries that can help you achieve that, like...
Read more >
Axios — Download Files & Images in Node.js - Future Studio
It has a convenient and modern API simplifying asynchronous HTTP request and response handling. Let's explore how to download files with ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found