| 9 |
lars |
1 |
/** @preserve
|
|
|
2 |
jsPDF addImage plugin (JPEG only at this time)
|
|
|
3 |
Copyright (c) 2012 https://github.com/siefkenj/
|
|
|
4 |
*/
|
|
|
5 |
|
|
|
6 |
/**
|
|
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
8 |
* a copy of this software and associated documentation files (the
|
|
|
9 |
* "Software"), to deal in the Software without restriction, including
|
|
|
10 |
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
11 |
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
12 |
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
13 |
* the following conditions:
|
|
|
14 |
*
|
|
|
15 |
* The above copyright notice and this permission notice shall be
|
|
|
16 |
* included in all copies or substantial portions of the Software.
|
|
|
17 |
*
|
|
|
18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
19 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
20 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
21 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
22 |
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
23 |
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
24 |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
25 |
* ====================================================================
|
|
|
26 |
*/
|
|
|
27 |
|
|
|
28 |
;(function(jsPDFAPI) {
|
|
|
29 |
'use strict'
|
|
|
30 |
|
|
|
31 |
var namespace = 'addImage_'
|
|
|
32 |
|
|
|
33 |
// takes a string imgData containing the raw bytes of
|
|
|
34 |
// a jpeg image and returns [width, height]
|
|
|
35 |
// Algorithm from: http://www.64lines.com/jpeg-width-height
|
|
|
36 |
var getJpegSize = function(imgData) {
|
|
|
37 |
'use strict'
|
|
|
38 |
var width, height;
|
|
|
39 |
// Verify we have a valid jpeg header 0xff,0xd8,0xff,0xe0,?,?,'J','F','I','F',0x00
|
|
|
40 |
if (!imgData.charCodeAt(0) === 0xff ||
|
|
|
41 |
!imgData.charCodeAt(1) === 0xd8 ||
|
|
|
42 |
!imgData.charCodeAt(2) === 0xff ||
|
|
|
43 |
!imgData.charCodeAt(3) === 0xe0 ||
|
|
|
44 |
!imgData.charCodeAt(6) === 'J'.charCodeAt(0) ||
|
|
|
45 |
!imgData.charCodeAt(7) === 'F'.charCodeAt(0) ||
|
|
|
46 |
!imgData.charCodeAt(8) === 'I'.charCodeAt(0) ||
|
|
|
47 |
!imgData.charCodeAt(9) === 'F'.charCodeAt(0) ||
|
|
|
48 |
!imgData.charCodeAt(10) === 0x00) {
|
|
|
49 |
throw new Error('getJpegSize requires a binary jpeg file')
|
|
|
50 |
}
|
|
|
51 |
var blockLength = imgData.charCodeAt(4)*256 + imgData.charCodeAt(5);
|
|
|
52 |
var i = 4, len = imgData.length;
|
|
|
53 |
while ( i < len ) {
|
|
|
54 |
i += blockLength;
|
|
|
55 |
if (imgData.charCodeAt(i) !== 0xff) {
|
|
|
56 |
throw new Error('getJpegSize could not find the size of the image');
|
|
|
57 |
}
|
|
|
58 |
if (imgData.charCodeAt(i+1) === 0xc0) {
|
|
|
59 |
height = imgData.charCodeAt(i+5)*256 + imgData.charCodeAt(i+6);
|
|
|
60 |
width = imgData.charCodeAt(i+7)*256 + imgData.charCodeAt(i+8);
|
|
|
61 |
return [width, height];
|
|
|
62 |
} else {
|
|
|
63 |
i += 2;
|
|
|
64 |
blockLength = imgData.charCodeAt(i)*256 + imgData.charCodeAt(i+1)
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
// Image functionality ported from pdf.js
|
|
|
69 |
, putImage = function(img) {
|
|
|
70 |
var objectNumber = this.internal.newObject()
|
|
|
71 |
, out = this.internal.write
|
|
|
72 |
, putStream = this.internal.putStream
|
|
|
73 |
|
|
|
74 |
img['n'] = objectNumber
|
|
|
75 |
|
|
|
76 |
out('<</Type /XObject')
|
|
|
77 |
out('/Subtype /Image')
|
|
|
78 |
out('/Width ' + img['w'])
|
|
|
79 |
out('/Height ' + img['h'])
|
|
|
80 |
if (img['cs'] === 'Indexed') {
|
|
|
81 |
out('/ColorSpace [/Indexed /DeviceRGB '
|
|
|
82 |
+ (img['pal'].length / 3 - 1) + ' ' + (objectNumber + 1)
|
|
|
83 |
+ ' 0 R]');
|
|
|
84 |
} else {
|
|
|
85 |
out('/ColorSpace /' + img['cs']);
|
|
|
86 |
if (img['cs'] === 'DeviceCMYK') {
|
|
|
87 |
out('/Decode [1 0 1 0 1 0 1 0]');
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
out('/BitsPerComponent ' + img['bpc']);
|
|
|
91 |
if ('f' in img) {
|
|
|
92 |
out('/Filter /' + img['f']);
|
|
|
93 |
}
|
|
|
94 |
if ('dp' in img) {
|
|
|
95 |
out('/DecodeParms <<' + img['dp'] + '>>');
|
|
|
96 |
}
|
|
|
97 |
if ('trns' in img && img['trns'].constructor == Array) {
|
|
|
98 |
var trns = '';
|
|
|
99 |
for ( var i = 0; i < img['trns'].length; i++) {
|
|
|
100 |
trns += (img[trns][i] + ' ' + img['trns'][i] + ' ');
|
|
|
101 |
out('/Mask [' + trns + ']');
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
if ('smask' in img) {
|
|
|
105 |
out('/SMask ' + (objectNumber + 1) + ' 0 R');
|
|
|
106 |
}
|
|
|
107 |
out('/Length ' + img['data'].length + '>>');
|
|
|
108 |
|
|
|
109 |
putStream(img['data']);
|
|
|
110 |
|
|
|
111 |
out('endobj');
|
|
|
112 |
}
|
|
|
113 |
, putResourcesCallback = function() {
|
|
|
114 |
var images = this.internal.collections[namespace + 'images']
|
|
|
115 |
for ( var i in images ) {
|
|
|
116 |
putImage.call(this, images[i])
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
, putXObjectsDictCallback = function(){
|
|
|
120 |
var images = this.internal.collections[namespace + 'images']
|
|
|
121 |
, out = this.internal.write
|
|
|
122 |
, image
|
|
|
123 |
for (var i in images) {
|
|
|
124 |
image = images[i]
|
|
|
125 |
out(
|
|
|
126 |
'/I' + image['i']
|
|
|
127 |
, image['n']
|
|
|
128 |
, '0'
|
|
|
129 |
, 'R'
|
|
|
130 |
)
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
jsPDFAPI.addImage = function(imageData, format, x, y, w, h) {
|
|
|
135 |
'use strict'
|
|
|
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 |
}
|
|
|
149 |
if (format.toUpperCase() !== 'JPEG') {
|
|
|
150 |
throw new Error('addImage currently only supports format \'JPEG\', not \''+format+'\'');
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
var imageIndex
|
|
|
154 |
, images = this.internal.collections[namespace + 'images']
|
|
|
155 |
, coord = this.internal.getCoordinateString
|
|
|
156 |
, vcoord = this.internal.getVerticalCoordinateString;
|
|
|
157 |
|
|
|
158 |
// Detect if the imageData is raw binary or Data URL
|
|
|
159 |
if (imageData.substring(0, 23) === 'data:image/jpeg;base64,') {
|
|
|
160 |
imageData = atob(imageData.replace('data:image/jpeg;base64,', ''));
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
if (images){
|
|
|
164 |
// this is NOT the first time this method is ran on this instance of jsPDF object.
|
|
|
165 |
imageIndex = Object.keys ?
|
|
|
166 |
Object.keys(images).length :
|
|
|
167 |
(function(o){
|
|
|
168 |
var i = 0
|
|
|
169 |
for (var e in o){if(o.hasOwnProperty(e)){ i++ }}
|
|
|
170 |
return i
|
|
|
171 |
})(images)
|
|
|
172 |
} else {
|
|
|
173 |
// this is the first time this method is ran on this instance of jsPDF object.
|
|
|
174 |
imageIndex = 0
|
|
|
175 |
this.internal.collections[namespace + 'images'] = images = {}
|
|
|
176 |
this.internal.events.subscribe('putResources', putResourcesCallback)
|
|
|
177 |
this.internal.events.subscribe('putXobjectDict', putXObjectsDictCallback)
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
var dims = getJpegSize(imageData);
|
|
|
181 |
var info = {
|
|
|
182 |
w : dims[0],
|
|
|
183 |
h : dims[1],
|
|
|
184 |
cs : 'DeviceRGB',
|
|
|
185 |
bpc : 8,
|
|
|
186 |
f : 'DCTDecode',
|
|
|
187 |
i : imageIndex,
|
|
|
188 |
data : imageData
|
|
|
189 |
// n: objectNumber will be added by putImage code
|
|
|
190 |
|
|
|
191 |
};
|
|
|
192 |
images[imageIndex] = info
|
|
|
193 |
if (!w && !h) {
|
|
|
194 |
w = -96;
|
|
|
195 |
h = -96;
|
|
|
196 |
}
|
|
|
197 |
if (w < 0) {
|
|
|
198 |
w = (-1) * info['w'] * 72 / w / this.internal.scaleFactor;
|
|
|
199 |
}
|
|
|
200 |
if (h < 0) {
|
|
|
201 |
h = (-1) * info['h'] * 72 / h / this.internal.scaleFactor;
|
|
|
202 |
}
|
|
|
203 |
if (w === 0) {
|
|
|
204 |
w = h * info['w'] / info['h'];
|
|
|
205 |
}
|
|
|
206 |
if (h === 0) {
|
|
|
207 |
h = w * info['h'] / info['w'];
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
this.internal.write(
|
|
|
211 |
'q'
|
|
|
212 |
, coord(w)
|
|
|
213 |
, '0 0'
|
|
|
214 |
, coord(h) // TODO: check if this should be shifted by vcoord
|
|
|
215 |
, coord(x)
|
|
|
216 |
, vcoord(y + h)
|
|
|
217 |
, 'cm /I'+info['i']
|
|
|
218 |
, 'Do Q'
|
|
|
219 |
)
|
|
|
220 |
|
|
|
221 |
return this
|
|
|
222 |
}
|
|
|
223 |
})(jsPDF.API)
|