prawn-print 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in prawn-print.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard 'rspec', :version => 2, :cli => "--color" do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
data/README.markdown ADDED
@@ -0,0 +1,73 @@
1
+ # Prawn Print
2
+
3
+ Ruby gem that adds `print` and `autoprint` methods for Adobe Reader to generated [Prawn](http://prawn.majesticseacreature.com) PDF documents.
4
+
5
+ The documents can be opened in any PDF reader, but only Adobe Reader supports triggering printing in this way.
6
+
7
+
8
+ ## Installation
9
+
10
+ With Bundler for e.g. Ruby on Rails, add this to your `Gemfile`:
11
+
12
+ gem 'prawn-print', :git => 'git://github.com/barsoom/prawn-print.git'
13
+
14
+ and run
15
+
16
+ bundle
17
+
18
+ to install it.
19
+
20
+
21
+ ## Usage
22
+
23
+ # Open print dialog, but don't autoprint.
24
+ pdf = Prawn::Document.new
25
+ pdf.text "I installed Adobe Reader and all I got was this lousy printout."
26
+ pdf.print
27
+
28
+ # Autoprint it on the default printer, when opened.
29
+ pdf = Prawn::Document.new
30
+ pdf.text "Help! I am trapped in a PDF factory!"
31
+ pdf.autoprint
32
+
33
+ # Autoprint it on a printer where the name includes "LaserJet".
34
+ pdf = Prawn::Document.new
35
+ pdf.text "Help! I am trapped in a PDF factory!"
36
+ pdf.autoprint "LaserJet"
37
+
38
+ You can call `print`/`autoprint` at any place of the generated PDF – it doesn't have to be at the end.
39
+
40
+
41
+ # Caveats
42
+
43
+ Sadly, autoprinting isn't fully hands-off – for security reasons, Reader will show a confirmation dialog.
44
+
45
+
46
+ ## Credits and license
47
+
48
+ Based on [prawn-js](https://github.com/threadhead/prawn-js) by James Healy, forked and fixed by Karl Smith,
49
+ and on JavaScript samples found somewhere on the web at the dawn of time.
50
+
51
+ Glued together by [Henrik Nyh](http://henrik.nyh.se/) for [Barsoom](http://barsoom.se) under the MIT license:
52
+
53
+ > Copyright (c) 2011 Barsoom AB
54
+ >
55
+ > Permission is hereby granted, free of charge, to any person obtaining a copy
56
+ > of this software and associated documentation files (the "Software"), to deal
57
+ > in the Software without restriction, including without limitation the rights
58
+ > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
59
+ > copies of the Software, and to permit persons to whom the Software is
60
+ > furnished to do so, subject to the following conditions:
61
+ >
62
+ > The above copyright notice and this permission notice shall be included in
63
+ > all copies or substantial portions of the Software.
64
+ >
65
+ > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
66
+ > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
67
+ > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
68
+ > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
69
+ > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
70
+ > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
71
+ > THE SOFTWARE.
72
+
73
+ #
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,13 @@
1
+ require "rubygems"
2
+ $:.push File.join(File.dirname(__FILE__), "../lib")
3
+ require "prawn-print"
4
+
5
+ FILENAME = "/tmp/prawn_autoprint.pdf"
6
+
7
+ pdf = Prawn::Document.new
8
+ pdf.text "Help! I am trapped in a PDF factory!"
9
+ pdf.autoprint
10
+ pdf.render_file FILENAME
11
+
12
+ `open -a "Adobe Reader" #{FILENAME}` if system("which open")
13
+ puts "Done."
@@ -0,0 +1,13 @@
1
+ require "rubygems"
2
+ $:.push File.join(File.dirname(__FILE__), "../lib")
3
+ require "prawn-print"
4
+
5
+ FILENAME = "/tmp/prawn_autoprint_named.pdf"
6
+
7
+ pdf = Prawn::Document.new
8
+ pdf.text "Help! I am trapped in a PDF factory!"
9
+ pdf.autoprint "LaserJet"
10
+ pdf.render_file FILENAME
11
+
12
+ `open -a "Adobe Reader" #{FILENAME}` if system("which open")
13
+ puts "Done."
data/examples/print.rb ADDED
@@ -0,0 +1,13 @@
1
+ require "rubygems"
2
+ $:.push File.join(File.dirname(__FILE__), "../lib")
3
+ require "prawn-print"
4
+
5
+ FILENAME = "/tmp/prawn_print.pdf"
6
+
7
+ pdf = Prawn::Document.new
8
+ pdf.text "I installed Adobe Reader and all I got was this lousy printout."
9
+ pdf.print
10
+ pdf.render_file FILENAME
11
+
12
+ `open -a "Adobe Reader" #{FILENAME}` if system("which open")
13
+ puts "Done."
@@ -0,0 +1,32 @@
1
+ # From https://github.com/threadhead/prawn-js/
2
+ # by James Healy and Karl Smith under the MIT license.
3
+
4
+ module Prawn
5
+ module Print
6
+ module JS
7
+
8
+ # The maximum number of children to fit into a single node in the JavaScript tree.
9
+ NAME_TREE_CHILDREN_LIMIT = 20 #:nodoc:
10
+
11
+ # add a Javascript fragment that will execute when the document is opened.
12
+ #
13
+ # There can only be as many fragments as required. Calling this function
14
+ # multiple times will append the new fragment to the list.
15
+ #
16
+ def add_docopen_js(name, script)
17
+ obj = ref!(:S => :JavaScript, :JS => script)
18
+ javascript.data.add(name, obj)
19
+ end
20
+
21
+ private
22
+
23
+ # create or access the Javascript Name Tree in the document names dict.
24
+ # See section 3.6.3 and table 3.28 in the PDF spec.
25
+ #
26
+ def javascript
27
+ names.data[:JavaScript] ||= ref!(Prawn::Core::NameTree::Node.new(self, NAME_TREE_CHILDREN_LIMIT))
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ module Prawn
2
+ module Print
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,61 @@
1
+ require "prawn"
2
+ require "prawn-print/version"
3
+ require "prawn-print/js"
4
+
5
+ module Prawn
6
+ module Print
7
+ include JS
8
+
9
+ def print
10
+ print_with_auto_to_printer(false, nil)
11
+ end
12
+
13
+ def autoprint(printer=nil)
14
+ print_with_auto_to_printer(true, printer)
15
+ end
16
+
17
+ private
18
+
19
+ def print_with_auto_to_printer(auto, printer)
20
+ add_docopen_js("print", x = <<-JS)
21
+
22
+ var pp = this.getPrintParams();
23
+ #{interactive_js(auto)}
24
+ #{select_printer_js(printer)}
25
+ this.print(pp);
26
+
27
+ JS
28
+ end
29
+
30
+ def interactive_js(auto)
31
+ if auto
32
+ <<-JS
33
+ pp.interactive = pp.constants.interactionLevel.silent;
34
+ JS
35
+ end
36
+ end
37
+
38
+ def select_printer_js(printer)
39
+ if printer
40
+ escaped_printer = printer.gsub('"') { "\\#$0" }
41
+ <<-JS
42
+ var names = app.printerNames;
43
+ var regex = new RegExp("#{escaped_printer}", 'i');
44
+ for (var i = 0; i < names.length; i++) {
45
+ if (names[i].match(regex)) {
46
+ pp.printerName = names[i];
47
+ break;
48
+ }
49
+ }
50
+ JS
51
+ else
52
+ <<-JS
53
+ pp.printerName = ""; // Default.
54
+ JS
55
+ end
56
+ end
57
+
58
+ end
59
+ end
60
+
61
+ Prawn::Document.send(:include, Prawn::Print)
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "prawn-print/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "prawn-print"
7
+ s.version = Prawn::Print::VERSION
8
+ s.authors = ["Henrik Nyh"]
9
+ s.email = ["henrik@barsoom.se"]
10
+ s.homepage = "https://github.com/barsoom/prawn-print"
11
+ s.summary = %q{Print/autoprint Prawn-generated PDFs from Adobe Reader.}
12
+
13
+ s.rubyforge_project = "prawn-print"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency "prawn", "~> 0.12.0"
21
+
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency "guard"
24
+ s.add_development_dependency "guard-rspec"
25
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+ require "prawn-print"
4
+
5
+ # These could use more work...
6
+
7
+ describe Prawn::Print, "#print" do
8
+ it "should work without arguments" do
9
+ pdf = Prawn::Document.new
10
+ lambda { pdf.print }.should_not raise_error
11
+ end
12
+ end
13
+
14
+ describe Prawn::Print, "#autoprint" do
15
+ it "should work without arguments" do
16
+ pdf = Prawn::Document.new
17
+ lambda { pdf.autoprint }.should_not raise_error
18
+ end
19
+
20
+ it "should work with an argument" do
21
+ pdf = Prawn::Document.new
22
+ lambda { pdf.autoprint("LaserJet") }.should_not raise_error
23
+ end
24
+ end
File without changes
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prawn-print
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Henrik Nyh
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-10-21 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: prawn
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 47
30
+ segments:
31
+ - 0
32
+ - 12
33
+ - 0
34
+ version: 0.12.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: guard
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :development
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: guard-rspec
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ type: :development
78
+ version_requirements: *id004
79
+ description:
80
+ email:
81
+ - henrik@barsoom.se
82
+ executables: []
83
+
84
+ extensions: []
85
+
86
+ extra_rdoc_files: []
87
+
88
+ files:
89
+ - .gitignore
90
+ - Gemfile
91
+ - Guardfile
92
+ - README.markdown
93
+ - Rakefile
94
+ - examples/autoprint.rb
95
+ - examples/autoprint_named.rb
96
+ - examples/print.rb
97
+ - lib/prawn-print.rb
98
+ - lib/prawn-print/js.rb
99
+ - lib/prawn-print/version.rb
100
+ - prawn-print.gemspec
101
+ - spec/prawn-print_spec.rb
102
+ - spec/spec_helper.rb
103
+ has_rdoc: true
104
+ homepage: https://github.com/barsoom/prawn-print
105
+ licenses: []
106
+
107
+ post_install_message:
108
+ rdoc_options: []
109
+
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 3
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: 3
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ requirements: []
131
+
132
+ rubyforge_project: prawn-print
133
+ rubygems_version: 1.5.3
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: Print/autoprint Prawn-generated PDFs from Adobe Reader.
137
+ test_files:
138
+ - spec/prawn-print_spec.rb
139
+ - spec/spec_helper.rb