Skip to content

Commit c8ea94b

Browse files
committed
Merge pull request #267 from woolfg/dataurl
support of dataurl images
2 parents fcb97f4 + 029b96a commit c8ea94b

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

jspdf.plugin.addimage.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@
174174

175175
if(element.nodeName === 'CANVAS') {
176176
var canvas = element;
177+
//if element is an image which uses data url defintion, just return the dataurl
178+
} else if (element.nodeName === 'IMG' && element.getAttribute('src') && element.getAttribute('src').indexOf('data:image/') === 0) {
179+
return element.getAttribute('src');
177180
} else {
178181
var canvas = document.createElement('canvas');
179182
canvas.width = element.clientWidth || element.width;

jspdf.plugin.from_html.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,22 +420,33 @@
420420
renderer.pdf.internal.events.publish('imagesLoaded');
421421
cb();
422422
}
423-
function loadImage(url) {
423+
function loadImage(url, width, height) {
424424
if (!url)
425425
return;
426426
var img = new Image();
427427
++x;
428428
img.crossOrigin = '';
429429
img.onerror = img.onload = function () {
430-
if (img.complete && img.width + img.height)
431-
images[url] = images[url] || img;
432-
if (!--x)
430+
if(img.complete) {
431+
//to support data urls in images, set width and height
432+
//as those values are not recognized automatically
433+
if (img.src.indexOf('data:image/') === 0) {
434+
img.width = width || 0;
435+
img.height = height || 0;
436+
}
437+
//if valid image add to known images array
438+
if (img.width + img.height) {
439+
images[url] = images[url] || img;
440+
}
441+
}
442+
if(!--x) {
433443
done();
444+
}
434445
};
435446
img.src = url;
436447
}
437448
while (l--)
438-
loadImage(imgs[l].getAttribute("src"));
449+
loadImage(imgs[l].getAttribute("src"),imgs[l].width,imgs[l].height);
439450
return x || done();
440451
};
441452
checkForFooter = function (elem, renderer, elementHandlers, callback) {

0 commit comments

Comments
 (0)