From fb3c7df1a4e26e19464cb73ef8238709f8245f47 Mon Sep 17 00:00:00 2001 From: Wolfgang Gassler Date: Sun, 25 May 2014 11:13:33 +0200 Subject: [PATCH 1/2] support of dataurl images --- jspdf.plugin.addimage.js | 3 +++ jspdf.plugin.from_html.js | 23 +++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/jspdf.plugin.addimage.js b/jspdf.plugin.addimage.js index 2a1dcbfcd..780c61a89 100644 --- a/jspdf.plugin.addimage.js +++ b/jspdf.plugin.addimage.js @@ -174,6 +174,9 @@ if(element.nodeName === 'CANVAS') { var canvas = element; + //if already a supported data url image, just return the dataurl + } else if (element.getAttribute('src').indexOf('data:image/') === 0) { + return element.getAttribute('src'); } else { var canvas = document.createElement('canvas'); canvas.width = element.clientWidth || element.width; diff --git a/jspdf.plugin.from_html.js b/jspdf.plugin.from_html.js index d1cdd74d0..c0d01579f 100644 --- a/jspdf.plugin.from_html.js +++ b/jspdf.plugin.from_html.js @@ -420,22 +420,33 @@ renderer.pdf.internal.events.publish('imagesLoaded'); cb(); } - function loadImage(url) { + function loadImage(url, width, height) { if (!url) return; var img = new Image(); ++x; img.crossOrigin = ''; img.onerror = img.onload = function () { - if (img.complete && img.width + img.height) - images[url] = images[url] || img; - if (!--x) - done(); + if(img.complete) { + //to support data urls in images, set width and height + //as those values are not recognized automatically + if (img.src.indexOf('data:image/') === 0) { + img.width = width || 0; + img.height = height || 0; + } + //if valid image add to known images array + if (img.width + img.height) { + images[url] = images[url] || img; + } + if(!--x) { + done(); + } + } }; img.src = url; } while (l--) - loadImage(imgs[l].getAttribute("src")); + loadImage(imgs[l].getAttribute("src"),imgs[l].width,imgs[l].height); return x || done(); }; checkForFooter = function (elem, renderer, elementHandlers, callback) { From 029b96ab4704392fed975f45518018edeb0c46df Mon Sep 17 00:00:00 2001 From: Wolfgang Gassler Date: Sun, 25 May 2014 14:11:22 +0200 Subject: [PATCH 2/2] applied proposals of #267 by @diegocr --- jspdf.plugin.addimage.js | 4 ++-- jspdf.plugin.from_html.js | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/jspdf.plugin.addimage.js b/jspdf.plugin.addimage.js index 780c61a89..c2ae68b38 100644 --- a/jspdf.plugin.addimage.js +++ b/jspdf.plugin.addimage.js @@ -174,8 +174,8 @@ if(element.nodeName === 'CANVAS') { var canvas = element; - //if already a supported data url image, just return the dataurl - } else if (element.getAttribute('src').indexOf('data:image/') === 0) { + //if element is an image which uses data url defintion, just return the dataurl + } else if (element.nodeName === 'IMG' && element.getAttribute('src') && element.getAttribute('src').indexOf('data:image/') === 0) { return element.getAttribute('src'); } else { var canvas = document.createElement('canvas'); diff --git a/jspdf.plugin.from_html.js b/jspdf.plugin.from_html.js index c0d01579f..2dba2a0f9 100644 --- a/jspdf.plugin.from_html.js +++ b/jspdf.plugin.from_html.js @@ -438,9 +438,9 @@ if (img.width + img.height) { images[url] = images[url] || img; } - if(!--x) { - done(); - } + } + if(!--x) { + done(); } }; img.src = url;