jspdf.plugin.addimage.js 7.0 KB

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