prawn-js 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ v0.1 (18th June 2009)
2
+ - initial release
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 James Healy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,73 @@
1
+ # prawn-js
2
+
3
+ ## Overview
4
+
5
+ This extension to the Prawn PDF generation library adds support for embedding javascript
6
+ in your files. These Javascript fragments will be compiled and executed on supported
7
+ viewers (like Adobe Acrobat Reader), usually in response to particular events (like the
8
+ document opening, closing, etc).
9
+
10
+ The Javascript syntax supported is fairly standard, although it includes a few extensions
11
+ defined by Adobe. For more information on these extensions (and the capabilities of
12
+ Javascript and PDF in general) visit Adobe's reference site, linked in the further reading
13
+ section of this readme.
14
+
15
+ ## Installation
16
+
17
+ gem install prawn-js
18
+
19
+ ## Usage
20
+
21
+ Prawn-JS depends on the core prawn library, and simply adds a few additional
22
+ methods to the standard Prawn::Document object that allow your Javascript to be
23
+ associated with certain events.
24
+
25
+ Start by requiring the prawn library, then the prawn-js library. Build your PDF as usual,
26
+ and use methods like add_docopen_js to register your code fragments.
27
+
28
+ require 'prawn'
29
+ require 'prawn/js'
30
+
31
+ Prawn::Document.generate "js_doc_open.pdf" do |pdf|
32
+ pdf.add_docopen_js("alert", "app.alert('open!',3)")
33
+ pdf.text "Javascript Prototype", :at => [150,720], :size => 32
34
+ end
35
+
36
+ For further examples and documentation, check out the examples/ directory of this project
37
+ and the code docs for the Prawn::JS module.
38
+
39
+ For general information on the syntax of JavaScript, I highly recommend
40
+ "JavaScript: The Good Parts" by Douglas Crockford, ISBN: 9780596517748.
41
+
42
+ For specific information on Adobe's extensions to JavaScript, in particular the
43
+ objects they provide for interacting with your document and the application
44
+ (somewhat similar to the DOM provided to Javascript in web browsers) read the
45
+ documents available on Adobe's JavaScript site (see links below). Look for
46
+ "JavaScript for Acrobat API Reference" and "Developing Acrobat Applications
47
+ Using JavaScript".
48
+
49
+ ## Disclaimer
50
+
51
+ I haven't yet found a situation where I've wanted to embed JS in my documents.
52
+ Most viewers ignore it and few users demand it.
53
+
54
+ This extension was more or less thrown together as a proof of concept. I'm
55
+ happy to review any patches that are submitted, but I not planning to actively
56
+ develop it. I'm releasing it because the code was getting lonely sitting on my
57
+ laptop. if there's one thing I hate it's lonely code.
58
+
59
+ ## Licensing
60
+
61
+ This library is distributed under the terms of the MIT License. See the included file for
62
+ more detail.
63
+
64
+ ## Contributing
65
+
66
+ All suggestions and patches welcome, preferably via a git repository I can pull from.
67
+
68
+ ## Further Reading
69
+
70
+ - The source: [http://github.com/yob/prawn-js/tree/master](http://github.com/yob/prawn-js/tree/master)
71
+ - Prawn source: [http://github.com/sandal/prawn/tree/master](http://github.com/sandal/prawn/tree/master)
72
+ - Rubyforge: [http://rubyforge.org/projects/prawn](http://rubyforge.org/projects/prawn)
73
+ - Adobe Javascript Site: [http://www.adobe.com/devnet/acrobat/javascript.html](http://www.adobe.com/devnet/acrobat/javascript.html)
@@ -0,0 +1,28 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require 'rubygems'
4
+ require 'prawn'
5
+ require 'prawn/js'
6
+
7
+ # A simple example that demonstrates having a PDF document automatically
8
+ # print itself when it's opened.
9
+ #
10
+ # For more detail on the options available when printing without
11
+ # user interaction, see chapter 5 of "Developing Acrobat Applications Using
12
+ # Javascript", available on the Adobe Javascript page.
13
+ #
14
+ Prawn::Document.generate "autoprint.pdf" do |pdf|
15
+ # Build the script we want to execute when the document opens
16
+ script = <<-EOF
17
+ var pp = this.getPrintParams();
18
+ pp.interactive = pp.constants.interactionLevel.silent;
19
+ this.print(pp);
20
+ EOF
21
+
22
+ # embed our script. The label is arbitary and can be anything
23
+ pdf.add_docopen_js("somelabel", script)
24
+
25
+ # finally add some real content to the page so something gets printed.
26
+ pdf.text "Javascript Prototype", :at => [150,720], :size => 32
27
+
28
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require 'rubygems'
4
+ require 'prawn'
5
+ require 'prawn/js'
6
+
7
+ Prawn::Document.generate "js_doc_close.pdf" do |pdf|
8
+
9
+ pdf.add_will_close_js("app.alert('closing',3)")
10
+
11
+ pdf.text "Javascript Prototype", :at => [150,720], :size => 32
12
+
13
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require 'rubygems'
4
+ require 'prawn'
5
+ require 'prawn/js'
6
+
7
+ Prawn::Document.generate "js_doc_open.pdf" do |pdf|
8
+
9
+ pdf.add_docopen_js("alert", "app.alert('open!',3)")
10
+
11
+ pdf.text "Javascript Prototype", :at => [150,720], :size => 32
12
+
13
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require 'rubygems'
4
+ require 'prawn'
5
+ require 'prawn/js'
6
+
7
+ Prawn::Document.generate "js_doc_save.pdf" do |pdf|
8
+
9
+ pdf.add_will_save_js("app.alert('saving',3)")
10
+
11
+ pdf.text "Javascript Prototype", :at => [150,720], :size => 32
12
+
13
+ end
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'prawn/core'
3
+
4
+ # This is how you can add some JS to your PDF files using only prawn-core.
5
+ # Kids, don't try this at home.
6
+
7
+ NAME_TREE_CHILDREN_LIMIT = 20
8
+
9
+ Prawn::Document.generate "autoprint.pdf" do |pdf|
10
+ # Build the script we want to execute when the document opens
11
+ script = <<-EOF
12
+ var pp = this.getPrintParams();
13
+ pp.interactive = pp.constants.interactionLevel.silent;
14
+ this.print(pp);
15
+ EOF
16
+
17
+ js_root = pdf.ref(Prawn::NameTree::Node.new(self, NAME_TREE_CHILDREN_LIMIT))
18
+ pdf.names.data[:JavaScript] ||= js_root
19
+
20
+ # embed our script. The label is arbitary and can be anything
21
+ obj = pdf.ref(:S => :JavaScript, :JS => script)
22
+
23
+ # reference our JS fragment from the root javascript object
24
+ js_root.data.add("some_random_label_that_can_be_anything", obj)
25
+
26
+ # finally add some real content to the page so something gets printed.
27
+ pdf.text "Javascript Prototype", :at => [150,720], :size => 32
28
+
29
+ end
@@ -0,0 +1,93 @@
1
+ # encoding: utf-8
2
+ #
3
+ # js.rb : Implements embeddable Javascript support for PDF
4
+ #
5
+ # Copyright March 2009, James Healy. All Rights Reserved.
6
+ #
7
+ # This is free software. Please see the LICENSE file for details.
8
+
9
+ gem "prawn", ">= 0.5"
10
+
11
+ module Prawn
12
+ module JS
13
+ # The maximum number of children to fit into a single node in the JavaScript tree.
14
+ NAME_TREE_CHILDREN_LIMIT = 20 #:nodoc:
15
+
16
+ # add a Javascript fragment that will execute after the document is saved.
17
+ #
18
+ # There can only be a single fragment. Calling this function multiple times
19
+ # will override earlier fragments.
20
+ #
21
+ def add_did_save_js(script)
22
+ aa[:DS] = ref(:S => :JavaScript, :JS => script)
23
+ end
24
+
25
+ # add a Javascript fragment that will execute after the document is printed.
26
+ #
27
+ # There can only be a single fragment. Calling this function multiple times
28
+ # will override earlier fragments.
29
+ #
30
+ def add_did_print_js(script)
31
+ aa[:DP] = ref(:S => :JavaScript, :JS => script)
32
+ end
33
+
34
+ # add a Javascript fragment that will execute when the document is opened.
35
+ #
36
+ # There can only be as many fragments as required. Calling this function
37
+ # multiple times will append the new fragment to the list.
38
+ #
39
+ def add_docopen_js(name, script)
40
+ obj = ref(:S => :JavaScript, :JS => script)
41
+ javascript.data.add(name, obj)
42
+ end
43
+
44
+ # add a Javascript fragment that will execute when the document is closed.
45
+ #
46
+ # There can only be a single fragment. Calling this function multiple times
47
+ # will override earlier fragments.
48
+ #
49
+ def add_will_close_js(script)
50
+ aa[:WC] = ref(:S => :JavaScript, :JS => script)
51
+ end
52
+
53
+ # add a Javascript fragment that will execute before the document is printed.
54
+ #
55
+ # There can only be a single fragment. Calling this function multiple times
56
+ # will override earlier fragments.
57
+ #
58
+ def add_will_print_js(script)
59
+ aa[:WP] = ref(:S => :JavaScript, :JS => script)
60
+ end
61
+
62
+ # add a Javascript fragment that will execute before the document is saved.
63
+ #
64
+ # There can only be a single fragment. Calling this function multiple times
65
+ # will override earlier fragments.
66
+ #
67
+ def add_will_save_js(script)
68
+ aa[:WS] = ref(:S => :JavaScript, :JS => script)
69
+ end
70
+
71
+ private
72
+
73
+ # create or access the Additional Actions dictionary in the document catalogue.
74
+ # See section 8.5.2 and table 8.47 of the PDF spec
75
+ #
76
+ def aa
77
+ # this entry is only valid in the document catalogue in PDF versions 1.4+.
78
+ min_version(1.4)
79
+ @root.data[:AA] ||= {}
80
+ end
81
+
82
+ # create or access the Javascript Name Tree in the document names dict.
83
+ # See section 3.6.3 and table 3.28 in the PDF spec.
84
+ #
85
+ def javascript
86
+ names.data[:JavaScript] ||= ref(Prawn::NameTree::Node.new(self, NAME_TREE_CHILDREN_LIMIT))
87
+ end
88
+
89
+ end
90
+ end
91
+
92
+ require 'prawn/document'
93
+ Prawn::Document.send(:include, Prawn::JS)
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prawn-js
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - James Healy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-18 00:00:00 +10:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: prawn-core
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0.5"
24
+ version:
25
+ description: A small extension to prawn that simplifies embedding JavaScript in your PDF files
26
+ email: pat@freelancing-gods.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - examples/doc_open.rb
35
+ - examples/doc_close.rb
36
+ - examples/doc_save.rb
37
+ - examples/autoprint.rb
38
+ - examples/manual.rb
39
+ - lib/prawn/js.rb
40
+ - README.markdown
41
+ - MIT-LICENSE
42
+ - CHANGELOG
43
+ has_rdoc: true
44
+ homepage: http://ts.freelancing-gods.com
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --title
50
+ - PrawnJS
51
+ - --line-numbers
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: prawn
69
+ rubygems_version: 1.3.4
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: A small extension to prawn that makes it possible to embed JavaScript fragment in your document that respond to events.
73
+ test_files: []
74
+