plugins_outline.js.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>plugins/outline.js - Documentation</title>
  6. <script src="scripts/prettify/prettify.js"></script>
  7. <script src="scripts/prettify/lang-css.js"></script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
  13. </head>
  14. <body>
  15. <input type="checkbox" id="nav-trigger" class="nav-trigger" />
  16. <label for="nav-trigger" class="navicon-button x">
  17. <div class="navicon"></div>
  18. </label>
  19. <label for="nav-trigger" class="overlay"></label>
  20. <nav>
  21. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="jsPDF.html">jsPDF</a></li></ul><h3>Global</h3><ul><li><a href="global.html#addFont">addFont</a></li><li><a href="global.html#addHTML">addHTML</a></li><li><a href="global.html#addMetadata">addMetadata</a></li><li><a href="global.html#addPage">addPage</a></li><li><a href="global.html#autoPrint">autoPrint</a></li><li><a href="global.html#CapJoinStyles">CapJoinStyles</a></li><li><a href="global.html#circle">circle</a></li><li><a href="global.html#ellipse">ellipse</a></li><li><a href="global.html#getFontList">getFontList</a></li><li><a href="global.html#lines">lines</a></li><li><a href="global.html#lstext">lstext</a></li><li><a href="global.html#output">output</a></li><li><a href="global.html#rect">rect</a></li><li><a href="global.html#roundedRect">roundedRect</a></li><li><a href="global.html#save">save</a></li><li><a href="global.html#setDisplayMode">setDisplayMode</a></li><li><a href="global.html#setDrawColor">setDrawColor</a></li><li><a href="global.html#setFillColor">setFillColor</a></li><li><a href="global.html#setFont">setFont</a></li><li><a href="global.html#setFontSize">setFontSize</a></li><li><a href="global.html#setFontStyle">setFontStyle</a></li><li><a href="global.html#setLineCap">setLineCap</a></li><li><a href="global.html#setLineJoin">setLineJoin</a></li><li><a href="global.html#setLineWidth">setLineWidth</a></li><li><a href="global.html#setPage">setPage</a></li><li><a href="global.html#setProperties">setProperties</a></li><li><a href="global.html#setTextColor">setTextColor</a></li><li><a href="global.html#text">text</a></li><li><a href="global.html#triangle">triangle</a></li></ul>
  22. </nav>
  23. <div id="main">
  24. <h1 class="page-title">plugins/outline.js</h1>
  25. <section>
  26. <article>
  27. <pre class="prettyprint source linenums"><code>/**
  28. * jsPDF Outline PlugIn
  29. * Copyright (c) 2014 Steven Spungin (TwelveTone LLC) steven@twelvetone.tv
  30. *
  31. * Licensed under the MIT License.
  32. * http://opensource.org/licenses/mit-license
  33. */
  34. /**
  35. * Generates a PDF Outline
  36. */
  37. ;
  38. (function(jsPDFAPI) {
  39. 'use strict';
  40. jsPDFAPI.events.push([
  41. 'postPutResources', function() {
  42. var pdf = this;
  43. var rx = /^(\d+) 0 obj$/;
  44. // Write action goto objects for each page
  45. // this.outline.destsGoto = [];
  46. // for (var i = 0; i &lt; totalPages; i++) {
  47. // var id = pdf.internal.newObject();
  48. // this.outline.destsGoto.push(id);
  49. // pdf.internal.write("&lt;&lt;/D[" + (i * 2 + 3) + " 0 R /XYZ null
  50. // null null]/S/GoTo>> endobj");
  51. // }
  52. //
  53. // for (var i = 0; i &lt; dests.length; i++) {
  54. // pdf.internal.write("(page_" + (i + 1) + ")" + dests[i] + " 0
  55. // R");
  56. // }
  57. //
  58. if (this.outline.root.children.length > 0) {
  59. var lines = pdf.outline.render().split(/\r\n/);
  60. for (var i = 0; i &lt; lines.length; i++) {
  61. var line = lines[i];
  62. var m = rx.exec(line);
  63. if (m != null) {
  64. var oid = m[1];
  65. pdf.internal.newObjectDeferredBegin(oid);
  66. }
  67. pdf.internal.write(line);
  68. }
  69. }
  70. // This code will write named destination for each page reference
  71. // (page_1, etc)
  72. if (this.outline.createNamedDestinations) {
  73. var totalPages = this.internal.pages.length;
  74. // WARNING: this assumes jsPDF starts on page 3 and pageIDs
  75. // follow 5, 7, 9, etc
  76. // Write destination objects for each page
  77. var dests = [];
  78. for (var i = 0; i &lt; totalPages; i++) {
  79. var id = pdf.internal.newObject();
  80. dests.push(id);
  81. var info = pdf.internal.getPageInfo(i+1);
  82. pdf.internal.write("&lt;&lt; /D[" + info.objId + " 0 R /XYZ null null null]>> endobj");
  83. }
  84. // assign a name for each destination
  85. var names2Oid = pdf.internal.newObject();
  86. pdf.internal.write('&lt;&lt; /Names [ ');
  87. for (var i = 0; i &lt; dests.length; i++) {
  88. pdf.internal.write("(page_" + (i + 1) + ")" + dests[i] + " 0 R");
  89. }
  90. pdf.internal.write(' ] >>', 'endobj');
  91. // var kids = pdf.internal.newObject();
  92. // pdf.internal.write('&lt;&lt; /Kids [ ' + names2Oid + ' 0 R');
  93. // pdf.internal.write(' ] >>', 'endobj');
  94. var namesOid = pdf.internal.newObject();
  95. pdf.internal.write('&lt;&lt; /Dests ' + names2Oid + " 0 R");
  96. pdf.internal.write('>>', 'endobj');
  97. }
  98. }
  99. ]);
  100. jsPDFAPI.events.push([
  101. 'putCatalog', function() {
  102. var pdf = this;
  103. if (pdf.outline.root.children.length > 0) {
  104. pdf.internal.write("/Outlines", this.outline.makeRef(this.outline.root));
  105. if (this.outline.createNamedDestinations) {
  106. pdf.internal.write("/Names " + namesOid + " 0 R");
  107. }
  108. // Open with Bookmarks showing
  109. // pdf.internal.write("/PageMode /UseOutlines");
  110. }
  111. }
  112. ]);
  113. jsPDFAPI.events.push([
  114. 'initialized', function() {
  115. var pdf = this;
  116. pdf.outline = {
  117. createNamedDestinations : false,
  118. root : {
  119. children : []
  120. }
  121. };
  122. var namesOid;
  123. var destsGoto = [];
  124. /**
  125. * Options: pageNumber
  126. */
  127. pdf.outline.add = function(parent,title,options) {
  128. var item = {
  129. title : title,
  130. options : options,
  131. children : []
  132. };
  133. if (parent == null) {
  134. parent = this.root;
  135. }
  136. parent.children.push(item);
  137. return item;
  138. }
  139. pdf.outline.render = function() {
  140. this.ctx = {};
  141. this.ctx.val = '';
  142. this.ctx.pdf = pdf;
  143. this.genIds_r(this.root);
  144. this.renderRoot(this.root);
  145. this.renderItems(this.root);
  146. return this.ctx.val;
  147. };
  148. pdf.outline.genIds_r = function(node) {
  149. node.id = pdf.internal.newObjectDeferred();
  150. for (var i = 0; i &lt; node.children.length; i++) {
  151. this.genIds_r(node.children[i]);
  152. }
  153. };
  154. pdf.outline.renderRoot = function(node) {
  155. this.objStart(node);
  156. this.line('/Type /Outlines');
  157. if (node.children.length > 0) {
  158. this.line('/First ' + this.makeRef(node.children[0]));
  159. this.line('/Last ' + this.makeRef(node.children[node.children.length - 1]));
  160. }
  161. this.line('/Count ' + this.count_r({
  162. count : 0
  163. }, node));
  164. this.objEnd();
  165. };
  166. pdf.outline.renderItems = function(node) {
  167. for (var i = 0; i &lt; node.children.length; i++) {
  168. var item = node.children[i];
  169. this.objStart(item);
  170. this.line('/Title ' + this.makeString(item.title));
  171. this.line('/Parent ' + this.makeRef(node));
  172. if (i > 0) {
  173. this.line('/Prev ' + this.makeRef(node.children[i - 1]));
  174. }
  175. if (i &lt; node.children.length - 1) {
  176. this.line('/Next ' + this.makeRef(node.children[i + 1]));
  177. }
  178. if (item.children.length > 0) {
  179. this.line('/First ' + this.makeRef(item.children[0]));
  180. this.line('/Last ' + this.makeRef(item.children[item.children.length - 1]));
  181. }
  182. var count = this.count = this.count_r({
  183. count : 0
  184. }, item);
  185. if (count > 0) {
  186. this.line('/Count ' + count);
  187. }
  188. if (item.options) {
  189. if (item.options.pageNumber) {
  190. // Explicit Destination
  191. //WARNING this assumes page ids are 3,5,7, etc.
  192. var info = pdf.internal.getPageInfo(item.options.pageNumber)
  193. this.line('/Dest ' + '[' + info.objId + ' 0 R /XYZ 0 ' + this.ctx.pdf.internal.pageSize.height + ' 0]');
  194. // this line does not work on all clients (pageNumber instead of page ref)
  195. //this.line('/Dest ' + '[' + (item.options.pageNumber - 1) + ' /XYZ 0 ' + this.ctx.pdf.internal.pageSize.height + ' 0]');
  196. // Named Destination
  197. // this.line('/Dest (page_' + (item.options.pageNumber) + ')');
  198. // Action Destination
  199. // var id = pdf.internal.newObject();
  200. // pdf.internal.write('&lt;&lt;/D[' + (item.options.pageNumber - 1) + ' /XYZ null null null]/S/GoTo>> endobj');
  201. // this.line('/A ' + id + ' 0 R' );
  202. }
  203. }
  204. this.objEnd();
  205. }
  206. for (var i = 0; i &lt; node.children.length; i++) {
  207. var item = node.children[i];
  208. this.renderItems(item);
  209. }
  210. };
  211. pdf.outline.line = function(text) {
  212. this.ctx.val += text + '\r\n';
  213. };
  214. pdf.outline.makeRef = function(node) {
  215. return node.id + ' 0 R';
  216. };
  217. pdf.outline.makeString = function(val) {
  218. return '(' + pdf.internal.pdfEscape(val) + ')';
  219. };
  220. pdf.outline.objStart = function(node) {
  221. this.ctx.val += '\r\n' + node.id + ' 0 obj' + '\r\n&lt;&lt;\r\n';
  222. };
  223. pdf.outline.objEnd = function(node) {
  224. this.ctx.val += '>> \r\n' + 'endobj' + '\r\n';
  225. };
  226. pdf.outline.count_r = function(ctx,node) {
  227. for (var i = 0; i &lt; node.children.length; i++) {
  228. ctx.count++;
  229. this.count_r(ctx, node.children[i]);
  230. }
  231. return ctx.count;
  232. };
  233. }
  234. ]);
  235. return this;
  236. })(jsPDF.API);
  237. </code></pre>
  238. </article>
  239. </section>
  240. </div>
  241. <br class="clear">
  242. <footer>
  243. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.2</a> on Sun Oct 09 2016 11:08:27 GMT+0100 (BST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
  244. </footer>
  245. <script>prettyPrint();</script>
  246. <script src="scripts/linenumber.js"></script>
  247. </body>
  248. </html>