|
| 1 | +/** |
| 2 | + * jsPDF Outline PlugIn |
| 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 | +/** |
| 10 | + * Generates a PDF Outline |
| 11 | + */ |
| 12 | +; |
| 13 | +(function(jsPDFAPI) { |
| 14 | + 'use strict'; |
| 15 | + |
| 16 | + jsPDFAPI.initOutlinePlugin = function() { |
| 17 | + |
| 18 | + this.outline = {}; |
| 19 | + this.outline.root = { |
| 20 | + children : [] |
| 21 | + }; |
| 22 | + |
| 23 | + this.internal.events.subscribe('postPutResources', function() { |
| 24 | + if (this.outline.root.children.length > 0) { |
| 25 | + var lines = this.outline.render().split(/\r\n/); |
| 26 | + for (var int = 0; int < lines.length; int++) { |
| 27 | + var line = lines[int]; |
| 28 | + if (line.endsWith('obj')){ |
| 29 | + var oid = line.split(' ')[0]; |
| 30 | + this.internal.newObjectDeferredBegin(oid); |
| 31 | + } |
| 32 | + this.internal.write(line); |
| 33 | + } |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + this.internal.events.subscribe('putCatalog', function() { |
| 38 | + if (this.outline.root.children.length > 0) { |
| 39 | + this.internal.write("/Outlines", this.outline.makeRef(this.outline.root)); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + /** |
| 44 | + * Options: pageNumber |
| 45 | + */ |
| 46 | + this.outline.add = function(parent,title,options) { |
| 47 | + var item = { |
| 48 | + title : title, |
| 49 | + options : options, |
| 50 | + children : [] |
| 51 | + }; |
| 52 | + if (parent == null) { |
| 53 | + parent = this.root; |
| 54 | + } |
| 55 | + parent.children.push(item); |
| 56 | + return item; |
| 57 | + } |
| 58 | + |
| 59 | + this.outline.render = function() { |
| 60 | + this.outline.ctx = {}; |
| 61 | + this.outline.nextId = 1000; |
| 62 | + this.outline.ctx.val = ''; |
| 63 | + this.outline.ctx.pdf = this; |
| 64 | + |
| 65 | + this.outline.genIds_r(this.outline.root); |
| 66 | + this.outline.renderRoot(this.outline.root); |
| 67 | + this.outline.renderItems(this.outline.root); |
| 68 | + |
| 69 | + return this.outline.ctx.val; |
| 70 | + }.bind(this); |
| 71 | + |
| 72 | + this.outline.genIds_r = function(node) { |
| 73 | + node.id = this.internal.newObjectDeferred(); |
| 74 | + for (var i = 0; i < node.children.length; i++) { |
| 75 | + this.outline.genIds_r(node.children[i]); |
| 76 | + } |
| 77 | + }.bind(this); |
| 78 | + |
| 79 | + this.outline.renderRoot = function(node) { |
| 80 | + this.objStart(node); |
| 81 | + this.line('/Type /Outlines'); |
| 82 | + if (node.children.length > 0) { |
| 83 | + this.line('/First ' + this.makeRef(node.children[0])); |
| 84 | + this.line('/Last ' + this.makeRef(node.children[node.children.length - 1])); |
| 85 | + } |
| 86 | + this.line('/Count ' + this.count_r({ |
| 87 | + count : 0 |
| 88 | + }, node)); |
| 89 | + this.objEnd(); |
| 90 | + } |
| 91 | + |
| 92 | + this.outline.renderItems = function(node) { |
| 93 | + for (var i = 0; i < node.children.length; i++) { |
| 94 | + var item = node.children[i]; |
| 95 | + this.objStart(item); |
| 96 | + |
| 97 | + this.line('/Title ' + this.makeString(item.title)); |
| 98 | + |
| 99 | + this.line('/Parent ' + this.makeRef(node)); |
| 100 | + if (i > 0) { |
| 101 | + this.line('/Prev ' + this.makeRef(node.children[i - 1])); |
| 102 | + } |
| 103 | + if (i < node.children.length - 1) { |
| 104 | + this.line('/Next ' + this.makeRef(node.children[i + 1])); |
| 105 | + } |
| 106 | + if (item.children.length > 0) { |
| 107 | + this.line('/First ' + this.makeRef(item.children[0])); |
| 108 | + this.line('/Last ' + this.makeRef(item.children[item.children.length - 1])); |
| 109 | + } |
| 110 | + |
| 111 | + var count = this.count = this.count_r({ |
| 112 | + count : 0 |
| 113 | + }, item); |
| 114 | + if (count > 0) { |
| 115 | + this.line('/Count ' + count); |
| 116 | + } |
| 117 | + |
| 118 | + if (item.options) { |
| 119 | + if (item.options.pageNumber) { |
| 120 | + this.line('/Dest ' + '[' + (item.options.pageNumber - 1) + ' /XYZ 0 ' + this.ctx.pdf.internal.pageSize.height + ' 0]'); |
| 121 | + //this.line('/Dest ' + '[' + (item.options.pageNumber - 1) + ' /Fit]'); |
| 122 | + } |
| 123 | + } |
| 124 | + this.objEnd(); |
| 125 | + } |
| 126 | + for (var i = 0; i < node.children.length; i++) { |
| 127 | + var item = node.children[i]; |
| 128 | + this.renderItems(item); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + this.outline.line = function(text) { |
| 133 | + this.ctx.val += text + '\r\n'; |
| 134 | + } |
| 135 | + |
| 136 | + this.outline.makeRef = function(node) { |
| 137 | + return node.id + ' 0 R'; |
| 138 | + } |
| 139 | + |
| 140 | + this.outline.makeString = function(val) { |
| 141 | + return '(' + this.internal.pdfEscape(val) + ')'; |
| 142 | + }.bind(this); |
| 143 | + |
| 144 | + this.outline.objStart = function(node) { |
| 145 | + this.ctx.val += '\r\n' + node.id + ' 0 obj' + '\r\n<<\r\n'; |
| 146 | + } |
| 147 | + |
| 148 | + this.outline.objEnd = function(node) { |
| 149 | + this.ctx.val += '>> \r\n' + 'endobj' + '\r\n'; |
| 150 | + } |
| 151 | + |
| 152 | + this.outline.count_r = function(ctx,node) { |
| 153 | + for (var i = 0; i < node.children.length; i++) { |
| 154 | + ctx.count++; |
| 155 | + this.count_r(ctx, node.children[i]); |
| 156 | + } |
| 157 | + return ctx.count; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + return this; |
| 162 | +})(jsPDF.API); |
0 commit comments