escpos 0.0.3
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.
- checksums.yaml +7 -0
- data/.gitignore +69 -0
- data/Gemfile +4 -0
- data/README.md +87 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/config +13 -0
- data/escpos.gemspec +27 -0
- data/examples/report.erb +3 -0
- data/examples/report.rb +15 -0
- data/lib/escpos.rb +70 -0
- data/lib/escpos/helpers.rb +132 -0
- data/lib/escpos/printer.rb +34 -0
- data/lib/escpos/report.rb +22 -0
- data/lib/escpos/version.rb +3 -0
- data/test/fixtures/report.erb +7 -0
- data/test/fixtures/styles.erb +29 -0
- data/test/lib/escpos/printer_test.rb +17 -0
- data/test/lib/escpos/report_test.rb +25 -0
- data/test/test_helper.rb +7 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 07e5de3027f521cd1b3ed0be7c34be9bc54fc9d5
|
4
|
+
data.tar.gz: 0b48cb01f97e2e1bc77e4d8deb88711f3d163322
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 76c976c0f10b0b418d57971bbcba86850d01622efb0ef70585dd0ac393d14dbbab0b1cad7b0ec8c96acb6e1bf7456c853ed438251ca87863427c231990f87f73
|
7
|
+
data.tar.gz: 7aa7b8cf020f4d2889b7f80fbceb9446dd0a5108dfa5b72615fb03f38b6a2c2ae1c2c14b4c4662c48d0198c5d6ee3de2cbe6abd88c4c48ea2c4b117e340b9bcf
|
data/.gitignore
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# Compiled source #
|
2
|
+
###################
|
3
|
+
*.com
|
4
|
+
*.class
|
5
|
+
*.dll
|
6
|
+
*.exe
|
7
|
+
*.o
|
8
|
+
*.so
|
9
|
+
|
10
|
+
# Packages #
|
11
|
+
############
|
12
|
+
# it's better to unpack these files and commit the raw source
|
13
|
+
# git has its own built in compression methods
|
14
|
+
*.7z
|
15
|
+
*.dmg
|
16
|
+
*.gz
|
17
|
+
*.iso
|
18
|
+
*.jar
|
19
|
+
*.rar
|
20
|
+
*.tar
|
21
|
+
*.zip
|
22
|
+
|
23
|
+
# Logs and databases #
|
24
|
+
######################
|
25
|
+
*.log
|
26
|
+
*.sql
|
27
|
+
*.sqlite
|
28
|
+
*.sqlite3
|
29
|
+
|
30
|
+
# OS generated files #
|
31
|
+
######################
|
32
|
+
.DS_Store
|
33
|
+
.DS_Store?
|
34
|
+
._*
|
35
|
+
.Spotlight-V100
|
36
|
+
.Trashes
|
37
|
+
ehthumbs.db
|
38
|
+
Thumbs.db
|
39
|
+
|
40
|
+
# Rails
|
41
|
+
*.rbc
|
42
|
+
*.sassc
|
43
|
+
.sass-cache
|
44
|
+
capybara-*.html
|
45
|
+
.rspec
|
46
|
+
/log
|
47
|
+
/tmp
|
48
|
+
/db/*.sqlite3
|
49
|
+
/public/system
|
50
|
+
/coverage/
|
51
|
+
/spec/tmp
|
52
|
+
**.orig
|
53
|
+
rerun.txt
|
54
|
+
pickle-email-*.html
|
55
|
+
config/initializers/secret_token.rb
|
56
|
+
config/secrets.yml
|
57
|
+
|
58
|
+
## Environment normalisation:
|
59
|
+
/.bundle
|
60
|
+
/vendor/bundle
|
61
|
+
|
62
|
+
# these should all be checked in to normalise the environment:
|
63
|
+
# Gemfile.lock, .ruby-version, .ruby-gemset
|
64
|
+
|
65
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
66
|
+
.rvmrc
|
67
|
+
|
68
|
+
pkg/*
|
69
|
+
Gemfile.lock
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# Escpos
|
2
|
+
|
3
|
+
A ruby implementation of ESC/POS (thermal) printer command specification.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'escpos'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install escpos
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
@printer = Escpos::Printer.new
|
25
|
+
@printer.write "Some text"
|
26
|
+
|
27
|
+
@printer.to_escpos # returns ESC/POS data ready to be sent to printer
|
28
|
+
# on linux this can be piped directly to /dev/usb/lp0
|
29
|
+
# with network printer sent directly to printer socket
|
30
|
+
# with serial port printer it can be sent directly to the serial port
|
31
|
+
|
32
|
+
@printer.to_base64 # returns base64 encoded ESC/POS data
|
33
|
+
|
34
|
+
# using report class
|
35
|
+
|
36
|
+
# my_report.rb:
|
37
|
+
class MyReport < Escpos::Report
|
38
|
+
def item(text)
|
39
|
+
@count ||= 0
|
40
|
+
@count += 1
|
41
|
+
quad_text "#{@count}. #{text}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
```erb
|
48
|
+
<% # my_report.erb: %>
|
49
|
+
<%= item "First item" %>
|
50
|
+
<%= item "Second item" %>
|
51
|
+
<%= item "Third item" %>
|
52
|
+
```
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
# usage:
|
56
|
+
|
57
|
+
report = MyReport.new 'path/to/my_report.erb'
|
58
|
+
@printer.write report.render
|
59
|
+
@printer.cut!
|
60
|
+
# @printer.to_escpos or @printer.to_base64 contains resulting ESC/POS data
|
61
|
+
```
|
62
|
+
|
63
|
+
## Available helpers
|
64
|
+
|
65
|
+
- text: Normal text formatting
|
66
|
+
- double_height: Double height text
|
67
|
+
- quad_text, big, title header, double_width_double_height, double_height_double_width: Double width & Double height text
|
68
|
+
- double_width: Double iwdth text
|
69
|
+
- underline, u: Underlined text
|
70
|
+
- underline2, u2: Stronger underlined text
|
71
|
+
- bold, b: Bold text
|
72
|
+
- left: Align to left
|
73
|
+
- right: Align to right
|
74
|
+
- center: Align to center
|
75
|
+
- barcode: Print barcode
|
76
|
+
- partial_cut: Partially cut the paper (may not be available on all devices)
|
77
|
+
- cut: Fully cut the paper (may not be available on all devices)
|
78
|
+
|
79
|
+
## Contributing
|
80
|
+
|
81
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/escpos/escpos.
|
82
|
+
|
83
|
+
1. Fork it ( https://github.com/escpos/escpos/fork )
|
84
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
85
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
86
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
87
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "escpos"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/config
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
[core]
|
2
|
+
repositoryformatversion = 0
|
3
|
+
filemode = true
|
4
|
+
bare = false
|
5
|
+
logallrefupdates = true
|
6
|
+
ignorecase = true
|
7
|
+
precomposeunicode = true
|
8
|
+
[remote "origin"]
|
9
|
+
url = https://github.com/escpos/escpos.git
|
10
|
+
fetch = +refs/heads/*:refs/remotes/origin/*
|
11
|
+
[branch "master"]
|
12
|
+
remote = origin
|
13
|
+
merge = refs/heads/master
|
data/escpos.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'escpos/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "escpos"
|
8
|
+
spec.version = Escpos::VERSION
|
9
|
+
spec.authors = ["Jan Svoboda"]
|
10
|
+
spec.email = ["jan@mluv.cz"]
|
11
|
+
spec.summary = %q{A ruby implementation of ESC/POS (thermal) printer command specification.}
|
12
|
+
spec.description = %q{A ruby implementation of ESC/POS (thermal) printer command specification.}
|
13
|
+
spec.homepage = "https://github.com/escpos/escpos"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.required_ruby_version = ">= 1.9"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.4.2"
|
27
|
+
end
|
data/examples/report.erb
ADDED
data/examples/report.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'escpos'
|
2
|
+
|
3
|
+
class MyReport < Escpos::Report
|
4
|
+
def item(text)
|
5
|
+
@count ||= 0
|
6
|
+
@count += 1
|
7
|
+
quad_text "#{@count}. #{text}"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
report = MyReport.new File.join(__dir__, 'report.erb')
|
12
|
+
@printer.write report.render
|
13
|
+
@printer.cut!
|
14
|
+
|
15
|
+
# @printer.to_escpos or @printer.to_base64 contains resulting ESC/POS data
|
data/lib/escpos.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require "escpos/version"
|
2
|
+
require "escpos/printer"
|
3
|
+
require "escpos/helpers"
|
4
|
+
require "escpos/report"
|
5
|
+
|
6
|
+
module Escpos
|
7
|
+
|
8
|
+
# Printer hardware
|
9
|
+
HW_INIT = [ 0x1b, 0x40 ] # Clear data in buffer and reset modes
|
10
|
+
HW_SELECT = [ 0x1b, 0x3d, 0x01 ] # Printer select
|
11
|
+
HW_RESET = [ 0x1b, 0x3f, 0x0a, 0x00 ] # Reset printer hardware
|
12
|
+
|
13
|
+
# Feed control sequences
|
14
|
+
CTL_LF = [ 0x0a ] # Print and line feed
|
15
|
+
CTL_FF = [ 0x0c ] # Form feed
|
16
|
+
CTL_CR = [ 0x0d ] # Carriage return
|
17
|
+
CTL_HT = [ 0x09 ] # Horizontal tab
|
18
|
+
CTL_VT = [ 0x0b ] # Vertical tab
|
19
|
+
|
20
|
+
# Paper
|
21
|
+
PAPER_FULL_CUT = [ 0x1d, 0x56, 0x00 ] # Full paper cut
|
22
|
+
PAPER_PARTIAL_CUT = [ 0x1d, 0x56, 0x01 ] # Partial paper cut
|
23
|
+
|
24
|
+
# Cash Drawer
|
25
|
+
CD_KICK_2 = [ 0x1b, 0x70, 0x00 ] # Send pulse to pin 2
|
26
|
+
CD_KICK_5 = [ 0x1b, 0x70, 0x01 ] # Send pulse to pin 5
|
27
|
+
|
28
|
+
# Text formating
|
29
|
+
TXT_NORMAL = [ 0x1b, 0x21, 0x00 ] # Normal text
|
30
|
+
TXT_2HEIGHT = [ 0x1b, 0x21, 0x10 ] # Double height text
|
31
|
+
TXT_2WIDTH = [ 0x1b, 0x21, 0x20 ] # Double width text
|
32
|
+
TXT_4SQUARE = [ 0x1b, 0x21, 0x30 ] # Quad area text
|
33
|
+
TXT_UNDERL_OFF = [ 0x1b, 0x2d, 0x00 ] # Underline font OFF
|
34
|
+
TXT_UNDERL_ON = [ 0x1b, 0x2d, 0x01 ] # Underline font 1
|
35
|
+
TXT_UNDERL2_ON = [ 0x1b, 0x2d, 0x02 ] # Underline font 2
|
36
|
+
TXT_BOLD_OFF = [ 0x1b, 0x45, 0x00 ] # Bold font OFF
|
37
|
+
TXT_BOLD_ON = [ 0x1b, 0x45, 0x01 ] # Bold font ON
|
38
|
+
TXT_FONT_A = [ 0x1b, 0x4d, 0x00 ] # Font type A
|
39
|
+
TXT_FONT_B = [ 0x1b, 0x4d, 0x01 ] # Font type B
|
40
|
+
TXT_ALIGN_LT = [ 0x1b, 0x61, 0x00 ] # Left justification
|
41
|
+
TXT_ALIGN_CT = [ 0x1b, 0x61, 0x01 ] # Centering
|
42
|
+
TXT_ALIGN_RT = [ 0x1b, 0x61, 0x02 ] # Right justification
|
43
|
+
|
44
|
+
# Barcodes
|
45
|
+
BARCODE_TXT_OFF = [ 0x1d, 0x48, 0x00 ] # HRI barcode chars OFF
|
46
|
+
BARCODE_TXT_ABV = [ 0x1d, 0x48, 0x01 ] # HRI barcode chars above
|
47
|
+
BARCODE_TXT_BLW = [ 0x1d, 0x48, 0x02 ] # HRI barcode chars below
|
48
|
+
BARCODE_TXT_BTH = [ 0x1d, 0x48, 0x03 ] # HRI barcode chars both above and below
|
49
|
+
BARCODE_FONT_A = [ 0x1d, 0x66, 0x00 ] # Font type A for HRI barcode chars
|
50
|
+
BARCODE_FONT_B = [ 0x1d, 0x66, 0x01 ] # Font type B for HRI barcode chars
|
51
|
+
BARCODE_HEIGHT = [ 0x1d, 0x68 ] # Barcode Height (1 - 255)
|
52
|
+
BARCODE_WIDTH = [ 0x1d, 0x77 ] # Barcode Width (2 - 6)
|
53
|
+
BARCODE_UPC_A = [ 0x1d, 0x6b, 0x00 ] # Barcode type UPC-A
|
54
|
+
BARCODE_UPC_E = [ 0x1d, 0x6b, 0x01 ] # Barcode type UPC-E
|
55
|
+
BARCODE_EAN13 = [ 0x1d, 0x6b, 0x02 ] # Barcode type EAN13
|
56
|
+
BARCODE_EAN8 = [ 0x1d, 0x6b, 0x03 ] # Barcode type EAN8
|
57
|
+
BARCODE_CODE39 = [ 0x1d, 0x6b, 0x04 ] # Barcode type CODE39
|
58
|
+
BARCODE_ITF = [ 0x1d, 0x6b, 0x05 ] # Barcode type ITF
|
59
|
+
BARCODE_NW7 = [ 0x1d, 0x6b, 0x06 ] # Barcode type NW7
|
60
|
+
|
61
|
+
# Images
|
62
|
+
IMAGE = [ 0x1d, 0x76, 0x30, 0x00 ] # Start image pixel data
|
63
|
+
|
64
|
+
# Transforms an array of codes into a string
|
65
|
+
def sequence(arr_sequence)
|
66
|
+
arr_sequence.pack('U*')
|
67
|
+
end
|
68
|
+
module_function :sequence
|
69
|
+
|
70
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
module Escpos
|
2
|
+
module Helpers
|
3
|
+
|
4
|
+
# Encodes UTF-8 string to encoding acceptable for the printer
|
5
|
+
# The printer must be set to that encoding, defaults to ISO-8859-2
|
6
|
+
# Available encodings can be listed in console using Encoding.constants
|
7
|
+
def encode(data, opts = {})
|
8
|
+
data.encode(opts.fetch(:encoding, 'ISO-8859-2'), 'UTF-8', {
|
9
|
+
invalid: opts.fetch(:invalid, :replace),
|
10
|
+
undef: opts.fetch(:undef, :replace),
|
11
|
+
replace: opts.fetch(:replace, '?')
|
12
|
+
})
|
13
|
+
end
|
14
|
+
|
15
|
+
def text(data)
|
16
|
+
[
|
17
|
+
Escpos.sequence(TXT_NORMAL),
|
18
|
+
data,
|
19
|
+
Escpos.sequence(TXT_NORMAL),
|
20
|
+
].join
|
21
|
+
end
|
22
|
+
|
23
|
+
def double_height(data)
|
24
|
+
[
|
25
|
+
Escpos.sequence(TXT_2HEIGHT),
|
26
|
+
data,
|
27
|
+
Escpos.sequence(TXT_NORMAL),
|
28
|
+
].join
|
29
|
+
end
|
30
|
+
|
31
|
+
def quad_text(data)
|
32
|
+
[
|
33
|
+
Escpos.sequence(TXT_4SQUARE),
|
34
|
+
data,
|
35
|
+
Escpos.sequence(TXT_NORMAL),
|
36
|
+
].join
|
37
|
+
end
|
38
|
+
alias :big :quad_text
|
39
|
+
alias :title :quad_text
|
40
|
+
alias :header :quad_text
|
41
|
+
alias :double_width_double_height :quad_text
|
42
|
+
alias :double_height_double_width :quad_text
|
43
|
+
|
44
|
+
def double_width(data)
|
45
|
+
[
|
46
|
+
Escpos.sequence(TXT_2WIDTH),
|
47
|
+
data,
|
48
|
+
Escpos.sequence(TXT_NORMAL),
|
49
|
+
].join
|
50
|
+
end
|
51
|
+
|
52
|
+
def underline(data)
|
53
|
+
[
|
54
|
+
Escpos.sequence(TXT_UNDERL_ON),
|
55
|
+
data,
|
56
|
+
Escpos.sequence(TXT_UNDERL_OFF),
|
57
|
+
].join
|
58
|
+
end
|
59
|
+
alias :u :underline
|
60
|
+
|
61
|
+
def underline2(data)
|
62
|
+
[
|
63
|
+
Escpos.sequence(TXT_UNDERL2_ON),
|
64
|
+
data,
|
65
|
+
Escpos.sequence(TXT_UNDERL_OFF),
|
66
|
+
].join
|
67
|
+
end
|
68
|
+
alias :u2 :underline2
|
69
|
+
|
70
|
+
def bold(data)
|
71
|
+
[
|
72
|
+
Escpos.sequence(TXT_BOLD_ON),
|
73
|
+
data,
|
74
|
+
Escpos.sequence(TXT_BOLD_OFF),
|
75
|
+
].join
|
76
|
+
end
|
77
|
+
alias :b :bold
|
78
|
+
|
79
|
+
def left(data)
|
80
|
+
[
|
81
|
+
Escpos.sequence(TXT_ALIGN_LT),
|
82
|
+
data,
|
83
|
+
Escpos.sequence(TXT_ALIGN_LT),
|
84
|
+
].join
|
85
|
+
end
|
86
|
+
|
87
|
+
def right(data)
|
88
|
+
[
|
89
|
+
Escpos.sequence(TXT_ALIGN_RT),
|
90
|
+
data,
|
91
|
+
Escpos.sequence(TXT_ALIGN_LT),
|
92
|
+
].join
|
93
|
+
end
|
94
|
+
|
95
|
+
def center(data)
|
96
|
+
[
|
97
|
+
Escpos.sequence(TXT_ALIGN_CT),
|
98
|
+
data,
|
99
|
+
Escpos.sequence(TXT_ALIGN_LT),
|
100
|
+
].join
|
101
|
+
end
|
102
|
+
|
103
|
+
def barcode(data, opts = {})
|
104
|
+
text_position = opts.fetch(:text_position, BARCODE_TXT_OFF)
|
105
|
+
unless [BARCODE_TXT_OFF, BARCODE_TXT_ABV, BARCODE_TXT_BLW, BARCODE_TXT_BTH].include?(text_position)
|
106
|
+
raise ArgumentError("Text position must be one of the following: BARCODE_TXT_OFF, BARCODE_TXT_ABV, BARCODE_TXT_BLW, BARCODE_TXT_BTH.")
|
107
|
+
end
|
108
|
+
height = opts.fetch(:height, 50)
|
109
|
+
raise ArgumentError("Height must be in range from 1 to 255.") if height && (height < 1 || height > 255)
|
110
|
+
width = opts.fetch(:width, 3)
|
111
|
+
raise ArgumentError("Width must be in range from 2 to 6.") if width && (width < 2 || width > 6)
|
112
|
+
[
|
113
|
+
Escpos.sequence(text_position),
|
114
|
+
Escpos.sequence(BARCODE_WIDTH),
|
115
|
+
Escpos.sequence([width]),
|
116
|
+
Escpos.sequence(BARCODE_HEIGHT),
|
117
|
+
Escpos.sequence([height]),
|
118
|
+
Escpos.sequence(opts.fetch(:format, BARCODE_EAN13)),
|
119
|
+
data
|
120
|
+
].join
|
121
|
+
end
|
122
|
+
|
123
|
+
def partial_cut
|
124
|
+
Escpos.sequence(PAPER_PARTIAL_CUT)
|
125
|
+
end
|
126
|
+
|
127
|
+
def cut
|
128
|
+
Escpos.sequence(PAPER_FULL_CUT)
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'base64'
|
2
|
+
|
3
|
+
module Escpos
|
4
|
+
|
5
|
+
class Printer
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
# ensure only supported sequences are generated
|
9
|
+
@data = "".force_encoding("ASCII-8BIT")
|
10
|
+
@data = Escpos.sequence HW_INIT
|
11
|
+
end
|
12
|
+
|
13
|
+
def write(data)
|
14
|
+
@data << data
|
15
|
+
end
|
16
|
+
|
17
|
+
def partial_cut!
|
18
|
+
@data << Escpos.sequence(PAPER_PARTIAL_CUT)
|
19
|
+
end
|
20
|
+
|
21
|
+
def cut!
|
22
|
+
@data << Escpos.sequence(PAPER_FULL_CUT)
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_escpos
|
26
|
+
@data
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_base64
|
30
|
+
Base64.strict_encode64 @data
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'erb'
|
2
|
+
|
3
|
+
module Escpos
|
4
|
+
class Report
|
5
|
+
include ERB::Util
|
6
|
+
include Helpers
|
7
|
+
|
8
|
+
def initialize(file_or_path, optr = {})
|
9
|
+
if file_or_path.is_a?(String)
|
10
|
+
@template = ERB.new(File.open(file_or_path).read)
|
11
|
+
elsif file_or_path.is_a?(File)
|
12
|
+
@template = ERB.new(file_or_path.read)
|
13
|
+
else
|
14
|
+
raise ArgumentError.new("Must pass instance of file or path as argument.")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def render
|
19
|
+
@template.result binding
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
Unformatted text
|
2
|
+
|
3
|
+
<%= encode "ISO-8859-2 encoded text. (ěščřžýáíéúů)", encoding: "ISO-8859-2" %>
|
4
|
+
|
5
|
+
<%= text "Normal text" %>
|
6
|
+
|
7
|
+
<%= double_height "Double height text" %>
|
8
|
+
|
9
|
+
<%= double_width "Double width text" %>
|
10
|
+
|
11
|
+
<%= quad_text "Quad area text" %>
|
12
|
+
|
13
|
+
<%= underline "Underlined text" %>
|
14
|
+
|
15
|
+
<%= underline2 "Underlined text (2)" %>
|
16
|
+
|
17
|
+
<%= bold "Bold text" %>
|
18
|
+
|
19
|
+
<%= left "Left aligned text" %>
|
20
|
+
|
21
|
+
<%= right "Right aligned text" %>
|
22
|
+
|
23
|
+
<%= center "Centered text" %>
|
24
|
+
|
25
|
+
<%= barcode "8594404000572" %>
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative '../../test_helper'
|
2
|
+
|
3
|
+
class TestPrinter < Minitest::Test
|
4
|
+
def setup
|
5
|
+
@printer = Escpos::Printer.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_styles
|
9
|
+
require 'erb'
|
10
|
+
template = ERB.new(File.open(File.join(__dir__, '../../fixtures/styles.erb')).read)
|
11
|
+
@printer.write template.result(Class.new { include Escpos::Helpers }.new.instance_eval { binding })
|
12
|
+
@printer.cut!
|
13
|
+
#pp @printer.to_base64
|
14
|
+
assert_equal @printer.to_base64, 'G0BVbmZvcm1hdHRlZCB0ZXh0CgpJU08tODg1OS0yIGVuY29kZWQgdGV4dC4gKOy56Pi+/eHt6fr5KQoKGyEATm9ybWFsIHRleHQbIQAKChshEERvdWJsZSBoZWlnaHQgdGV4dBshAAoKGyEgRG91YmxlIHdpZHRoIHRleHQbIQAKChshMFF1YWQgYXJlYSB0ZXh0GyEACgobLQFVbmRlcmxpbmVkIHRleHQbLQAKChstAlVuZGVybGluZWQgdGV4dCAoMikbLQAKChtFAUJvbGQgdGV4dBtFAAoKG2EATGVmdCBhbGlnbmVkIHRleHQbYQAKChthAlJpZ2h0IGFsaWduZWQgdGV4dBthAAoKG2EBQ2VudGVyZWQgdGV4dBthAAoKHUgAHXcDHWgyHWsCODU5NDQwNDAwMDU3MgoKCgoKHVYA'
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative '../../test_helper'
|
2
|
+
|
3
|
+
class TestReport < Minitest::Test
|
4
|
+
def setup
|
5
|
+
@printer = Escpos::Printer.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_styles
|
9
|
+
report_klass = Class.new(Escpos::Report)
|
10
|
+
report_klass.class_eval do
|
11
|
+
def item(text)
|
12
|
+
@count ||= 0
|
13
|
+
@count += 1
|
14
|
+
quad_text "#{@count}. #{text}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
report = report_klass.new(File.join(__dir__, '../../fixtures/report.erb'))
|
18
|
+
|
19
|
+
@printer.write report.render
|
20
|
+
@printer.cut!
|
21
|
+
#pp @printer.to_base64
|
22
|
+
assert_equal @printer.to_base64, 'G0AbITAxLiBGaXJzdCBpdGVtGyEAChshMDIuIFNlY29uZCBpdGVtGyEAChshMDMuIFRoaXJkIGl0ZW0bIQAKCgoKCh1WAA=='
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: escpos
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jan Svoboda
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 5.4.2
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 5.4.2
|
55
|
+
description: A ruby implementation of ESC/POS (thermal) printer command specification.
|
56
|
+
email:
|
57
|
+
- jan@mluv.cz
|
58
|
+
executables:
|
59
|
+
- console
|
60
|
+
- setup
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- Gemfile
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- config
|
71
|
+
- escpos.gemspec
|
72
|
+
- examples/report.erb
|
73
|
+
- examples/report.rb
|
74
|
+
- lib/escpos.rb
|
75
|
+
- lib/escpos/helpers.rb
|
76
|
+
- lib/escpos/printer.rb
|
77
|
+
- lib/escpos/report.rb
|
78
|
+
- lib/escpos/version.rb
|
79
|
+
- test/fixtures/report.erb
|
80
|
+
- test/fixtures/styles.erb
|
81
|
+
- test/lib/escpos/printer_test.rb
|
82
|
+
- test/lib/escpos/report_test.rb
|
83
|
+
- test/test_helper.rb
|
84
|
+
homepage: https://github.com/escpos/escpos
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata: {}
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.9'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.5.1
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: A ruby implementation of ESC/POS (thermal) printer command specification.
|
108
|
+
test_files:
|
109
|
+
- test/fixtures/report.erb
|
110
|
+
- test/fixtures/styles.erb
|
111
|
+
- test/lib/escpos/printer_test.rb
|
112
|
+
- test/lib/escpos/report_test.rb
|
113
|
+
- test/test_helper.rb
|
114
|
+
has_rdoc:
|