test_harness.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * jsPDF PDF Test Harness
  3. * Copyright (c) 2014 Steven Spungin (TwelveTone LLC) steven@twelvetone.tv
  4. *
  5. * Licensed under the MIT License.
  6. * http://opensource.org/licenses/mit-license
  7. */
  8. /**
  9. * An easy way to view PDF and PDF source code side by side.
  10. */
  11. pdf_test_harness_init = function(pdf, message) {
  12. var harness = new pdf_test_harness();
  13. var body = document.getElementsByTagName('body')[0];
  14. body.style.display = 'flex';
  15. var div = document.createElement('div');
  16. div.setAttribute('style', 'position:fixed;height:20px;left:0;right:0;background:lightblue');
  17. body.appendChild(div);
  18. harness.header = div;
  19. var div2 = document.createElement('div');
  20. div2.setAttribute('style', 'position:fixed;display:flex;top:20px; bottom:0;left:0;right:0');
  21. body.appendChild(div2);
  22. harness.body = div2;
  23. var btn1 = document.createElement('input');
  24. btn1.setAttribute('type', 'radio');
  25. btn1.setAttribute('name', 'view');
  26. div.appendChild(btn1);
  27. btn1.checked = true;
  28. var lbl1 = document.createElement('label');
  29. lbl1.setAttribute('for', 'btn1');
  30. lbl1.innerHTML = 'PDF'
  31. div.appendChild(lbl1);
  32. var btn2 = document.createElement('input');
  33. btn2.setAttribute('type', 'radio');
  34. btn2.setAttribute('name', 'view');
  35. div.appendChild(btn2);
  36. var lbl2 = document.createElement('label');
  37. lbl2.setAttribute('for', 'btn2');
  38. lbl2.innerHTML = 'Source'
  39. div.appendChild(lbl2);
  40. var btn3 = document.createElement('input');
  41. btn3.setAttribute('type', 'radio');
  42. btn3.setAttribute('name', 'view');
  43. div.appendChild(btn3);
  44. var lbl3 = document.createElement('label');
  45. lbl3.setAttribute('for', 'btn3');
  46. lbl3.innerHTML = 'Both'
  47. div.appendChild(lbl3);
  48. harness.source = document.createElement('pre');
  49. harness.source.setAttribute('style', 'margin-top:0;width:100%;height:100%;position:absolute;top:0px;bottom:0px;overflow:auto');
  50. div2.appendChild(harness.source);
  51. harness.iframe = document.createElement('iframe');
  52. harness.iframe.setAttribute('style', 'width:100%;height:100%;position:absolute;overflow:auto;top:0px;bottom:0px');
  53. div2.appendChild(harness.iframe);
  54. //if (pdf_test_harness.onload) {
  55. //harness.pdf = pdf_test_harness.onload(harness);
  56. if (message) {
  57. message += "<p style='text-align:center;font-style:italic;font-size:.8em'>click to close</p>";
  58. var popup = document.createElement('div');
  59. popup.setAttribute('style', 'z-index:100;margin:100px auto;cursor:pointer;font-size:1.3em;top:50px;background-color:rgb(243, 224, 141);padding:1em;border:1px solid black');
  60. popup.innerHTML = message;
  61. body.appendChild(popup);
  62. popup.onclick = function() {
  63. popup.parentNode.removeChild(popup);
  64. }
  65. }
  66. //}
  67. harness.pdf = pdf;
  68. harness.render('pdf');
  69. btn1.onclick = function() {
  70. harness.render('pdf');
  71. }
  72. btn2.onclick = function() {
  73. harness.render('source');
  74. }
  75. btn3.onclick = function() {
  76. harness.render('both');
  77. }
  78. return harness;
  79. }
  80. pdf_test_harness = function(pdf) {
  81. this.pdf = pdf;
  82. this.onload = undefined;
  83. this.iframe = undefined;
  84. this.entityMap = {
  85. "&" : "&amp;",
  86. "<" : "&lt;",
  87. ">" : "&gt;",
  88. '"' : '&quot;',
  89. "'" : '&#39;',
  90. "/" : '&#x2F;'
  91. };
  92. this.escapeHtml = function(string) {
  93. return String(string).replace(/[&<>"'\/]/g, function(s) {
  94. return this.entityMap[s];
  95. }.bind(this));
  96. };
  97. this.getParameterByName = function(name) {
  98. name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  99. var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search);
  100. return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  101. };
  102. this.setPdf = function(pdf) {
  103. this.pdf = pdf;
  104. this.rendered = undefined;
  105. this.render(this.view);
  106. };
  107. // generate the pdf, the source code, or both
  108. this.render = function(view) {
  109. this.view = view;
  110. //Current code only lets us render one time.
  111. if (!this.rendered) {
  112. this.rendered = this.pdf.output('datauristring');
  113. this.iframe.src = this.rendered;
  114. var raw = this.pdf.output();
  115. raw = this.escapeHtml(raw);
  116. this.source.innerHTML = raw;
  117. }
  118. if ('pdf' === view) {
  119. this.source.style.display = 'none';
  120. this.iframe.style.display = 'block';
  121. this.iframe.style.width = '100%';
  122. } else if ('source' === view) {
  123. this.iframe.style.display = 'none';
  124. this.source.style.display = 'block';
  125. this.source.style.width = '100%';
  126. }
  127. if ('both' === view) {
  128. raw = this.escapeHtml(raw);
  129. this.iframe.style.width = '50%';
  130. this.iframe.style.position = 'relative';
  131. this.iframe.style.display = 'inline-block';
  132. this.source.style.width = '50%';
  133. this.source.style.position = 'relative';
  134. this.source.style.display = 'inline-block';
  135. }
  136. }
  137. }