pdf_generate_tests.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. $(document).ready(function(){
  2. var dump = false // turn to true to dump contents of PDFs into textareas instead.
  3. if (typeof atob === 'undefined') {
  4. atob = function (data) {
  5. // Decodes string using MIME base64 algorithm
  6. //
  7. // version: 1109.2015
  8. // discuss at: http://phpjs.org/functions/base64_decode
  9. // + original by: Tyler Akins (http://rumkin.com)
  10. // + improved by: Thunder.m
  11. // + input by: Aman Gupta
  12. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  13. // + bugfixed by: Onno Marsman
  14. // + bugfixed by: Pellentesque Malesuada
  15. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  16. // + input by: Brett Zamir (http://brett-zamir.me)
  17. // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  18. // - depends on: utf8_decode
  19. // * example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
  20. // * returns 1: 'Kevin van Zonneveld'
  21. var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  22. var o1, o2, o3, h1, h2, h3, h4, bits,
  23. ac = 0,
  24. i = 0,
  25. l = data.length
  26. tmp_arr = [];
  27. do { // unpack four hexets into three octets using index points in b64
  28. h1 = b64.indexOf(data.charAt(i++));
  29. h2 = b64.indexOf(data.charAt(i++));
  30. h3 = b64.indexOf(data.charAt(i++));
  31. h4 = b64.indexOf(data.charAt(i++));
  32. bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
  33. o1 = bits >> 16 & 0xff;
  34. o2 = bits >> 8 & 0xff;
  35. o3 = bits & 0xff;
  36. if (h3 == 64) {
  37. tmp_arr[ac++] = String.fromCharCode(o1);
  38. } else if (h4 == 64) {
  39. tmp_arr[ac++] = String.fromCharCode(o1, o2);
  40. } else {
  41. tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
  42. }
  43. } while (i < l);
  44. return tmp_arr.join('');
  45. }
  46. }
  47. var displayInTextArea = function(output, label) {
  48. var $l = $('<label for="'+label+'">'+label+'</label>').appendTo(document.body)
  49. $('<textarea id="'+label+'"></textarea>').appendTo($l).text(btoa(output))
  50. }
  51. var datestringregex = /\/CreationDate \(D:\d+\)/
  52. , replacementdatestring = '/CreationDate (D:0)'
  53. , producerstringregex = /\/Producer\s+\(jsPDF\s+\d+\)/
  54. , replacementproducerstring = '/Producer (jsPDF 0)'
  55. , removeMinorDiffs = function(t){
  56. t = t.replace(datestringregex, replacementdatestring)
  57. t = t.replace(producerstringregex, replacementproducerstring)
  58. if (t.trim)
  59. return t.trim()
  60. else
  61. return t
  62. }
  63. , testinventory = {
  64. "001_blankpdf.pdf": function(){
  65. var doc = new jsPDF()
  66. return doc.output()
  67. }
  68. , "002_twopagedoc.pdf":function(){
  69. var doc = new jsPDF()
  70. doc.text('Hello world!', 20, 20)
  71. doc.text('This is client-side Javascript, pumping out a PDF.', 20, 30)
  72. doc.addPage()
  73. doc.text('Do you like that?', 20, 20)
  74. return doc.output()
  75. }
  76. , "002_twopagedoc_oldapi.pdf":function(){
  77. var doc = new jsPDF()
  78. doc.text(20, 20, 'Hello world!')
  79. doc.text(20, 30, 'This is client-side Javascript, pumping out a PDF.')
  80. doc.addPage()
  81. doc.text(20, 20, 'Do you like that?')
  82. return doc.output()
  83. }
  84. , "003_demolandscape.pdf":function(){
  85. var doc = new jsPDF('landscape')
  86. doc.text('Hello landscape world!', 20, 20)
  87. return doc.output()
  88. }
  89. , "004_fontsizes.pdf":function(){
  90. var doc = new jsPDF()
  91. doc.setFontSize(22)
  92. doc.text(20, 20, 'This is a title')
  93. doc.setFontSize(16)
  94. doc.text(20, 30, 'This is some normal sized text underneath.');
  95. return doc.output()
  96. }
  97. , "005_demofonttypes.pdf":function(){
  98. var doc = new jsPDF()
  99. doc.text(20, 20, 'This is the default font.')
  100. doc.setFont("courier")
  101. doc.setFontType("normal")
  102. doc.text(20, 30, 'This is courier normal.')
  103. doc.setFont("times")
  104. doc.setFontType("italic")
  105. doc.text(20, 40, 'This is times italic.')
  106. doc.setFont("helvetica")
  107. doc.setFontType("bold")
  108. doc.text(20, 50, 'This is helvetica bold.')
  109. doc.setFont("courier")
  110. doc.setFontType("bolditalic")
  111. doc.text(20, 60, 'This is courier bolditalic.')
  112. return doc.output()
  113. }
  114. , "006_demotestcolors.pdf":function(){
  115. var doc = new jsPDF()
  116. doc.setTextColor(100)
  117. doc.text(20, 20, 'This is gray.')
  118. doc.setTextColor(150)
  119. doc.text(20, 30, 'This is light gray.')
  120. doc.setTextColor(255,0,0)
  121. doc.text(20, 40, 'This is red.')
  122. doc.setTextColor(0,255,0)
  123. doc.text(20, 50, 'This is green.')
  124. doc.setTextColor(0,0,255)
  125. doc.text(20, 60, 'This is blue.')
  126. return doc.output()
  127. }
  128. , "007_demometadata.pdf":function(){
  129. var doc = new jsPDF()
  130. doc.text(20, 20, 'This PDF has a title, subject, author, keywords and a creator.')
  131. // Optional - set properties on the document
  132. doc.setProperties({
  133. title: 'Title',
  134. subject: 'This is the subject',
  135. author: 'James Hall',
  136. keywords: 'generated, javascript, web 2.0, ajax',
  137. creator: 'MEEE'
  138. })
  139. return doc.output()
  140. }
  141. , "008_demorectangles.pdf":function(){
  142. var doc = new jsPDF()
  143. doc.rect(20, 20, 10, 10); // empty square
  144. doc.rect(40, 20, 10, 10, 'F') // filled square
  145. doc.setDrawColor(255,0,0)
  146. doc.rect(60, 20, 10, 10); // empty red square
  147. doc.setDrawColor(255,0,0)
  148. doc.rect(80, 20, 10, 10, 'FD') // filled square with red borders
  149. doc.setDrawColor(0)
  150. doc.setFillColor(255,0,0)
  151. doc.rect(100, 20, 10, 10, 'F') // filled red square
  152. doc.setDrawColor(0)
  153. doc.setFillColor(255,0,0)
  154. doc.rect(120, 20, 10, 10, 'FD') // filled red square with black borders
  155. return doc.output()
  156. }
  157. , "009_demoliness.pdf":function(){
  158. var doc = new jsPDF()
  159. doc.line(20, 20, 60, 20) // horizontal line
  160. doc.setLineWidth(0.5)
  161. doc.line(20, 25, 60, 25)
  162. doc.setLineWidth(1)
  163. doc.line(20, 30, 60, 30)
  164. doc.setLineWidth(1.5)
  165. doc.line(20, 35, 60, 35)
  166. doc.setDrawColor(255,0,0) // draw red lines
  167. doc.setLineWidth(0.1)
  168. doc.line(100, 20, 100, 60) // vertical line
  169. doc.setLineWidth(0.5)
  170. doc.line(105, 20, 105, 60)
  171. doc.setLineWidth(1)
  172. doc.line(110, 20, 110, 60)
  173. doc.setLineWidth(1.5)
  174. doc.line(115, 20, 115, 60)
  175. return doc.output()
  176. }
  177. , "010_democircles.pdf":function(){
  178. var doc = new jsPDF()
  179. doc.ellipse(40, 20, 10, 5)
  180. doc.setFillColor(0,0,255)
  181. doc.ellipse(80, 20, 10, 5, 'F')
  182. doc.setLineWidth(1)
  183. doc.setDrawColor(0)
  184. doc.setFillColor(255,0,0)
  185. doc.circle(120, 20, 5, 'FD')
  186. return doc.output()
  187. }
  188. , "011_multilinetext.pdf":function(){
  189. var doc = new jsPDF()
  190. , text = [
  191. 'This is line one'
  192. , 'This is line two'
  193. , 'This is line three'
  194. , 'This is line four'
  195. , 'This is line five'
  196. ]
  197. doc.text(text, 20, 20)
  198. return doc.output()
  199. }
  200. , "012_multiplelines.pdf":function(){
  201. var doc = new jsPDF()
  202. , x1 = 40
  203. , y1 = 40
  204. , lines = [
  205. [10,10]
  206. , [-20,10]
  207. , [-15,5,-20,10,-30,15]
  208. ]
  209. doc.lines(lines, x1, y1)
  210. doc.lines(lines, x1, y1, [-1, -1])
  211. doc.lines(lines, x1, y1, [0.5, -0.5])
  212. doc.lines(lines, x1, y1, [-2, 2])
  213. return doc.output()
  214. }
  215. }
  216. , testrunner = function(reference_file_name, test_data_yielder){
  217. asyncTest(reference_file_name, function() {
  218. //QUnit.stop()
  219. require(['text!'+reference_file_name])
  220. .then(function(expectedtext){
  221. QUnit.expect(1)
  222. var output = test_data_yielder()
  223. if (dump) {
  224. displayInTextArea(output, reference_file_name)
  225. QUnit.equal(true, true)
  226. } else {
  227. QUnit.equal(
  228. removeMinorDiffs( output )
  229. , removeMinorDiffs( expectedtext )
  230. )
  231. }
  232. QUnit.start()
  233. //stop()
  234. })
  235. })
  236. }
  237. ////////////////////////////////////////////////////
  238. // running homogenous tests
  239. for (var filename in testinventory){
  240. if (testinventory.hasOwnProperty(filename)){
  241. testrunner(
  242. filename
  243. , testinventory[filename]
  244. )
  245. }
  246. }
  247. asyncTest('013_sillysvgrenderer', function() {
  248. //QUnit.stop()
  249. require(['text!013_sillysvgrenderer.svg', 'text!013_sillysvgrenderer.pdf'])
  250. .then(function(svgtext, expectedtext){
  251. QUnit.expect(1)
  252. var pdf = jsPDF() // 'p','pt','letter')
  253. pdf.addSVG(svgtext, 20, 20, pdf.internal.pageSize.width - 20*2)
  254. // pdf.output('dataurl')
  255. // window.mypdf = pdf.output('dataurlstring')
  256. var output = pdf.output()
  257. if (dump) {
  258. displayInTextArea(output, '013_sillysvgrenderer')
  259. QUnit.equal(true, true)
  260. } else {
  261. QUnit.equal(
  262. removeMinorDiffs( output )
  263. , removeMinorDiffs( expectedtext )
  264. )
  265. }
  266. QUnit.start()
  267. //stop()
  268. })
  269. })
  270. // handcrafted tests
  271. asyncTest('014_addImage', function() {
  272. //QUnit.stop()
  273. require(
  274. ['text!014_addImage.jpeg.base64.txt', 'text!014_addImage.pdf.base64.txt']
  275. ).then(function(base64encodedJpeg, base64encodedPDF){
  276. QUnit.expect(2)
  277. var pdf = jsPDF()
  278. pdf.addImage(atob(base64encodedJpeg), 'jpeg', 20, 20)
  279. QUnit.equal(
  280. // just testing if it does not blow up.
  281. pdf.output('datauristring') !== ''
  282. , true
  283. )
  284. pdf = jsPDF()
  285. pdf.addImage(atob(base64encodedJpeg), 'jpeg', 20, 20)
  286. // window.pdfbase64 = btoa(pdf.output())
  287. var output = pdf.output()
  288. if (dump) {
  289. displayInTextArea(output, '014_addImage')
  290. QUnit.equal(true, true)
  291. } else {
  292. QUnit.equal(
  293. removeMinorDiffs( output )
  294. , removeMinorDiffs( atob(base64encodedPDF) )
  295. )
  296. }
  297. QUnit.start()
  298. //stop()
  299. })
  300. })
  301. // handcrafted tests
  302. asyncTest('015_splittext', function() {
  303. //QUnit.stop()
  304. require(
  305. ['015_splittext', 'text!015_splittext.pdf']
  306. ).then(function(runner, shouldbe){
  307. QUnit.expect(1)
  308. var pdf = runner(jsPDF)
  309. var output = pdf.output()
  310. if (dump) {
  311. displayInTextArea(output, '015_splittext')
  312. QUnit.equal(true, true)
  313. } else {
  314. QUnit.equal(
  315. removeMinorDiffs( output )
  316. , removeMinorDiffs( shouldbe )
  317. )
  318. }
  319. QUnit.start()
  320. //stop()
  321. })
  322. })
  323. }) // end of document.ready(