MrRio/jsPDF — pull request #15 by dvdotsenko (2012-06-16 23:08:33+00:00)
Title: Plugins API - start of conversation. Do not merge yet.

Hi.

I was contemplating ways of adding (potentially large) plugins to jsPDF. We need SVG images to PDF renderer for our business (we take signatures from willowsystems.github.com/jSignature/ and need to put them into PDF) so I started with basic SVG image import / renderer plugin.

You can ignore the actual plug in code, as it's very specific (basic) to the SVGs jSignature produces and is still work in progress. However, the API hooks I attached to jsPDF are of public interest.

Please, glance at 173747137ac0020d5ec6984c27bde373a2e9138f - "Adding hooks for plugins to attach to jsPDF instantiator" then see how it's used in the SillySVGRenderer plug in.

Please, chime in with your thoughts on this. I tried to make it feel and behave exactly like jQuery plugins work, where you extend $.fn and they show up as methods on derivative objects. Similarly, jsPDF plugins proposal allows plugins to extend global jsPDF.API and the plugin methods show up among the built-in methods on derivative (new) jsPDF objects.

// load jsPDF script
jsPDF.API.mymethod = function(){
// 'this' will be ref to _jsPDF object. see jsPDF source
// , so you can refer to internal methods like so:
// this.line(....)
// this.text(....)
}

var pdfdoc = new jsPDF()

pdfdoc.text(.......)
pdfdoc.mymethod() // <- !!!!!!
pdfdoc.output()

I really hope to hear from others before I commit to a particular API hook. Thoughts?