Skip to content

Commit 6683eb3

Browse files
author
Trinh
committed
Add support for other image formats.
Relying on canvas for conversion of other image formats into jpeg dataurl.
1 parent 49db0fb commit 6683eb3

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

jspdf.plugin.addimage.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,19 @@ var getJpegSize = function(imgData) {
133133

134134
jsPDFAPI.addImage = function(imageData, format, x, y, w, h) {
135135
'use strict'
136-
136+
if (typeof imageData === 'object' && imageData.nodeType === 1) {
137+
var canvas = document.createElement('canvas');
138+
canvas.width = imageData.clientWidth;
139+
canvas.height = imageData.clientHeight;
140+
141+
var ctx = canvas.getContext('2d');
142+
if (!ctx) {
143+
throw ('addImage requires canvas to be supported by browser.');
144+
}
145+
ctx.drawImage(imageData, 0, 0, canvas.width, canvas.height);
146+
imageData = canvas.toDataURL('image/jpeg');
147+
format = "JPEG";
148+
}
137149
if (format.toUpperCase() !== 'JPEG') {
138150
throw new Error('addImage currently only supports format \'JPEG\', not \''+format+'\'');
139151
}
@@ -208,5 +220,4 @@ jsPDFAPI.addImage = function(imageData, format, x, y, w, h) {
208220

209221
return this
210222
}
211-
212223
})(jsPDF.API)

0 commit comments

Comments
 (0)