-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Initial support for annotations (links, etc.) #390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
94c5b74
Initial support for annotations (links, etc.)
Flamenco 970f0fd
removed hardcoded url
Flamenco a6d7af4
Initial support for outlines (table of contents)
Flamenco a315a4d
Added a plugin manager.
Flamenco b403daf
removed duplicate comment
Flamenco 9d70421
removed extra call to render
Flamenco 19251b0
Fix to make destinations more compatable on certain clients.
Flamenco f3aa1a5
hack: adjust annotation area for text baseline.
Flamenco 0675144
Merge remote-tracking branch 'mr_rio/master' into master2
Flamenco 981d249
removed plugin manager
Flamenco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/** | ||
* jsPDF Annotations PlugIn | ||
* Copyright (c) 2014 Steven Spungin (TwelveTone LLC) steven@twelvetone.tv | ||
* | ||
* Licensed under the MIT License. | ||
* http://opensource.org/licenses/mit-license | ||
*/ | ||
|
||
/** | ||
* There are many types of annotations in a PDF document. Annotations are placed | ||
* on a page at a particular location. They are not 'attached' to an object. | ||
* <br /> | ||
* This plugin current supports <br /> | ||
* <li> Goto Page (set pageNumber in options) | ||
* <li> Goto URL (set url in options) | ||
* | ||
* <p> | ||
* Options In PDF spec Not Implemented Yet | ||
* <li> link border | ||
* <li> named target | ||
* <li> page coordinates | ||
* <li> destination page scaling and layout | ||
* <li> actions other than URL and GotoPage | ||
* <li> background / hover actions | ||
* </p> | ||
*/ | ||
|
||
function notEmpty(obj) { | ||
if (typeof obj != 'undefined') { | ||
if (obj != '') { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
(function(jsPDFAPI) { | ||
'use strict'; | ||
|
||
var annotationPlugin = { | ||
|
||
/** | ||
* An array of arrays, indexed by <em>pageNumber</em>. | ||
*/ | ||
annotations : [], | ||
|
||
f2 : function(number) { | ||
return number.toFixed(2); | ||
} | ||
}; | ||
|
||
jsPDF.API.annotationPlugin = annotationPlugin; | ||
|
||
jsPDF.API.events.push([ | ||
'addPage', function(info) { | ||
this.annotationPlugin.annotations[info.pageNumber] = []; | ||
} | ||
]); | ||
|
||
jsPDFAPI.events.push([ | ||
'putPage', function(info) { | ||
var pageAnnos = this.annotationPlugin.annotations[info.pageNumber]; | ||
|
||
var found = false; | ||
for (var a = 0; a < pageAnnos.length; a++) { | ||
var anno = pageAnnos[a]; | ||
if (anno.type === 'link') { | ||
if (notEmpty(anno.options.url) || notEmpty(anno.options.pageNumber)) { | ||
found = true; | ||
break; | ||
} | ||
} | ||
} | ||
if (found == false) { | ||
return; | ||
} | ||
|
||
this.internal.write("/Annots ["); | ||
var f2 = this.annotationPlugin.f2; | ||
for (var a = 0; a < pageAnnos.length; a++) { | ||
var anno = pageAnnos[a]; | ||
var k = this.internal.scaleFactor; | ||
var pageHeight = this.internal.pageSize.height; | ||
var rect = "/Rect [" + f2(anno.x * k) + " " + f2((pageHeight - anno.y) * k) + " " + f2(anno.x + anno.w * k) + " " + f2(pageHeight - (anno.y + anno.h) * k) + "] "; | ||
if (anno.options.url) { | ||
this.internal.write('<</Type /Annot /Subtype /Link ' + rect + '/Border [0 0 0] /A <</S /URI /URI (' + anno.options.url + ') >> >>') | ||
} else if (anno.options.pageNumber) { | ||
// first page is 0 | ||
this.internal.write('<</Type /Annot /Subtype /Link ' + rect + '/Border [0 0 0] /Dest [' + (anno.options.pageNumber - 1) + ' /XYZ 0 ' + pageHeight + ' 0] >>') | ||
} else { | ||
// TODO error - should not be here | ||
} | ||
} | ||
this.internal.write("]"); | ||
} | ||
]); | ||
|
||
/** | ||
* valid options | ||
* <li> pageNumber or url [required] | ||
*/ | ||
jsPDFAPI.link = function(x,y,w,h,options) { | ||
'use strict'; | ||
this.annotationPlugin.annotations[this.internal.getNumberOfPages()].push({ | ||
x : x, | ||
y : y, | ||
w : w, | ||
h : h, | ||
options : options, | ||
type : 'link' | ||
}); | ||
}; | ||
|
||
/** | ||
* Currently only supports single line text. | ||
*/ | ||
jsPDFAPI.textWithLink = function(text,x,y,options) { | ||
'use strict'; | ||
var width = this.getTextWidth(text); | ||
var height = this.internal.getLineHeight(); | ||
this.text(text, x, y); | ||
//TODO We really need the text baseline height to do this correctly. | ||
// Or ability to draw text on top, bottom, center, or baseline. | ||
y += height * .2; | ||
this.link(x, y - height, width, height, options); | ||
return this; | ||
}; | ||
|
||
//TODO move into external library | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than into an external library, feel free to add them both to the main/internal jsPDF API. |
||
jsPDFAPI.getTextWidth = function(text) { | ||
'use strict'; | ||
var fontSize = this.internal.getFontSize(); | ||
var txtWidth = this.getStringUnitWidth(text) * fontSize / this.internal.scaleFactor; | ||
return txtWidth; | ||
}; | ||
|
||
//TODO move into external library | ||
jsPDFAPI.getLineHeight = function() { | ||
return this.internal.getLineHeight(); | ||
}; | ||
|
||
return this; | ||
|
||
})(jsPDF.API); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should make this function private to your plugin scope.