reports_web 1.0.0 → 1.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cce3c314a64cb98332c0bd5f20ee01090efcb661fec493cd956200fec8ca554e
4
- data.tar.gz: 56a79b610b2312d3b830276daca73404ccec9b6c776f40c9db953fe9dd6cc875
3
+ metadata.gz: b88e38191d0e912fde120900a9105046b1e52f5e729ebaaf24757d1b2540d8c6
4
+ data.tar.gz: 1adf23f01c0a43eb5d526c09c0b74a80f9de5249eb9b7280b8c3950a79ef43a7
5
5
  SHA512:
6
- metadata.gz: 17ca2e2979d7c46b2ae716a4fd048113bbf5d307098fd7304120b1c892a408052b9d7f636c96015d5b9a784090d28c29ff6434611cb79a64e7ec14fb92ad7d63
7
- data.tar.gz: a7c0cf4f546619cdb3f19c2e2a56c82fc93ba197e3e3c19fce540965cef75576eb45363db74fa2a1e92ddd86c797a815ae33e7f10a3de436c4651a9fd4690b63
6
+ metadata.gz: ec7bea2e3813cfc8c9598767fc58464dc5e277a794a7c79285e5d4e5b53d790106036c07efb483192f02b0518f719201205954b79c89ca6456d18908c9879a74
7
+ data.tar.gz: 6e9bbeb9e304c2b9266d13d92e3b0511e8b885a220c0d5d986f2eb0436d969e2c0777de5689138a731a0a39208f42937a9e41ba34d831828c1980002dcd4e910
data/LICENSE.md CHANGED
@@ -1,22 +1,22 @@
1
- # Reports.Web License / 使用許諾
2
-
3
- Copyright (c) Pao@Office. All rights reserved.
4
-
5
- This package is part of **Reports.Web**, a commercial product of Pao@Office.
6
- Use of this package is governed by the Reports.Web End User License Agreement:
7
-
8
- - 使用許諾(正式な条件): https://www.pao.ac/reports.web/manual/index.html#16
9
- - 製品サイト: https://www.pao.ac/reports.web/
10
- - ご購入・お見積もり: https://www.pao.ac/reports.web/buy.html
11
-
12
- ## 要点 / Summary
13
-
14
- - **体験版 / Trial** — 出力される帳票には赤い「SAMPLE」の印が付きます。購入前の評価に、無償でお使いいただけます。
15
- Output carries a red "SAMPLE" mark. You may evaluate it free of charge before purchase.
16
- - **製品版 / Product** — ご購入時に納品するライセンスファイルを Reports.Web エンジンに設定すると、「SAMPLE」の印が消えます。
17
- Configure the license file delivered on purchase in the Reports.Web engine to remove the mark.
18
- - **ライセンスの数え方 / Licensing** — 開発用パソコン 1 台につき 1 開発ライセンスです。運用専用のサーバーやエンドユーザーのパソコンには不要です(ランタイムライセンスフリー)。
19
- One development license per development PC. Production servers and end-user PCs need no license (royalty-free runtime).
20
-
21
- 上記の要点と使用許諾が異なる場合は、使用許諾が優先します。
22
- If this summary and the license agreement differ, the license agreement prevails.
1
+ # Reports.Web License / 使用許諾
2
+
3
+ Copyright (c) Pao@Office. All rights reserved.
4
+
5
+ This package is part of **Reports.Web**, a commercial product of Pao@Office.
6
+ Use of this package is governed by the Reports.Web End User License Agreement:
7
+
8
+ - 使用許諾(正式な条件、共通マニュアル第16章): https://www.pao.ac/reports.web/manual/Reports-Web-Guide.pdf
9
+ - 製品サイト: https://www.pao.ac/reports.web/
10
+ - ご購入・お見積もり: https://www.pao.ac/reports.web/buy.html
11
+
12
+ ## 要点 / Summary
13
+
14
+ - **体験版 / Trial** — 出力される帳票には赤い「SAMPLE」の印が付きます。購入前の評価に、無償でお使いいただけます。
15
+ Output carries a red "SAMPLE" mark. You may evaluate it free of charge before purchase.
16
+ - **製品版 / Product** — ご購入時に納品するライセンスファイルを Reports.Web エンジンに設定すると、「SAMPLE」の印が消えます。
17
+ Configure the license file delivered on purchase in the Reports.Web engine to remove the mark.
18
+ - **ライセンスの数え方 / Licensing** — 開発用パソコン 1 台につき 1 開発ライセンスです。運用専用のサーバーやエンドユーザーのパソコンには不要です(ランタイムライセンスフリー)。
19
+ One development license per development PC. Production servers and end-user PCs need no license (royalty-free runtime).
20
+
21
+ 上記の要点と使用許諾が異なる場合は、使用許諾が優先します。
22
+ If this summary and the license agreement differ, the license agreement prevails.
data/README.md CHANGED
@@ -22,7 +22,7 @@ your Ruby app ──(PREPEJ JSON)──▶ Reports.Web engine (Docker: ghcr.io/r
22
22
 
23
23
  ```sh
24
24
  gem install reports_web # or: gem 'reports_web', '~> 1.0' in your Gemfile
25
- docker run -d -p 3107:3107 ghcr.io/reportsweb/engine:1.0.0
25
+ docker run -d -p 3107:3107 ghcr.io/reportsweb/engine:1.3.0
26
26
  ```
27
27
 
28
28
  Ruby 3.1 以降。エンジンは Docker イメージで動きます。追加の gem は不要です(json・net/http を使います)。
@@ -79,10 +79,10 @@ Errors are raised as `PaoReportsWeb::ReportsWebEngineError` with the HTTP `statu
79
79
  The gem and the engine image are trial builds: output carries a red "SAMPLE" mark. Mount the license file delivered on purchase to remove it:
80
80
 
81
81
  ```sh
82
- docker run -d -p 3107:3107 -v /path/to/reports-web.license:/app/reports-web.license:ro ghcr.io/reportsweb/engine:1.0.0
82
+ docker run -d -p 3107:3107 -v /path/to/reports-web.license:/app/reports-web.license:ro ghcr.io/reportsweb/engine:1.3.0
83
83
  ```
84
84
 
85
- 開発用パソコン 1 台につき 1 ライセンス、運用環境はランタイムライセンスフリーです。詳しくは [LICENSE.md](LICENSE.md) と [使用許諾](https://www.pao.ac/reports.web/manual/index.html#16) をご覧ください。
85
+ 開発用パソコン 1 台につき 1 ライセンス、運用環境はランタイムライセンスフリーです。詳しくは [LICENSE.md](LICENSE.md) と [使用許諾(共通マニュアル第16章)](https://www.pao.ac/reports.web/manual/Reports-Web-Guide.pdf) をご覧ください。
86
86
 
87
87
  ## 他の言語 / Other languages
88
88
 
@@ -1,80 +1,80 @@
1
- # frozen_string_literal: true
2
- require 'json'
3
- require 'net/http'
4
- require 'uri'
5
-
6
- module PaoReportsWeb
7
- # Error returned by the Reports.Web engine. +status+ is the HTTP status, or nil for transport errors.
8
- class ReportsWebEngineError < StandardError
9
- attr_reader :status
10
-
11
- def initialize(message, status = nil)
12
- super(message)
13
- @status = status
14
- end
15
- end
16
-
17
- # Client for the Reports.Web engine (ghcr.io/reportsweb/engine).
18
- # Sends PREPEJ print data to POST /render/pdf and returns the PDF bytes.
19
- class ReportsWebEngine
20
- DEFAULT_MAX = 32 * 1024 * 1024
21
-
22
- attr_reader :url
23
-
24
- # url: engine base URL (default: REPORTS_ENGINE_URL or http://127.0.0.1:3107)
25
- # timeout: seconds; max_bytes: largest PDF accepted from the engine
26
- def initialize(url: nil, timeout: 90, max_bytes: DEFAULT_MAX)
27
- @url = (url || ENV['REPORTS_ENGINE_URL'] || 'http://127.0.0.1:3107').sub(%r{/+\z}, '')
28
- @timeout = timeout
29
- @max_bytes = max_bytes
30
- end
31
-
32
- # GET /health. Returns the engine status, e.g. {"status"=>"ok", "engine"=>"C++ WebAssembly"}.
33
- def health
34
- response = request(Net::HTTP::Get.new(uri('/health')))
35
- raise ReportsWebEngineError.new("Engine health check failed (HTTP #{response.code}).", response.code.to_i) unless response.is_a?(Net::HTTPSuccess)
36
- JSON.parse(response.body)
37
- end
38
-
39
- # Render PREPEJ print data (PrintData, its Hash form, or JSON text) to a PDF (binary String).
40
- def render_pdf(data, swap_pdf_image: nil)
41
- payload = case data
42
- when String then data
43
- when PrintData then data.to_json
44
- else JSON.generate(data)
45
- end
46
- raise ReportsWebEngineError, 'Print data exceeds 32 MiB.' if payload.bytesize > DEFAULT_MAX
47
- req = Net::HTTP::Post.new(uri('/render/pdf'))
48
- req['Content-Type'] = 'application/json'
49
- req['X-Reports-Swap-Pdf-Image'] = swap_pdf_image.to_s unless swap_pdf_image.nil?
50
- req.body = payload
51
- response = request(req)
52
- unless response.is_a?(Net::HTTPSuccess)
53
- detail = begin
54
- JSON.parse(response.body.to_s)['error']
55
- rescue JSON::ParserError, TypeError
56
- response.body.to_s
57
- end
58
- message = "Engine could not create the PDF (HTTP #{response.code})"
59
- message += detail.to_s.empty? ? '.' : ": #{detail}"
60
- raise ReportsWebEngineError.new(message, response.code.to_i)
61
- end
62
- pdf = response.body.to_s.b
63
- raise ReportsWebEngineError, "PDF exceeds #{@max_bytes} bytes." if pdf.bytesize > @max_bytes
64
- raise ReportsWebEngineError, 'Engine response is not a PDF.' unless pdf.start_with?('%PDF-')
65
- pdf
66
- end
67
-
68
- private
69
-
70
- def uri(path) = URI(@url + path)
71
-
72
- def request(req)
73
- target = req.uri
74
- Net::HTTP.start(target.host, target.port, use_ssl: target.scheme == 'https',
75
- open_timeout: [@timeout, 10].min, read_timeout: @timeout) { |http| http.request(req) }
76
- rescue StandardError => e
77
- raise ReportsWebEngineError, "Cannot reach the Reports.Web engine at #{@url}: #{e.message}"
78
- end
79
- end
80
- end
1
+ # frozen_string_literal: true
2
+ require 'json'
3
+ require 'net/http'
4
+ require 'uri'
5
+
6
+ module PaoReportsWeb
7
+ # Error returned by the Reports.Web engine. +status+ is the HTTP status, or nil for transport errors.
8
+ class ReportsWebEngineError < StandardError
9
+ attr_reader :status
10
+
11
+ def initialize(message, status = nil)
12
+ super(message)
13
+ @status = status
14
+ end
15
+ end
16
+
17
+ # Client for the Reports.Web engine (ghcr.io/reportsweb/engine).
18
+ # Sends PREPEJ print data to POST /render/pdf and returns the PDF bytes.
19
+ class ReportsWebEngine
20
+ DEFAULT_MAX = 32 * 1024 * 1024
21
+
22
+ attr_reader :url
23
+
24
+ # url: engine base URL (default: REPORTS_ENGINE_URL or http://127.0.0.1:3107)
25
+ # timeout: seconds; max_bytes: largest PDF accepted from the engine
26
+ def initialize(url: nil, timeout: 90, max_bytes: DEFAULT_MAX)
27
+ @url = (url || ENV['REPORTS_ENGINE_URL'] || 'http://127.0.0.1:3107').sub(%r{/+\z}, '')
28
+ @timeout = timeout
29
+ @max_bytes = max_bytes
30
+ end
31
+
32
+ # GET /health. Returns the engine status, e.g. {"status"=>"ok", "engine"=>"C++ WebAssembly"}.
33
+ def health
34
+ response = request(Net::HTTP::Get.new(uri('/health')))
35
+ raise ReportsWebEngineError.new("Engine health check failed (HTTP #{response.code}).", response.code.to_i) unless response.is_a?(Net::HTTPSuccess)
36
+ JSON.parse(response.body)
37
+ end
38
+
39
+ # Render PREPEJ print data (PrintData, its Hash form, or JSON text) to a PDF (binary String).
40
+ def render_pdf(data, swap_pdf_image: nil)
41
+ payload = case data
42
+ when String then data
43
+ when PrintData then data.to_json
44
+ else JSON.generate(data)
45
+ end
46
+ raise ReportsWebEngineError, 'Print data exceeds 32 MiB.' if payload.bytesize > DEFAULT_MAX
47
+ req = Net::HTTP::Post.new(uri('/render/pdf'))
48
+ req['Content-Type'] = 'application/json'
49
+ req['X-Reports-Swap-Pdf-Image'] = swap_pdf_image.to_s unless swap_pdf_image.nil?
50
+ req.body = payload
51
+ response = request(req)
52
+ unless response.is_a?(Net::HTTPSuccess)
53
+ detail = begin
54
+ JSON.parse(response.body.to_s)['error']
55
+ rescue JSON::ParserError, TypeError
56
+ response.body.to_s
57
+ end
58
+ message = "Engine could not create the PDF (HTTP #{response.code})"
59
+ message += detail.to_s.empty? ? '.' : ": #{detail}"
60
+ raise ReportsWebEngineError.new(message, response.code.to_i)
61
+ end
62
+ pdf = response.body.to_s.b
63
+ raise ReportsWebEngineError, "PDF exceeds #{@max_bytes} bytes." if pdf.bytesize > @max_bytes
64
+ raise ReportsWebEngineError, 'Engine response is not a PDF.' unless pdf.start_with?('%PDF-')
65
+ pdf
66
+ end
67
+
68
+ private
69
+
70
+ def uri(path) = URI(@url + path)
71
+
72
+ def request(req)
73
+ target = req.uri
74
+ Net::HTTP.start(target.host, target.port, use_ssl: target.scheme == 'https',
75
+ open_timeout: [@timeout, 10].min, read_timeout: @timeout) { |http| http.request(req) }
76
+ rescue StandardError => e
77
+ raise ReportsWebEngineError, "Cannot reach the Reports.Web engine at #{@url}: #{e.message}"
78
+ end
79
+ end
80
+ end
@@ -1,131 +1,184 @@
1
- # frozen_string_literal: true
2
- require 'json'
3
- require 'tempfile'
4
-
5
- module PaoReportsWeb
6
- # Framework-independent builder for the shared PREPEJ print-document format.
7
- class PrintData
8
- FORMAT = 'Reports.net PrintData'
9
- VERSION = '1.0'
10
-
11
- def initialize
12
- @definition = nil
13
- @pages = []
14
- @page = nil
15
- end
16
-
17
- def set_definition(definition)
18
- raise ArgumentError, 'Cannot change the definition inside a page' if @page
19
- self.class.validate_definition!(definition)
20
- @pages.each { |page| page['Definition'] = deep_copy(@definition) if page['Definition'].nil? } if @definition
21
- @definition = deep_copy(definition)
22
- self
23
- end
24
-
25
- def page_start(definition = nil)
26
- raise ArgumentError, 'Call page_end before starting another page' if @page
27
- self.class.validate_definition!(definition) if definition
28
- raise ArgumentError, 'Set a definition first' unless definition || @definition
29
- @page = {'Index' => @pages.length + 1, 'Definition' => definition && deep_copy(definition), 'Values' => [], 'DynamicAttributes' => []}
30
- self
31
- end
32
-
33
- def set_value(name, value, index = 0, drawing = true)
34
- add_value(name, nil, value, index, drawing)
35
- end
36
-
37
- def set_repeated_value(name, value, x, y, drawing = true)
38
- add_value(name, nil, value, 0, drawing, x, y)
39
- end
40
-
41
- def set_typed_value(name, kind, value, index = 0, drawing = true)
42
- add_value(name, kind, value, index, drawing)
43
- end
44
-
45
- def set_image(name, source) = set_typed_value(name, 'DynamicImage', source)
46
- def set_barcode(name, code) = set_typed_value(name, 'DynamicBarcode', code)
47
-
48
- def change_attributes(name, values, index = 0)
49
- change_repeated_attributes(name, values, index)
50
- end
51
-
52
- def change_repeated_attributes(name, values, index = 0, x: nil, y: nil, kind: nil)
53
- page = current_page
54
- validate_indices!(index, x, y)
55
- raise ArgumentError, 'Attributes must be an object' unless values.is_a?(Hash)
56
- normalized = values.to_h { |key, value| raise(ArgumentError, 'Attribute values cannot be null') if value.nil?; [key.to_s, scalar(value)] }
57
- item = {'Name' => required(name), 'Type' => resolve_type(name, kind), 'Index' => index, 'Values' => normalized}
58
- item.merge!('IndexX' => x, 'IndexY' => y) unless x.nil?
59
- page['DynamicAttributes'] << item
60
- self
61
- end
62
-
63
- def page_end
64
- @pages << current_page
65
- @page = nil
66
- self
67
- end
68
-
69
- def to_h
70
- raise ArgumentError, 'An unfinished page exists' if @page
71
- raise ArgumentError, 'A definition is required' unless @definition
72
- raise ArgumentError, 'At least one completed page is required' if @pages.empty?
73
- deep_copy({'Format' => FORMAT, 'Version' => VERSION, 'Definition' => @definition, 'Pages' => @pages})
74
- end
75
-
76
- def to_json(pretty: false)
77
- (pretty ? JSON.pretty_generate(to_h) : JSON.generate(to_h)) + "\n"
78
- end
79
-
80
- def save(path)
81
- Tempfile.create(['.print-', '.tmp'], File.dirname(path)) do |file|
82
- file.write(to_json(pretty: true)); file.flush; file.close
83
- File.rename(file.path, path)
84
- end
85
- end
86
-
87
- def self.validate_definition!(definition)
88
- portable = definition.is_a?(Hash) && definition.key?('Version') && definition['Objects'].is_a?(Array) && %w[mm px].include?((definition['CoordinateUnit'] || 'mm').downcase)
89
- web = definition.is_a?(Hash) && definition['format'] == 'pao-reports-web' && definition['version'] == 1 && definition['pages'].is_a?(Array) && !definition['pages'].empty?
90
- raise ArgumentError, 'Invalid report definition' unless portable || web
91
- end
92
-
93
- private
94
- def add_value(name, kind, value, index, drawing, x = nil, y = nil)
95
- validate_indices!(index, x, y)
96
- item = {'Name' => required(name), 'Type' => resolve_type(name, kind), 'Index' => index, 'Drawing' => drawing, 'Value' => scalar(value)}
97
- item.merge!('IndexX' => x, 'IndexY' => y) unless x.nil?
98
- current_page['Values'] << item
99
- self
100
- end
101
- # 型省略時だけ、当該ページの定義を参照する。明示指定は保持する。
102
- def resolve_type(name, explicit)
103
- return required(explicit) unless explicit.nil?
104
- d = current_page['Definition'] || @definition
105
- objects = (d['Objects'] || []) + (d['pages'] || []).flat_map { |p| p['objects'] || [] }
106
- kinds = %w[Text Line Square Circle Image Barcode]
107
- web = {'text'=>'Text', 'line'=>'Line', 'rectangle'=>'Square', 'ellipse'=>'Circle', 'image'=>'Image', 'barcode'=>'Barcode'}
108
- objects.each do |o|
109
- next unless (o['Name'] || o['name']) == required(name)
110
- return 'Dynamic' + kinds[o['Kind'].to_i] if /\A[0-5]\z/.match?(o['Kind'].to_s)
111
- return 'Dynamic' + web[o['kind']] if web.key?(o['kind'])
112
- end
113
- 'DynamicText'
114
- end
115
- def current_page = @page || raise(ArgumentError, 'Call page_start first')
116
- def required(value) = value.to_s.strip.then { |v| v.empty? ? raise(ArgumentError, 'A name is required') : v }
117
- def scalar(value)
118
- return nil if value.nil?
119
- raise ArgumentError, 'A finite scalar is required' if value.is_a?(Float) && !value.finite?
120
- return value.dup if value.is_a?(String)
121
- return value.to_s if value.is_a?(Numeric) || value == true || value == false
122
- raise ArgumentError, 'Values must be scalar'
123
- end
124
- def deep_copy(value) = Marshal.load(Marshal.dump(value))
125
- def validate_indices!(index, x, y)
126
- valid = index.is_a?(Integer) && index >= 0 && x.nil? == y.nil?
127
- valid &&= x.is_a?(Integer) && x >= 0 && y.is_a?(Integer) && y >= 0 unless x.nil?
128
- raise ArgumentError, 'Indices must be non-negative integers; supply both x and y, or neither' unless valid
129
- end
130
- end
131
- end
1
+ # frozen_string_literal: true
2
+ require 'json'
3
+ require 'tempfile'
4
+
5
+ module PaoReportsWeb
6
+ # Framework-independent builder for the shared PREPEJ print-document format.
7
+ class PrintData
8
+ FORMAT = 'Reports.net PrintData'
9
+ VERSION = '1.0'
10
+
11
+ def initialize
12
+ @definition = nil
13
+ @pages = []
14
+ @page = nil
15
+ end
16
+
17
+ def set_definition(definition)
18
+ raise ArgumentError, 'ページを終了してから定義を変更してください。' if @page
19
+ self.class.validate_definition!(definition)
20
+ @pages.each { |page| page['Definition'] = deep_copy(@definition) if page['Definition'].nil? } if @definition
21
+ @definition = deep_copy(definition)
22
+ self
23
+ end
24
+
25
+ def page_start(definition = nil)
26
+ raise ArgumentError, '次のページを開始する前にpageEndで現在のページを終了してください。' if @page
27
+ self.class.validate_definition!(definition) if definition
28
+ raise ArgumentError, '先に帳票定義を設定してください。' unless definition || @definition
29
+ @page = {'Index' => @pages.length + 1, 'Definition' => definition && deep_copy(definition), 'Values' => [], 'DynamicAttributes' => []}
30
+ self
31
+ end
32
+
33
+ def set_value(name, value, index = 0, drawing = true)
34
+ add_value(name, nil, value, index, drawing)
35
+ end
36
+
37
+ def set_repeated_value(name, value, x, y, drawing = true)
38
+ add_value(name, nil, value, 0, drawing, x, y)
39
+ end
40
+
41
+ def set_typed_value(name, kind, value, index = 0, drawing = true)
42
+ add_value(name, kind, value, index, drawing)
43
+ end
44
+
45
+ def set_image(name, source) = set_typed_value(name, 'DynamicImage', source)
46
+ def set_barcode(name, code) = set_typed_value(name, 'DynamicBarcode', code)
47
+
48
+ def change_attributes(name, values, index = 0)
49
+ change_repeated_attributes(name, values, index)
50
+ end
51
+
52
+ def change_repeated_attributes(name, values, index = 0, x: nil, y: nil, kind: nil)
53
+ page = current_page
54
+ validate_indices!(index, x, y)
55
+ raise ArgumentError, '属性はオブジェクトで指定してください。' unless values.is_a?(Hash)
56
+ normalized = values.to_h { |key, value| raise(ArgumentError, '属性値にnullは指定できません。') if value.nil?; [key.to_s, scalar(value)] }
57
+ require_object!(name)
58
+ item = {'Name' => required(name), 'Type' => resolve_type(name, kind), 'Index' => index, 'Values' => normalized}
59
+ item.merge!('IndexX' => x, 'IndexY' => y) unless x.nil?
60
+ page['DynamicAttributes'] << item
61
+ self
62
+ end
63
+
64
+ def page_end
65
+ @pages << current_page
66
+ @page = nil
67
+ self
68
+ end
69
+
70
+ def to_h
71
+ raise ArgumentError, '終了していないページがあります。ページを終了してください。' if @page
72
+ raise ArgumentError, '先に帳票定義を設定してください。' unless @definition
73
+ raise ArgumentError, '少なくとも1ページを終了してください。' if @pages.empty?
74
+ deep_copy({'Format' => FORMAT, 'Version' => VERSION, 'Definition' => @definition, 'Pages' => @pages})
75
+ end
76
+
77
+ def to_json(pretty: false)
78
+ (pretty ? JSON.pretty_generate(to_h) : JSON.generate(to_h)) + "\n"
79
+ end
80
+
81
+ def require_writable(path)
82
+ return unless File.file?(path)
83
+ message = '読み取り専用のため保存できません。別名で保存してください。'
84
+ raise IOError, message if (File.stat(path).mode & 0o222).zero?
85
+ begin
86
+ File.open(path, File::WRONLY) { |probe| }
87
+ rescue Errno::EACCES, Errno::EPERM => error
88
+ raise IOError, message, cause: error
89
+ end
90
+ end
91
+ private :require_writable
92
+
93
+ def save(path)
94
+ require_writable(path)
95
+ Tempfile.create(['.print-', '.tmp'], File.dirname(path)) do |file|
96
+ file.write(to_json(pretty: true)); file.flush; file.close
97
+ require_writable(path)
98
+ File.rename(file.path, path)
99
+ end
100
+ end
101
+
102
+ def self.validate_definition!(definition)
103
+ portable = definition.is_a?(Hash) && definition.key?('Version') && definition['Objects'].is_a?(Array) && %w[mm px].include?((definition['CoordinateUnit'] || 'mm').downcase)
104
+ web = definition.is_a?(Hash) && definition['format'] == 'pao-reports-web' && definition['version'] == 1 && definition['pages'].is_a?(Array) && !definition['pages'].empty?
105
+ raise ArgumentError, '帳票定義の形式が不正です。' unless portable || web
106
+ end
107
+
108
+ private
109
+ def add_value(name, kind, value, index, drawing, x = nil, y = nil)
110
+ validate_indices!(index, x, y)
111
+ require_object!(name)
112
+ item = {'Name' => required(name), 'Type' => resolve_type(name, kind), 'Index' => index, 'Drawing' => drawing, 'Value' => scalar(value)}
113
+ item.merge!('IndexX' => x, 'IndexY' => y) unless x.nil?
114
+ current_page['Values'] << item
115
+ self
116
+ end
117
+ # 型省略時だけ、当該ページの定義を参照する。明示指定は保持する。
118
+ def page_objects
119
+ d = current_page['Definition'] || @definition
120
+ (d['Objects'] || []) + (d['pages'] || []).flat_map { |p| p['objects'] || [] }
121
+ end
122
+ # OP-R3-WRAP:Reports.net の Write と同じく、帳票に無い名前はその場で断る(TypeScript/Node.js 版と同じ)。
123
+ # 以前は黙って受け、その値は帳票に出なかった。
124
+ def require_object!(name)
125
+ wanted = required(name)
126
+ return if page_objects.any? { |o| (o['Name'] || o['name']) == wanted }
127
+ raise ArgumentError, "帳票にオブジェクト「#{name}」がありません。"
128
+ end
129
+ def resolve_type(name, explicit)
130
+ return required(explicit) unless explicit.nil?
131
+ objects = page_objects
132
+ kinds = %w[Text Line Square Circle Image Barcode]
133
+ web = {'text'=>'Text', 'line'=>'Line', 'rectangle'=>'Square', 'ellipse'=>'Circle', 'image'=>'Image', 'barcode'=>'Barcode'}
134
+ objects.each do |o|
135
+ next unless (o['Name'] || o['name']) == required(name)
136
+ return 'Dynamic' + kinds[o['Kind'].to_i] if /\A[0-5]\z/.match?(o['Kind'].to_s)
137
+ return 'Dynamic' + web[o['kind']] if web.key?(o['kind'])
138
+ end
139
+ 'DynamicText'
140
+ end
141
+ def current_page = @page || raise(ArgumentError, '先にページを開始してください。')
142
+ def required(value) = value.to_s.strip.then { |v| v.empty? ? raise(ArgumentError, '名前を指定してください。') : v }
143
+ def scalar(value)
144
+ return nil if value.nil?
145
+ raise ArgumentError, '値は文字列・有限の数値・真偽値・nullのいずれかで指定してください。' if value.is_a?(Float) && !value.finite?
146
+ return value.dup if value.is_a?(String)
147
+ return value.to_s if value.is_a?(Integer) || value == true || value == false
148
+ return js_number(value) if value.is_a?(Float)
149
+ # BigDecimal#to_s は 0.12345e4 の形になる(OP-R3-WRAP)。小数点の形で書く(1234.5、100)。
150
+ return value.to_s('F').sub(/\.0\z/, '') if defined?(BigDecimal) && value.is_a?(BigDecimal)
151
+ raise ArgumentError, '値は文字列・数値・真偽値・nullのいずれかで指定してください。'
152
+ end
153
+ # OP-R3-WRAP:小数は TypeScript/Node.js 版(JavaScript の数の文字列)と同じ書き方にする。
154
+ # 以前は 1.0・1000000.0・1.0e+21 と書いた。最短で元に戻る桁を使い、指数は 1e21 以上と 1e-7 未満だけ。
155
+ def js_number(value)
156
+ return '0' if value.zero?
157
+ text = value.to_s
158
+ negative = text.start_with?('-')
159
+ text = text[1..] if negative
160
+ mantissa, exponent = text.split(/e/i)
161
+ int, frac = mantissa.split('.', 2)
162
+ all = int + (frac || '')
163
+ lead = all.length - all.sub(/\A0+/, '').length
164
+ digits = all[lead..].sub(/0+\z/, '')
165
+ return '0' if digits.empty?
166
+ n = int.length - lead + exponent.to_i
167
+ k = digits.length
168
+ result = if k <= n && n <= 21 then digits + ('0' * (n - k))
169
+ elsif n.positive? && n <= 21 then "#{digits[0, n]}.#{digits[n..]}"
170
+ elsif n > -6 && n <= 0 then "0.#{'0' * -n}#{digits}"
171
+ else
172
+ e = n - 1
173
+ digits[0] + (k > 1 ? ".#{digits[1..]}" : '') + "e#{e.negative? ? '-' : '+'}#{e.abs}"
174
+ end
175
+ (negative ? '-' : '') + result
176
+ end
177
+ def deep_copy(value) = Marshal.load(Marshal.dump(value))
178
+ def validate_indices!(index, x, y)
179
+ valid = index.is_a?(Integer) && index >= 0 && x.nil? == y.nil?
180
+ valid &&= x.is_a?(Integer) && x >= 0 && y.is_a?(Integer) && y >= 0 unless x.nil?
181
+ raise ArgumentError, 'インデックスには0以上の整数を指定し、繰返し座標はxとyを両方指定してください。' unless valid
182
+ end
183
+ end
184
+ end
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: true
2
- module PaoReportsWeb
3
- GEM_VERSION = '1.0.0'
4
- end
1
+ # frozen_string_literal: true
2
+ module PaoReportsWeb
3
+ GEM_VERSION = '1.3.0'
4
+ end
@@ -1,2 +1,2 @@
1
- # frozen_string_literal: true
2
- require_relative 'reports_web'
1
+ # frozen_string_literal: true
2
+ require_relative 'reports_web'
data/lib/reports_web.rb CHANGED
@@ -1,6 +1,6 @@
1
- # frozen_string_literal: true
2
- # Reports.Web for Ruby: PaoReportsWeb::PrintData builds PREPEJ print data,
3
- # PaoReportsWeb::ReportsWebEngine renders it to PDF with the Reports.Web engine.
4
- require_relative 'pao_reports_web/version'
5
- require_relative 'pao_reports_web/print_data'
6
- require_relative 'pao_reports_web/engine'
1
+ # frozen_string_literal: true
2
+ # Reports.Web for Ruby: PaoReportsWeb::PrintData builds PREPEJ print data,
3
+ # PaoReportsWeb::ReportsWebEngine renders it to PDF with the Reports.Web engine.
4
+ require_relative 'pao_reports_web/version'
5
+ require_relative 'pao_reports_web/print_data'
6
+ require_relative 'pao_reports_web/engine'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reports_web
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pao@Office
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-10-02 00:00:00.000000000 Z
11
+ date: 2026-11-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'Reports.Web is a report designer and PDF engine for Japanese business
14
14
  forms (invoices, estimates, delivery slips, labels, business cards). This gem builds