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:
- Created 7 years ago
- Comments:7 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I already have an open pull request (#554) for images so I added a fix to this issue
It was not yet fixed when 0.8.0 was released. This bug is fixed in 0.8.2