|
547 | 547 | jsPDFAPI.addImage = function(imageData, format, x, y, w, h, alias, compression, rotation) {
|
548 | 548 | 'use strict'
|
549 | 549 |
|
| 550 | + var tmpImageData = ''; |
| 551 | + |
550 | 552 | if(typeof format !== 'string') {
|
551 | 553 | var tmp = h;
|
552 | 554 | h = w;
|
|
589 | 591 |
|
590 | 592 | if (!(info = checkImagesForAlias(alias, images))) {
|
591 | 593 | 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 | + } |
593 | 604 | }
|
594 | 605 | format = this.getImageFileTypeByImageData(imageData);
|
595 | 606 |
|
|
627 | 638 |
|
628 | 639 | jsPDFAPI.convertStringToImageData = function (stringData) {
|
629 | 640 | var base64Info;
|
630 |
| - var imageData; |
| 641 | + var imageData = ''; |
631 | 642 | if(this.isString(stringData)) {
|
632 | 643 | var base64Info = this.extractInfoFromBase64DataURI(stringData);
|
633 | 644 |
|
634 | 645 | if(base64Info !== null) {
|
635 | 646 | if (jsPDFAPI.validateStringAsBase64(base64Info[3])) {
|
636 | 647 | imageData = atob(base64Info[3]);//convert to binary string
|
637 |
| - } else { |
638 |
| - throw new Error("addImage expects a valid base64 encoded DataUrl-String.") |
639 |
| - } |
| 648 | + } |
640 | 649 | } else if (jsPDFAPI.validateStringAsBase64(stringData)){
|
641 | 650 | imageData = atob(stringData);
|
642 |
| - }else { |
643 |
| - throw new Error("addImage expects atleast a valid base64-String.") |
644 | 651 | }
|
645 | 652 | }
|
646 | 653 | return imageData;
|
|
773 | 780 | return this.processJPEG.apply(this, arguments);
|
774 | 781 | }
|
775 | 782 |
|
| 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 | + |
776 | 905 | })(jsPDF.API);
|
0 commit comments