super_pdf 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/super_pdf.rb +11 -0
- data/lib/super_pdf/document.rb +9 -0
- data/lib/super_pdf/enhance.rb +23 -0
- data/lib/super_pdf/font.rb +19 -0
- data/lib/super_pdf/measurement.rb +8 -0
- data/lib/super_pdf/page.rb +26 -0
- data/lib/super_pdf/renderer.rb +296 -0
- data/lib/super_pdf/renderer/helper.rb +101 -0
- data/lib/super_pdf/renderer/rows.rb +51 -0
- data/lib/super_pdf/version.rb +3 -0
- data/super_pdf.gemspec +31 -0
- metadata +166 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a1aab74252b84e463c3409a0e58bc6352108ab8a
|
4
|
+
data.tar.gz: 6f84ddab706ba13eeeb3470f32742f677e7bc109
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 02f77e14c60f360501399c87ee32ca519021c4729ca562b22adb0cfff8625aa190611729f4ad085282887d87f20e6904dbb626806dda801847de7c6de0bab1ed
|
7
|
+
data.tar.gz: ccb1732b47bd145054f5b73b91684caaf94c1d29f73747fc3ab6cab9fda6325742f954ab12c6a22dddc3401536d07f8268e5ff71ccdeba18067b19491a8d28ba
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 vegeta
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# SuperPDF
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/super_pdf`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'super_pdf'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install super_pdf
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/vegeta/super_pdf.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "super_pdf"
|
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(__FILE__)
|
data/bin/setup
ADDED
data/lib/super_pdf.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require "super_pdf/document"
|
2
|
+
|
3
|
+
module SuperPDF
|
4
|
+
module Enhance
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
include Font
|
9
|
+
include Page
|
10
|
+
|
11
|
+
def pdf
|
12
|
+
@pdf ||= Document.new(
|
13
|
+
page_size: page_size,
|
14
|
+
skip_page_creation: true,
|
15
|
+
margin: 0
|
16
|
+
).tap do |document|
|
17
|
+
document.font_families.update(Font::FAMILIES) unless Font::FAMILIES.empty?
|
18
|
+
document.default_font_family = default_font_family
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module SuperPDF
|
2
|
+
module Font
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
FAMILIES = {}
|
6
|
+
|
7
|
+
included do
|
8
|
+
delegate :default_font_family, to: :class
|
9
|
+
end
|
10
|
+
|
11
|
+
class_methods do
|
12
|
+
attr_reader :default_font_family
|
13
|
+
|
14
|
+
def default_font(key)
|
15
|
+
@default_font_family = key
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module SuperPDF
|
2
|
+
module Page
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
DEFAULT_SIZE = 'A4'
|
6
|
+
SIZE = {}
|
7
|
+
|
8
|
+
included do
|
9
|
+
def page_size
|
10
|
+
self.class.custom_page_size || DEFAULT_SIZE
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class_methods do
|
15
|
+
attr_reader :custom_page_size
|
16
|
+
|
17
|
+
def page_size(key_or_size)
|
18
|
+
@custom_page_size ||= Page.real_size(key_or_size)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.real_size(key_or_size)
|
23
|
+
SIZE[key_or_size]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,296 @@
|
|
1
|
+
require 'super_pdf/renderer/helper'
|
2
|
+
require 'super_pdf/renderer/rows'
|
3
|
+
|
4
|
+
module SuperPDF
|
5
|
+
module Renderer
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
include Renderer::Helper
|
8
|
+
|
9
|
+
included do
|
10
|
+
|
11
|
+
# 開新的一頁
|
12
|
+
# Examples:
|
13
|
+
# page(size: "A4", layout: :landscape)
|
14
|
+
# page(left_margin: 50, right_margin: 50)
|
15
|
+
# page(margin: 100)
|
16
|
+
# page(font: 'MJSH')
|
17
|
+
#
|
18
|
+
def page(options={})
|
19
|
+
family = options.delete(:font) || default_font_family
|
20
|
+
options[:size] = Page.real_size(options[:size])
|
21
|
+
start_new_page(options)
|
22
|
+
with_font(family) do
|
23
|
+
yield if block_given?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# 文字
|
28
|
+
#
|
29
|
+
# Examples
|
30
|
+
# text("...", size: 8, align: :center, valign: :center) do
|
31
|
+
# ...
|
32
|
+
# end
|
33
|
+
def text(string, options={})
|
34
|
+
family = options.delete(:family)
|
35
|
+
with_font(family) do
|
36
|
+
super(string, options)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# 區塊
|
41
|
+
# Options:
|
42
|
+
# options[:margin]
|
43
|
+
# options[:padding]
|
44
|
+
# options[:width]
|
45
|
+
# options[:height]
|
46
|
+
# options[:background_color]
|
47
|
+
#
|
48
|
+
# Examples:
|
49
|
+
# div(margin: [20, 30], padding: 10, border: true) do
|
50
|
+
# ...
|
51
|
+
# end
|
52
|
+
# div(margin: [20, 30], width: '50%', border: true) do
|
53
|
+
# ...
|
54
|
+
# end
|
55
|
+
def div(options = {}, &block)
|
56
|
+
margin_div(options[:margin], options.slice(:width, :height, :border, :background_color)) do
|
57
|
+
padding_div(options[:padding], options.slice(:width, :height), &block)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Examples:
|
62
|
+
#
|
63
|
+
# Margin + width
|
64
|
+
# margin_div(20, width: 100, height: 100, border: true) do
|
65
|
+
# ...
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# Auto width:
|
69
|
+
# margin_div([10, 20]) do
|
70
|
+
# ....
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
def margin_div(margin, width: nil, height: nil, border: false, background_color: nil, &block)
|
74
|
+
margin = convert_margin(margin)
|
75
|
+
|
76
|
+
# 上一個 Element 的寬度, 要讓他擠下來
|
77
|
+
pre_height = bounds.height - cursor
|
78
|
+
|
79
|
+
# 外寬
|
80
|
+
width = percent2pt_of_width(width) if percentage?(width)
|
81
|
+
width ||= (bounds.width - margin.left - margin.right)
|
82
|
+
width += (margin.left + margin.right)
|
83
|
+
|
84
|
+
# 外高
|
85
|
+
height = percent2pt_of_height(height) if percentage?(height)
|
86
|
+
height ||= bounds.height - margin.top - margin.bottom - pre_height
|
87
|
+
height += (margin.top + margin.bottom)
|
88
|
+
|
89
|
+
# 外框
|
90
|
+
bounding_box([bounds.left, bounds.top - pre_height], width: width, height: height) do
|
91
|
+
left = bounds.left + margin.left
|
92
|
+
top = bounds.top - margin.top
|
93
|
+
width = bounds.width - margin.left - margin.right
|
94
|
+
height = bounds.height - margin.top - margin.bottom
|
95
|
+
# 內框
|
96
|
+
bounding_box([left, top], width: width, height: height) do
|
97
|
+
yield if block_given?
|
98
|
+
fill_background_color(background_color) if background_color
|
99
|
+
draw_border(convert_border_options(border)) if border
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# Examples:
|
105
|
+
#
|
106
|
+
# padding_div(20, width: 100, height: 100, border: true) do
|
107
|
+
# ...
|
108
|
+
# end
|
109
|
+
#
|
110
|
+
# Auto width:
|
111
|
+
# padding_div([10, 20, 10, 10]) do
|
112
|
+
# ...
|
113
|
+
# end
|
114
|
+
#
|
115
|
+
def padding_div(padding, width: nil, height: nil, border: false, background_color: false, &block)
|
116
|
+
padding = convert_margin(padding)
|
117
|
+
|
118
|
+
pre_height = bounds.height - cursor
|
119
|
+
|
120
|
+
width = percent2pt_of_width(width) if percentage?(width)
|
121
|
+
width ||= bounds.width
|
122
|
+
|
123
|
+
height = percent2pt_of_height(height) if percentage?(height)
|
124
|
+
height ||= bounds.height - pre_height
|
125
|
+
|
126
|
+
bounding_box([bounds.left, bounds.top - pre_height], width: width, height: height) do
|
127
|
+
draw_border(convert_border_options(border)) if border
|
128
|
+
fill_background_color(background_color) if background_color
|
129
|
+
|
130
|
+
left = bounds.left + padding.left
|
131
|
+
top = bounds.top - padding.top
|
132
|
+
width = bounds.width - padding.left - padding.right
|
133
|
+
height = bounds.height - padding.top - padding.bottom
|
134
|
+
bounding_box([left, top], width: width, height: height) do
|
135
|
+
yield if block_given?
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# Examples:
|
141
|
+
#
|
142
|
+
# abs_div(left: 10, top: 10) do
|
143
|
+
# ...
|
144
|
+
# end
|
145
|
+
#
|
146
|
+
# abs_div(left: '10%', top: '10%') do
|
147
|
+
# ...
|
148
|
+
# end
|
149
|
+
#
|
150
|
+
# abs_div(left: 10, top: 10, right: 20, bottom: 20) do
|
151
|
+
# ...
|
152
|
+
# end
|
153
|
+
#
|
154
|
+
# abs_div(border: true, background_color: '#222') do
|
155
|
+
# ...
|
156
|
+
# end
|
157
|
+
#
|
158
|
+
def abs_div(left: 0, right: 0, top: 0, bottom: 0, border: false, background_color: false, &block)
|
159
|
+
left = percent2pt_of_width(left) if percentage?(left)
|
160
|
+
right = percent2pt_of_width(right) if percentage?(right)
|
161
|
+
top = percent2pt_of_height(top) if percentage?(top)
|
162
|
+
bottom = percent2pt_of_height(bottom) if percentage?(bottom)
|
163
|
+
|
164
|
+
width = bounds.width - left - right
|
165
|
+
height = bounds.height - top - bottom
|
166
|
+
|
167
|
+
cursor_cache = cursor
|
168
|
+
|
169
|
+
bounding_box([
|
170
|
+
bounds.left + left, bounds.top - top
|
171
|
+
], width: width, height: height) do
|
172
|
+
fill_background_color(background_color) if background_color
|
173
|
+
draw_border(convert_border_options(border)) if border
|
174
|
+
yield if block_given?
|
175
|
+
end
|
176
|
+
move_cursor_to cursor_cache
|
177
|
+
end
|
178
|
+
|
179
|
+
# 似 bootstrap4 grid
|
180
|
+
#
|
181
|
+
# div(options) do
|
182
|
+
# row do |row|
|
183
|
+
# row.column(width: '50%', ...) do
|
184
|
+
# ...
|
185
|
+
# end
|
186
|
+
# row.column(width: '50%', ...) do
|
187
|
+
# ...
|
188
|
+
# end
|
189
|
+
# end
|
190
|
+
# end
|
191
|
+
#
|
192
|
+
def row(&block)
|
193
|
+
div do
|
194
|
+
rows.row(&block)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
# 縮放
|
199
|
+
#
|
200
|
+
# Params
|
201
|
+
# factor 縮放比例
|
202
|
+
# options[:origin] 縮放起始點
|
203
|
+
# options[:position] :center
|
204
|
+
#
|
205
|
+
# Examples
|
206
|
+
# scale(0.8) do
|
207
|
+
# ...
|
208
|
+
# end
|
209
|
+
#
|
210
|
+
# scale(0.8, origin: [0, 100]) do
|
211
|
+
# ...
|
212
|
+
# end
|
213
|
+
#
|
214
|
+
# scale(0.8, position: :center) do
|
215
|
+
# ...
|
216
|
+
# end
|
217
|
+
#
|
218
|
+
def scale(factor, options={}, &block)
|
219
|
+
position = options.delete(:position)
|
220
|
+
case position
|
221
|
+
when :center
|
222
|
+
options[:origin] ||= [
|
223
|
+
bounds.left + (bounds.width / 2),
|
224
|
+
cursor - (bounds.height / 2)
|
225
|
+
]
|
226
|
+
else
|
227
|
+
options[:origin] ||= [ bounds.left, cursor ]
|
228
|
+
end
|
229
|
+
super(factor, options, &block)
|
230
|
+
end
|
231
|
+
|
232
|
+
# 填充背景顏色
|
233
|
+
#
|
234
|
+
# Examples:
|
235
|
+
# fill_background_color('#f0ffc1')
|
236
|
+
#
|
237
|
+
def fill_background_color(code)
|
238
|
+
code = full_color_code(code)
|
239
|
+
fill_color(code)
|
240
|
+
fill_rectangle([bounds.left, bounds.top], bounds.width, bounds.height)
|
241
|
+
end
|
242
|
+
|
243
|
+
# 旋轉
|
244
|
+
# def rotate(angle, options={}, &block)
|
245
|
+
# super(angle, options={}, &block)
|
246
|
+
# end
|
247
|
+
|
248
|
+
# 複寫 Prawn::Document,讓其可以吃預設字體
|
249
|
+
# def font(name=nil, options={}, &block)
|
250
|
+
# font_family = (name || default_font_family)
|
251
|
+
# super(font_family, options, &block)
|
252
|
+
# end
|
253
|
+
|
254
|
+
# 頁面寬
|
255
|
+
def page_width
|
256
|
+
margin_box.width
|
257
|
+
end
|
258
|
+
|
259
|
+
# 頁面高
|
260
|
+
def page_height
|
261
|
+
margin_box.height
|
262
|
+
end
|
263
|
+
|
264
|
+
def draw_border(color:, style:, width:)
|
265
|
+
line_width(width)
|
266
|
+
stroke_color(color)
|
267
|
+
case style
|
268
|
+
when "dotted"
|
269
|
+
dash(1, space: 1)
|
270
|
+
when "dashed"
|
271
|
+
dash(4, space: 2)
|
272
|
+
when "solid"
|
273
|
+
# Do nothing.
|
274
|
+
else
|
275
|
+
raise 'Style only for "dotted", "dashed", "solid"'
|
276
|
+
end
|
277
|
+
stroke_bounds
|
278
|
+
undash
|
279
|
+
end
|
280
|
+
|
281
|
+
protected
|
282
|
+
|
283
|
+
def rows
|
284
|
+
@rows ||= Rows.new(self)
|
285
|
+
end
|
286
|
+
|
287
|
+
def with_font(family, &block)
|
288
|
+
if family
|
289
|
+
font(family, &block)
|
290
|
+
else
|
291
|
+
block.call
|
292
|
+
end
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module SuperPDF
|
2
|
+
module Renderer
|
3
|
+
module Helper
|
4
|
+
include ::Prawn::Measurements
|
5
|
+
|
6
|
+
REGEX_PERCENTAGE = /\A(?<percentage>\d+)%\z/
|
7
|
+
REGEX_COLOR_CODE = /\A#(?<color>\S+)\z/
|
8
|
+
REGEX_BORDER_OPTION = /\A(?<width>[0-9.]+)(?<unit>mm|cm|dm|m|in|yd|ft|pt)\s(?<style>\S+)\s(?<color>\S+)\z/
|
9
|
+
|
10
|
+
def percent2pt(value, base_length)
|
11
|
+
number = value.match(REGEX_PERCENTAGE)[:percentage]
|
12
|
+
base_length * (number.to_f / 100)
|
13
|
+
end
|
14
|
+
|
15
|
+
def percent2pt_of_width(value)
|
16
|
+
percent2pt(value, bounds.width)
|
17
|
+
end
|
18
|
+
|
19
|
+
def percent2pt_of_height(value)
|
20
|
+
percent2pt(value, bounds.height)
|
21
|
+
end
|
22
|
+
|
23
|
+
def percentage?(value)
|
24
|
+
return false unless value.is_a?(String)
|
25
|
+
value.match(REGEX_PERCENTAGE)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Examples:
|
29
|
+
# #333 -> 333333
|
30
|
+
# #333333 -> 333333
|
31
|
+
def full_color_code(color)
|
32
|
+
matched = color.match(REGEX_COLOR_CODE)
|
33
|
+
raise ArgumentError, 'Ex: #222' if matched.nil?
|
34
|
+
case matched[:color].length
|
35
|
+
when 3; matched[:color] * 2
|
36
|
+
when 6; matched[:color]
|
37
|
+
else
|
38
|
+
raise ArgumentError, "Wrong color code, ex: #333 | #333333"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Examples:
|
43
|
+
# 4
|
44
|
+
# [2, 4]
|
45
|
+
# [2, 4, 2, 4]
|
46
|
+
def convert_padding(value)
|
47
|
+
values = Array(value)
|
48
|
+
sides = %i[top right bottom left]
|
49
|
+
|
50
|
+
# Treat :margin as CSS shorthand with 1-4 values.
|
51
|
+
positions = {
|
52
|
+
4 => [0, 1, 2, 3], 3 => [0, 1, 2, 1],
|
53
|
+
2 => [0, 1, 0, 1], 1 => [0, 0, 0, 0],
|
54
|
+
0 => []
|
55
|
+
}[values.length]
|
56
|
+
|
57
|
+
top, right, bottom, left = sides.zip(positions).map do |side, pos|
|
58
|
+
pos ? values[pos] : 0
|
59
|
+
end
|
60
|
+
|
61
|
+
OpenStruct.new(top: top, right: right, bottom: bottom, left: left)
|
62
|
+
end
|
63
|
+
alias_method :convert_margin, :convert_padding
|
64
|
+
|
65
|
+
# "1pt solid #222" => width: 1, style: solid, color: 222222
|
66
|
+
# "2pt dashed #fff000" => width: 2, style: dashed, color: fff000
|
67
|
+
# "3pt dotted #444" => width: 3, style: dotted, color: 444444
|
68
|
+
def convert_border_options(setting)
|
69
|
+
case setting
|
70
|
+
when TrueClass
|
71
|
+
{ color: '000000', style: 'solid', width: 1 }
|
72
|
+
when String
|
73
|
+
matched = setting.match(REGEX_BORDER_OPTION)
|
74
|
+
raise ArgumentError if matched.nil?
|
75
|
+
{
|
76
|
+
color: full_color_code(matched[:color]),
|
77
|
+
style: matched[:style],
|
78
|
+
width: convert_numeric(matched[:width].to_f, matched[:unit])
|
79
|
+
}
|
80
|
+
when Hash
|
81
|
+
setting
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def convert_numeric(n, unit)
|
86
|
+
case unit
|
87
|
+
when 'mm'; mm2pt(n)
|
88
|
+
when 'cm'; cm2pt(n)
|
89
|
+
when 'dm'; dm2pt(n)
|
90
|
+
when 'm' ; m2pt(n)
|
91
|
+
when 'in'; in2pt(n)
|
92
|
+
when 'yd'; yd2pt(n)
|
93
|
+
when 'ft'; ft2pt(n)
|
94
|
+
when 'pt'; pt2pt(n)
|
95
|
+
else
|
96
|
+
raise ArgumentError, "Unknow unit `#{unit}`"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module SuperPDF
|
2
|
+
module Renderer
|
3
|
+
class Rows
|
4
|
+
attr_reader :rows, :pdf
|
5
|
+
|
6
|
+
def initialize(pdf)
|
7
|
+
@pdf = pdf
|
8
|
+
end
|
9
|
+
|
10
|
+
def row(&block)
|
11
|
+
row = Row.new(pdf.cursor, pdf)
|
12
|
+
rows << row
|
13
|
+
|
14
|
+
block.call(row)
|
15
|
+
|
16
|
+
rows.pop
|
17
|
+
end
|
18
|
+
|
19
|
+
def rows
|
20
|
+
@rows ||= []
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Row
|
25
|
+
attr_reader :cursor, :pdf
|
26
|
+
|
27
|
+
def initialize(cursor, pdf)
|
28
|
+
@cursor = cursor
|
29
|
+
@pdf = pdf
|
30
|
+
end
|
31
|
+
|
32
|
+
def column(options, &block)
|
33
|
+
@left_pos ||= pdf.bounds.left
|
34
|
+
width = options.delete(:width)
|
35
|
+
width = pdf.percent2pt_of_width(width) if pdf.percentage?(width)
|
36
|
+
pdf.bounding_box([@left_pos, pdf.bounds.top], width: width, height: pdf.bounds.height) do
|
37
|
+
pdf.div(options) do
|
38
|
+
@left_pos += pdf.bounds.width
|
39
|
+
yield
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
fallback_cursor
|
44
|
+
end
|
45
|
+
|
46
|
+
def fallback_cursor
|
47
|
+
pdf.move_cursor_to cursor
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/super_pdf.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'super_pdf/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "super_pdf"
|
8
|
+
spec.version = SuperPDF::VERSION
|
9
|
+
spec.authors = ["vegeta"]
|
10
|
+
spec.email = ["phyala@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Super PDF"
|
13
|
+
spec.description = "A simple pdf enhance module."
|
14
|
+
spec.homepage = "https://github.com/superlanding/super_pdf"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
+
spec.add_development_dependency "mocha", "~> 1.9"
|
28
|
+
spec.add_dependency "prawn", "~> 2.2", '>= 2.2.2'
|
29
|
+
spec.add_dependency "prawn-table", "0.2.2"
|
30
|
+
spec.add_dependency "activesupport", ">= 4"
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: super_pdf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- vegeta
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-09-02 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: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mocha
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.9'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.9'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: prawn
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.2'
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 2.2.2
|
79
|
+
type: :runtime
|
80
|
+
prerelease: false
|
81
|
+
version_requirements: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - "~>"
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '2.2'
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 2.2.2
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: prawn-table
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - '='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: 0.2.2
|
96
|
+
type: :runtime
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - '='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 0.2.2
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: activesupport
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '4'
|
110
|
+
type: :runtime
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '4'
|
117
|
+
description: A simple pdf enhance module.
|
118
|
+
email:
|
119
|
+
- phyala@gmail.com
|
120
|
+
executables: []
|
121
|
+
extensions: []
|
122
|
+
extra_rdoc_files: []
|
123
|
+
files:
|
124
|
+
- ".gitignore"
|
125
|
+
- Gemfile
|
126
|
+
- LICENSE.txt
|
127
|
+
- README.md
|
128
|
+
- Rakefile
|
129
|
+
- bin/console
|
130
|
+
- bin/setup
|
131
|
+
- lib/super_pdf.rb
|
132
|
+
- lib/super_pdf/document.rb
|
133
|
+
- lib/super_pdf/enhance.rb
|
134
|
+
- lib/super_pdf/font.rb
|
135
|
+
- lib/super_pdf/measurement.rb
|
136
|
+
- lib/super_pdf/page.rb
|
137
|
+
- lib/super_pdf/renderer.rb
|
138
|
+
- lib/super_pdf/renderer/helper.rb
|
139
|
+
- lib/super_pdf/renderer/rows.rb
|
140
|
+
- lib/super_pdf/version.rb
|
141
|
+
- super_pdf.gemspec
|
142
|
+
homepage: https://github.com/superlanding/super_pdf
|
143
|
+
licenses:
|
144
|
+
- MIT
|
145
|
+
metadata: {}
|
146
|
+
post_install_message:
|
147
|
+
rdoc_options: []
|
148
|
+
require_paths:
|
149
|
+
- lib
|
150
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - ">="
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
155
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
requirements: []
|
161
|
+
rubyforge_project:
|
162
|
+
rubygems_version: 2.6.10
|
163
|
+
signing_key:
|
164
|
+
specification_version: 4
|
165
|
+
summary: Super PDF
|
166
|
+
test_files: []
|