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.

In browser using XHR ArrayBuffer, embedding multiple images will use only the first image

See original GitHub issue

@mattblang already mentioned this in this comment on the now closed issue #371.

I’m using XHR with responseType of ‘arraybuffer’, and feeding the response to pdfkit’s image method:

var imageURLs = ['a.png', 'b.png', 'c.png'];
imageURLs.forEach(fetchImage);

var fetchedImages = {};
function fetchImage(url) {
  var request = new XMLHttpRequest();
  request.open("GET", url, true);
  request.responseType = "arraybuffer";

  request.onload = function() {
    fetchedImages[url] = request.response; // ArrayBuffer instance
    if(imageURLs.every(function(imageURL) {
      return fetchedImages[imageURL];
    }) {
      pdfDoc.image(fetchedImages['a.png'], 0, 0);
      pdfDoc.image(fetchedImages['b.png'], 0, 100);
      pdfDoc.image(fetchedImages['c.png'], 0, 200);
    }
  };
  request.send();
}

The first image works fine, but all other images will display the first image instead. The problem is Buffer.isBuffer returns false for ArrayBuffer objects, which causes this code to use an image from cache.

    unless Buffer.isBuffer(src)
      image = @_imageRegistry[src]

A ugly workaround for this is to make ArrayBuffer pretend to be Buffer (this works because Buffer.isBuffer(src) calls src.constructor.isBuffer(src)):

ArrayBuffer.isBuffer = function() {
  return true;
};

But the proper solution would probably be to check that src is either Buffer or ArrayBuffer.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
alafrcommented, Oct 17, 2016

I already have an open pull request (#554) for images so I added a fix to this issue

1reaction
alafrcommented, Jun 10, 2017

It was not yet fixed when 0.8.0 was released. This bug is fixed in 0.8.2

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting BLOB data from XHR request - Stack Overflow
Basically, what I am trying to do here is retrieve an image, and convert it to base64. From reading in the comments here,...
Read more >
An Introduction to Blob API & Its Use Cases
Use Cases: 1. Upload in chunks, 2. Download data from the Internet, 3. Image Compression, 4. Create Blob URL, 5. Blob to Data...
Read more >
15. XMLHttpRequest - High Performance Browser ... - O'Reilly
XMLHttpRequest (XHR) is a browser-level API that enables the client to script data transfers via JavaScript. XHR made its first debut in Internet...
Read more >
New tricks in XMLHttpRequest2 - web.dev
This tutorial highlights some of the new features in XMLHttpRequest , especially those that can be used for working with files.
Read more >
Link types: preload - HTML: HyperText Markup Language | MDN
Here however, we will use a rel value of preload , which turns ... Resources that are pointed to from inside CSS, like...
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