itszero-InvoicePrinter 0.1.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/README.rdoc ADDED
@@ -0,0 +1,32 @@
1
+ = InvoicePrinter::U420
2
+ Ruby interface to Invoice Printer (Epson U420)
3
+
4
+ == Credits
5
+
6
+ The API interface idea comes from clkao's Business-TW-Invoice-U420[http://search.cpan.org/dist/Business-TW-Invoice-U420/] perl module.
7
+
8
+ Author:: Chien-An "Zero" Cho (itsZero)
9
+ Email:: itszero at gmail dot com
10
+ Copyright:: Copyright (c) 2009 Chien-An "Zero" Cho
11
+ License:: MIT License
12
+
13
+
14
+ == License
15
+
16
+ Permission is hereby granted, free of charge, to any person obtaining a copy
17
+ of this software and associated documentation files (the "Software"), to deal
18
+ in the Software without restriction, including without limitation the rights
19
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20
+ copies of the Software, and to permit persons to whom the Software is
21
+ furnished to do so, subject to the following conditions:
22
+
23
+ The above copyright notice and this permission notice shall be included in
24
+ all copies or substantial portions of the Software.
25
+
26
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32
+ THE SOFTWARE.
@@ -0,0 +1 @@
1
+ require 'invoice_printer/U420'
@@ -0,0 +1,159 @@
1
+ #!/usr/bin/ruby
2
+ # InvoicePrinter::U420
3
+ # Ruby interface to Invoice Printer (Epson U420)
4
+ #
5
+ # = Credits
6
+ #
7
+ # The API interface idea comes from clkao's Business-TW-Invoice-U420[http://search.cpan.org/dist/Business-TW-Invoice-U420/] perl module.
8
+ #
9
+ # Author:: Chien-An "Zero" Cho (itsZero)
10
+ # Email:: itszero at gmail dot com
11
+ # Copyright:: Copyright (c) 2009 Chien-An "Zero" Cho
12
+ # License:: MIT License
13
+ #
14
+ #
15
+ # = License
16
+ #
17
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
18
+ # of this software and associated documentation files (the "Software"), to deal
19
+ # in the Software without restriction, including without limitation the rights
20
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21
+ # copies of the Software, and to permit persons to whom the Software is
22
+ # furnished to do so, subject to the following conditions:
23
+ #
24
+ # The above copyright notice and this permission notice shall be included in
25
+ # all copies or substantial portions of the Software.
26
+ #
27
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33
+ # THE SOFTWARE.
34
+ require 'iconv'
35
+
36
+ # InvoicePrinter acts as a namespace of the InvoicePrinter gem.
37
+ # To use this gem, instantiate any class under this namespace.
38
+ module InvoicePrinter
39
+
40
+ # This class provides the interface to communicate with Epson U420-series
41
+ # invoice printer. The output is defaulted to STDOUT, which is your display.
42
+ # To actually print out the content, please use SerialPort[http://github.com/toholio/ruby-serialport] gem to
43
+ # get an IO object of the Serial Port, then assigned it to the output parameter when initiailizing this class.
44
+ #
45
+ # = Example
46
+ #
47
+ # require 'rubygems'
48
+ # require 'invoice_printer'
49
+ # require 'serialport'
50
+ #
51
+ # serial_out = SerialPort.new("/dev/tty.usbserial",
52
+ # :baud_rate => 9600,
53
+ # :databits => 8,
54
+ # :parity => SerialPort::NONE,
55
+ # :stopbits => 1
56
+ # )
57
+ #
58
+ # prt = InvoicePrinter::U420.new(
59
+ # :header => "Hello, World",
60
+ # :lines_total => 35,
61
+ # :lines_available => 18,
62
+ # :lines_stamp => 5,
63
+ # :output => serial_out
64
+ # )
65
+ #
66
+ # prt.println "Hello, World"
67
+ # prt.feed 2
68
+ # prt.println "Yeah"
69
+ # prt.cut # This is actually cut the paper.
70
+ class U420
71
+ # Current output stream, which receives the command generated by this library.
72
+ attr_accessor :output
73
+ # Options which controls the behavior of this library.
74
+ # Although we're using option[:output] to give output stream when initializing,
75
+ # please notice that changing options[:output] by accessor is not working.
76
+ # To do so, use output accessor instead.
77
+ attr_accessor :options
78
+
79
+ DEFAULT_OPTIONS =
80
+ {
81
+ :header => "",
82
+ :lines_total => 35,
83
+ :lines_available => 18,
84
+ :lines_stamp => 5,
85
+ :output => STDOUT,
86
+ :encoding => "big5//ignore"
87
+ }
88
+
89
+ ESC = 0x1b.chr
90
+
91
+ # Initialize a new instance of U420 printer API interface.
92
+ # The options contains some parameters you can use to customize the behavior of the printer.
93
+ # The default value should be very useful if you're going to print invoices in Taiwan.
94
+ #
95
+ # Supported options:
96
+ #
97
+ # * header - A string contains the header of each invoice, can contains multiple line. (Default: "")
98
+ # * lines_total - How many lines a page of invoice should contains. (Default: 35)
99
+ # * lines_available - How many lines should a page of invoice can print on. (Default: 18)
100
+ # * lines_stamp - The height of the stamp (before the header) of the invoice. (Default: 5)
101
+ # * output - An IO object which will receive the command of the printer. (Default: STDOUT)
102
+ # * encoding - The encoding of non-ascii characters the printer supported. (Default: Big5)
103
+ # * dont_init_printer - Don't send [ESC]@ to printer before the data. (Default: false)
104
+ def initialize(options)
105
+ @options = DEFAULT_OPTIONS.merge(options)
106
+ @output = @options[:output]
107
+ @lines_used = 0
108
+
109
+ self.init_printer unless options[:dont_init_printer]
110
+ end
111
+
112
+ # Output the [ESC]@ to the printer, initialize the printer.
113
+ def init_printer
114
+ @output.write("#{ESC}@")
115
+ end
116
+
117
+ # Skip some lines, default is 1.
118
+ def feed(lines = 1)
119
+ @lines_used += lines
120
+ @output.write("#{ESC}d#{lines.chr}")
121
+ end
122
+
123
+ # Use this command to utilize the stamping mechanism on the printer.
124
+ def stamp
125
+ @output.write("#{ESC}o")
126
+ end
127
+
128
+ # Roll the entire page out and do the cut, it then stamp on the next page, too.
129
+ def cut
130
+ return unless @lines_used > 0
131
+
132
+ feed(@options[:lines_total] - @lines_used + 1)
133
+ stamp
134
+ print(0x0c.chr)
135
+ @lines_used = 0
136
+ end
137
+
138
+ # Print out string on the printer, the non-ascii code is automatically converted using Iconv.
139
+ def print(str)
140
+ @output.write(Iconv.iconv(@options[:encoding], "utf-8//ignore", str))
141
+ end
142
+
143
+ # Print out string and feed to next line.
144
+ # We also do the paging logic here.
145
+ def println(str)
146
+ unless @lines_used > 0
147
+ feed(@options[:lines_stamp])
148
+ println(@options[:header])
149
+ end
150
+
151
+ str.split("\n").each do |line|
152
+ print("#{line}\n")
153
+
154
+ @lines_used += 1
155
+ cut if (@lines_used == @options[:lines_available] + @options[:lines_stamp])
156
+ end
157
+ end
158
+ end
159
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: itszero-InvoicePrinter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Chien-An Zero Cho (itsZero)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: The Ruby interface to the invoice printers. Currenetly works with EPSON U420. The default options is optimized for Taiwan Unified Invoices.
17
+ email: itszero@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - lib/invoice_printer.rb
26
+ - lib/invoice_printer/U420.rb
27
+ - README.rdoc
28
+ has_rdoc: true
29
+ homepage: http://github.com/itszero/InvoicePrinter
30
+ licenses:
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.3.5
52
+ signing_key:
53
+ specification_version: 2
54
+ summary: A ruby interface to communicate with the invoice printers.
55
+ test_files: []
56
+