Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 50d8b1e

Browse files
authoredFeb 22, 2018
Extend Image-Plugin (#1638)
* Update karma.conf.js * Merge (#7) * Update addimage.js Solution to solve memory leaks caused by big images. Is solving the issues #844 and #425 * normalize Color Methods * f2 => f3 * make it black * make it gray again typeof comparison was wrong... * make typeof comparison wrong to pass test * Update jspdf.js d'oh * Update jspdf.js correct it again.... * operation fix ch1 === ch2 === ch3 is wrong. it would process from left to right resulting in a ch1 === ch2 => true, true === ch3 => false * update test pdf fix it so that the test is passed * Update rectangles.pdf stupid letter * Update rectangles.pdf fix length of content * Update rectangles.pdf ... why? * Update jspdf.js DRY it up add cssColor to rgb conversion * Update jspdf.js bugfix * Update standard.spec.js add test for colornames * Update standard.spec.js * Update standard.spec.js * Update jspdf.js convert short rgb to long form * Update standard.spec.js add test case for short rgb-values * Update jspdf.js add a setter for the CreationDate * Update jspdf.js some major changes to the setCreationDate Algorythm * Update jspdf.js bugfix * Update jspdf.js darn... * Update jspdf.js * Update jspdf.js * Update jspdf.js bugfix * Update addimage.js round is not the proper method. It has to be ceil so that the splice-method is called atleast once. * add polyfill for Object.assign * Add files via upload * Add files via upload fix small mistake * fix tainted standardfontsmetrics * Add files via upload one fix * Update acroform.js * update references * modify tests * force testing * Update acroform.js * Add files via upload * Update acroform.js * update references * Add files via upload * Update acroform.js * Update acroform.js * Add files via upload * Update acroform.js * Add files via upload * Add files via upload * Update acroform.js * Add files via upload * addimage.js add cmyk recognition for jpg * make it less code * Update saucelabs.karma.conf.js * Create standard.spec.js * Create jpg.pdf * Rename tests/addimage/jpg.pdf to tests/addimage/reference/jpg.pdf * Add files via upload * test first if jpeg is working * Add files via upload * Update jpeg.spec.js * Add files via upload * Add files via upload * Add files via upload * Update saucelabs.karma.conf.js * Update jpg.b64 * Update addhtml.js #1480 * add ability to recognize filetype by header * remove unnecessary variable * typo-fix * Update addimage.js * Update addimage.js #966 * Update compare.js * Update jpeg.spec.js * Update compare.js * Update compare.js * Update jpeg.spec.js * Update saucelabs.karma.conf.js * Update compare.js * Update compare.js * Update compare.js * Update jpeg.spec.js * Update compare.js * Update jpeg.spec.js * Update compare.js * Update compare.js * Update jpeg.spec.js * Add files via upload * Update compare.js * Update jpeg.spec.js * Update compare.js * Update jpeg.spec.js * Update compare.js * Update compare.js * Update compare.js D'Oh * Update compare.js * Update jpeg.spec.js * Update compare.js * Update compare.js * Update compare.js * Update jpeg.spec.js why is here a done? fml * Add files via upload * Add files via upload * Add files via upload * Delete jpg.b64 * Update jpeg.spec.js * Update compare.js * Update saucelabs.karma.conf.js * Update png.spec.js * Update saucelabs.karma.conf.js * Update addimage.js * Create filetypeRecognition.spec.js * Update saucelabs.karma.conf.js * Update filetypeRecognition.spec.js * Update addimage.js * fix IE error * Update standard.spec.js HideWindowUI.pdf is misssing * compare.js make it look good * add tests * Update saucelabs.karma.conf.js * Fix for #905 #1163 #1317 adler32cs is no longer maintained, made package.json point to my fork of adler32cs that contains the loader.js fix * small modification * Update outline.js * Add files via upload * Update standard.spec.js * Update karma.conf.js to load outline.js * Update saucelabs.karma.conf.js to load outline.js * chore(package): update rollup-plugin-babel to version 3.0.3 * chore(package): update karma to version 2.0.0 * Add new method for internal, getTextColor * Update dist files and docs to include getTextColor * Revise getTextColor to return hex code; add new spec * Compiled assets for getTextColor modifications * Updates spec and documentation per feedback * chore(package): update uglify-js to version 3.3.5 Closes #1506 * simplify * addCreationDate * Update standard.spec.js * Update standard.spec.js * Update jspdf.js * Update standard.spec.js * Update standard.spec.js * Add png adam7 interlace (#6) * Update png.js Adapted from here foliojs/png.js#8 * make it look good * fix possible merging error * fix minor bug * hopefull removed all dupes * Update main.js * Don't skip HideWindowUI * Update addimage.js * Update compare.js * Add files via upload * Update jspdf.js * Update jspdf.js * Update addhtml.js * Update outline.js * Update cell.js * Update annotations.js * Update from_html.js * Update context2d.js * Update acroform.js * Update addimage.js * Update karma.conf.js * Create getImageProperties.spec.js * typo
1 parent 70d0033 commit 50d8b1e

File tree

2 files changed

+179
-7
lines changed

2 files changed

+179
-7
lines changed
 

‎plugins/addimage.js

+136-7
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,8 @@
547547
jsPDFAPI.addImage = function(imageData, format, x, y, w, h, alias, compression, rotation) {
548548
'use strict'
549549

550+
var tmpImageData = '';
551+
550552
if(typeof format !== 'string') {
551553
var tmp = h;
552554
h = w;
@@ -589,7 +591,16 @@
589591

590592
if (!(info = checkImagesForAlias(alias, images))) {
591593
if(this.isString(imageData)) {
592-
imageData = this.convertStringToImageData(imageData);
594+
tmpImageData = this.convertStringToImageData(imageData);
595+
596+
if (tmpImageData !== '') {
597+
imageData = tmpImageData;
598+
} else {
599+
tmpImageData = this.loadImageFile(imageData);
600+
if (tmpImageData !== undefined) {
601+
imageData = tmpImageData;
602+
}
603+
}
593604
}
594605
format = this.getImageFileTypeByImageData(imageData);
595606

@@ -627,20 +638,16 @@
627638

628639
jsPDFAPI.convertStringToImageData = function (stringData) {
629640
var base64Info;
630-
var imageData;
641+
var imageData = '';
631642
if(this.isString(stringData)) {
632643
var base64Info = this.extractInfoFromBase64DataURI(stringData);
633644

634645
if(base64Info !== null) {
635646
if (jsPDFAPI.validateStringAsBase64(base64Info[3])) {
636647
imageData = atob(base64Info[3]);//convert to binary string
637-
} else {
638-
throw new Error("addImage expects a valid base64 encoded DataUrl-String.")
639-
}
648+
}
640649
} else if (jsPDFAPI.validateStringAsBase64(stringData)){
641650
imageData = atob(stringData);
642-
}else {
643-
throw new Error("addImage expects atleast a valid base64-String.")
644651
}
645652
}
646653
return imageData;
@@ -773,4 +780,126 @@
773780
return this.processJPEG.apply(this, arguments);
774781
}
775782

783+
jsPDFAPI.loadImageFile = function (path, sync, callback) {
784+
sync = sync || true;
785+
callback = callback || function () {};
786+
var isNode = Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
787+
788+
var xhrMethod = function (url, sync, callback) {
789+
var req = new XMLHttpRequest();
790+
var byteArray = [];
791+
var i = 0;
792+
793+
var sanitizeUnicode = function (data) {
794+
var dataLength = data.length;
795+
var StringFromCharCode = String.fromCharCode;
796+
797+
//Transform Unicode to ASCII
798+
for (i = 0; i < dataLength; i += 1) {
799+
byteArray.push(StringFromCharCode(data.charCodeAt(i) & 0xff))
800+
}
801+
return byteArray.join("");
802+
}
803+
804+
req.open('GET', url, !sync)
805+
// XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
806+
req.overrideMimeType('text\/plain; charset=x-user-defined');
807+
808+
if (sync === false) {
809+
req.onload = function () {
810+
return sanitizeUnicode(this.responseText);
811+
};
812+
}
813+
req.send(null)
814+
815+
if (req.status !== 200) {
816+
console.warn('Unable to load file "' + url + '"');
817+
return;
818+
}
819+
820+
if (sync) {
821+
return sanitizeUnicode(req.responseText);
822+
}
823+
};
824+
825+
var nodeJSMethod = function (path, sync, callback) {
826+
sync = sync || true;
827+
var fs = require('fs');
828+
if (sync === true) {
829+
var data = fs.readFileSync(path).toString();
830+
return data;
831+
} else {
832+
fs.readFile('image.jpg', function(err, data) {
833+
callback(data);
834+
});
835+
}
836+
}
837+
838+
//we have a browser and probably no CORS-Problem
839+
if (typeof window !== undefined && typeof location === "object" && location.protocol.substr(0,4) === "http") {
840+
return xhrMethod(path, sync, callback);
841+
}else if (isNode) {
842+
return nodeJSMethod(path, sync, callback);
843+
} else {
844+
//We have CORS restriction.
845+
}
846+
}
847+
848+
jsPDFAPI.getImageProperties = function (imageData) {
849+
var info;
850+
var tmpImageData = '';
851+
var format;
852+
var dataAsBinaryString;
853+
854+
if(isDOMElement(imageData)) {
855+
imageData = createDataURIFromElement(imageData);
856+
}
857+
858+
if(this.isString(imageData)) {
859+
tmpImageData = this.convertStringToImageData(imageData);
860+
861+
if (tmpImageData !== '') {
862+
imageData = tmpImageData;
863+
} else {
864+
tmpImageData = this.loadImageFile(imageData);
865+
if (tmpImageData !== undefined) {
866+
imageData = tmpImageData;
867+
}
868+
}
869+
}
870+
format = this.getImageFileTypeByImageData(imageData);
871+
872+
if(!isImageTypeSupported(format))
873+
throw new Error('addImage does not support files of type \''+format+'\', please ensure that a plugin for \''+format+'\' support is added.');
874+
875+
/**
876+
* need to test if it's more efficient to convert all binary strings
877+
* to TypedArray - or should we just leave and process as string?
878+
*/
879+
if(this.supportsArrayBuffer()) {
880+
// no need to convert if imageData is already uint8array
881+
if(!(imageData instanceof Uint8Array)){
882+
dataAsBinaryString = imageData;
883+
imageData = this.binaryStringToUint8Array(imageData);
884+
}
885+
}
886+
887+
info = this['process' + format.toUpperCase()](
888+
imageData
889+
);
890+
891+
if(!info){
892+
throw new Error('An unkwown error occurred whilst processing the image');
893+
}
894+
895+
return {
896+
fileType : format,
897+
width: info.w,
898+
height: info.h,
899+
colorSpace: info.cs,
900+
compressionMode: info.f,
901+
bitsPerComponent: info.bpc
902+
};
903+
}
904+
776905
})(jsPDF.API);

‎tests/addimage/getImageProperties.spec.js

+43
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.