Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

support of dataurl images #267

Merged
merged 2 commits into from

2 participants

@woolfg

support of elements with dataurl src

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) {
@diegocr Collaborator
diegocr added a note

I think it should be good checking element.nodeName === 'IMG' before getAttribute()

@diegocr Collaborator
diegocr added a note

Also, getAttribute can return null, so using indexOf on it will throw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
jspdf.plugin.from_html.js
((12 lines not shown))
- 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) {
@diegocr Collaborator
diegocr added a note

The call to done() needs to be outside the img.complete block.

@woolfg
woolfg added a note

you are damn right, sorry, result of a too fast merge conflict resolution ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@diegocr
Collaborator

Thanks, i'm merging without testing (since i don't think it's needed)

@diegocr diegocr merged commit c8ea94b into from
@diegocr diegocr referenced this pull request from a commit
@diegocr diegocr Minor tweaks related to #267 fac3703
@diegocr
Collaborator

I just did a few minor changes on fac3703

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 25, 2014
  1. @woolfg

    support of dataurl images

    woolfg authored
  2. @woolfg

    applied proposals of #267 by @diegocr

    woolfg authored
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 5 deletions.
  1. +3 −0  jspdf.plugin.addimage.js
  2. +16 −5 jspdf.plugin.from_html.js
View
3  jspdf.plugin.addimage.js
@@ -174,6 +174,9 @@
if(element.nodeName === 'CANVAS') {
var canvas = element;
+ //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');
canvas.width = element.clientWidth || element.width;
View
21 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)
+ 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) {
Something went wrong with that request. Please try again.