Skip to content

Commit a0db64a

Browse files
committed
Added example file
Added example file added transparent parameter for greater control of canvas output when adding a DOM element
1 parent 9078189 commit a0db64a

File tree

6 files changed

+146
-7
lines changed

6 files changed

+146
-7
lines changed

examples/images.html

Lines changed: 121 additions & 0 deletions
Large diffs are not rendered by default.

examples/images/24_bit.png

35.8 KB
Loading

examples/images/32_bit.png

43.8 KB
Loading

examples/images/jpg.jpg

12.1 KB
Loading

examples/images/png8_trans.png

13.3 KB
Loading

jspdf.plugin.addimage.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ var getJpegSize = function(imgData) {
189189
object instanceof Float64Array );
190190
}
191191
, binaryStringToUint8Array = function(binary_string) {
192+
/*
193+
* not sure how efficient this will be will bigger files. Is there a native method?
194+
*/
192195
var len = binary_string.length;
193196
var bytes = new Uint8Array( len );
194197
for (var i = 0; i < len; i++) {
@@ -197,6 +200,20 @@ var getJpegSize = function(imgData) {
197200
return bytes;
198201
}
199202
, arrayBufferToBinaryString = function( array_buffer ) {
203+
/*
204+
* @see this discussion
205+
* http://stackoverflow.com/questions/6965107/converting-between-strings-and-arraybuffers
206+
*
207+
* As stated, i imagine the method below is highly inefficent for large files. Also of note from Mozilla,
208+
*
209+
* "However, this is slow and error-prone, due to the need for multiple conversions (especially if the binary data is not actually byte-format data, but, for example, 32-bit integers or floats)."
210+
*
211+
* https://developer.mozilla.org/en-US/Add-ons/Code_snippets/StringView
212+
*
213+
* Although i'm strugglig to see how it solves this issue? Doesn't appear to be a direct method for conversion?
214+
*
215+
* Async method using Blob and FileReader could be best, but i'm not sure how to fit it into this flow?
216+
*/
200217
/*var binary_string = '';
201218
//var bytes = new Uint8Array( array_buffer );
202219
var bytes = array_buffer;
@@ -209,9 +226,7 @@ var getJpegSize = function(imgData) {
209226
if(isArrayBufferView())
210227
array_buffer = array_buffer.buffer;
211228

212-
var base64 = base64ArrayBuffer(array_buffer);
213-
214-
return window.atob(base64);
229+
return window.atob(base64ArrayBuffer(array_buffer));
215230
}
216231
, createImageInfo = function(data, wd, ht, cs, bpc, f, imageIndex, alias, dp, trns, pal, smask) {
217232
var info = {
@@ -243,11 +258,13 @@ var getJpegSize = function(imgData) {
243258
return info;
244259
};
245260

246-
jsPDFAPI.addImage = function(imageData, format, x, y, w, h, alias) {
261+
jsPDFAPI.addImage = function(imageData, format, x, y, w, h, alias, transparency) {
247262
'use strict'
248263
var images = this.internal.collections[namespace + 'images'],
249264
cached_info,
250265
binaryStringData;
266+
267+
transparency = transparency || false;
251268

252269
if(typeof format === 'number') {
253270
var tmp = h;
@@ -272,7 +289,7 @@ jsPDFAPI.addImage = function(imageData, format, x, y, w, h, alias) {
272289
throw ('addImage requires canvas to be supported by browser.');
273290
}
274291
ctx.drawImage(imageData, 0, 0, canvas.width, canvas.height);
275-
imageData = canvas.toDataURL();
292+
imageData = canvas.toDataURL(transparency ? 'image/png' : 'image/jpeg');
276293
format = "png";
277294
}
278295

@@ -433,8 +450,9 @@ jsPDFAPI.addImage = function(imageData, format, x, y, w, h, alias) {
433450
if(img.palette && img.palette.length > 0)
434451
pal = img.palette;
435452

436-
if(img.transparency.indexed)
437-
trns = img.transparency.indexed;
453+
//I'm not sure the output from png.js is correct for this?
454+
/*if(img.transparency.indexed)
455+
trns = img.transparency.indexed;*/
438456

439457
info = createImageInfo(arrayBufferToBinaryString(imageData),
440458
img.width,

0 commit comments

Comments
 (0)