ironhide_html_to_pdf 1.0.0-x86_64-linux
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/LICENSE-COMMERCIAL.md +70 -0
- data/LICENSE-PERSONAL.md +79 -0
- data/NOTICE.txt +267 -0
- data/README.md +30 -0
- data/SOURCES.md +231 -0
- data/lib/ironhide/html_to_pdf.rb +620 -0
- data/lib/ironhide/native/x86_64-linux/libhtml_to_pdf.so +0 -0
- metadata +52 -0
|
@@ -0,0 +1,620 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "base64"
|
|
4
|
+
require "fiddle"
|
|
5
|
+
require "json"
|
|
6
|
+
require "rbconfig"
|
|
7
|
+
|
|
8
|
+
module Ironhide
|
|
9
|
+
module HtmlToPdf
|
|
10
|
+
REQUEST_SCHEMA = "h2p-ffi-request/1"
|
|
11
|
+
SUPPLY_SCHEMA = "h2p-ffi-supply/1"
|
|
12
|
+
|
|
13
|
+
PAPER_SIZES = {
|
|
14
|
+
"a3" => [841_890, 1_190_551],
|
|
15
|
+
"a4" => [595_440, 841_680],
|
|
16
|
+
"a5" => [419_528, 595_276],
|
|
17
|
+
"letter" => [612_000, 792_000],
|
|
18
|
+
"legal" => [612_000, 1_008_000],
|
|
19
|
+
}.freeze
|
|
20
|
+
|
|
21
|
+
module Status
|
|
22
|
+
OK = 0
|
|
23
|
+
INVALID_ARGUMENT = 1
|
|
24
|
+
ILLEGAL_TRANSITION = 2
|
|
25
|
+
MISMATCHED_RESPONSE = 3
|
|
26
|
+
LIMIT_EXCEEDED = 4
|
|
27
|
+
RESOURCE_UNAVAILABLE = 5
|
|
28
|
+
SINK_FAILED = 6
|
|
29
|
+
CANCELLED = 7
|
|
30
|
+
INTERNAL = 8
|
|
31
|
+
UNSUPPORTED_FEATURE = 9
|
|
32
|
+
BUFFER_TOO_SMALL = 10
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
module Phase
|
|
36
|
+
READY = 0
|
|
37
|
+
AWAITING_RESOURCE = 1
|
|
38
|
+
AWAITING_DRAIN = 2
|
|
39
|
+
COMPLETE = 3
|
|
40
|
+
FINISHED = 4
|
|
41
|
+
CANCELLED = 5
|
|
42
|
+
FAILED = 6
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
RenderResult = Struct.new(:pdf, :report, keyword_init: true)
|
|
46
|
+
ResourceRequest = Struct.new(:sequence, :url, :kind, keyword_init: true)
|
|
47
|
+
DrainChunk = Struct.new(:bytes, :final, keyword_init: true) do
|
|
48
|
+
alias_method :final?, :final
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
class Error < StandardError
|
|
52
|
+
attr_reader :status, :code, :diagnostics, :raw_json
|
|
53
|
+
|
|
54
|
+
def initialize(status:, code:, message:, diagnostics: [], raw_json: "")
|
|
55
|
+
super("html_to_pdf (#{code}, status=#{status}): #{message}")
|
|
56
|
+
@status = status
|
|
57
|
+
@code = code
|
|
58
|
+
@diagnostics = diagnostics.freeze
|
|
59
|
+
@raw_json = raw_json
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def self.from_native(status, raw_bytes)
|
|
63
|
+
raw = raw_bytes && !raw_bytes.empty? ? raw_bytes.dup.force_encoding(Encoding::UTF_8) : ""
|
|
64
|
+
unless raw.empty?
|
|
65
|
+
begin
|
|
66
|
+
parsed = JSON.parse(raw)
|
|
67
|
+
return new(
|
|
68
|
+
status: status,
|
|
69
|
+
code: (parsed["code"] || "status_#{status}").to_s,
|
|
70
|
+
message: (parsed["message"] || raw).to_s,
|
|
71
|
+
diagnostics: parsed["diagnostics"] || [],
|
|
72
|
+
raw_json: raw
|
|
73
|
+
)
|
|
74
|
+
rescue JSON::ParserError
|
|
75
|
+
# Fall through with raw text.
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
new(
|
|
79
|
+
status: status,
|
|
80
|
+
code: "status_#{status}",
|
|
81
|
+
message: raw.empty? ? "C ABI error status=#{status}" : raw,
|
|
82
|
+
raw_json: raw
|
|
83
|
+
)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
module Native
|
|
88
|
+
U32 = Fiddle::TYPE_UINT32_T
|
|
89
|
+
U64 = Fiddle::TYPE_UINT64_T
|
|
90
|
+
PTR = Fiddle::TYPE_VOIDP
|
|
91
|
+
|
|
92
|
+
@mutex = Mutex.new
|
|
93
|
+
@handle = nil
|
|
94
|
+
@funcs = nil
|
|
95
|
+
|
|
96
|
+
class << self
|
|
97
|
+
def library_candidates
|
|
98
|
+
candidates = []
|
|
99
|
+
env = ENV["IRONHIDE_HTML_TO_PDF_LIB"]
|
|
100
|
+
candidates << env if env && !env.empty?
|
|
101
|
+
|
|
102
|
+
host_os = RbConfig::CONFIG["host_os"].to_s
|
|
103
|
+
host_cpu = RbConfig::CONFIG["host_cpu"].to_s
|
|
104
|
+
|
|
105
|
+
os_name, lib_name = case host_os
|
|
106
|
+
when /darwin/i then ["darwin", "libhtml_to_pdf.dylib"]
|
|
107
|
+
when /mswin|mingw|cygwin/i then ["windows", "html_to_pdf.dll"]
|
|
108
|
+
else ["linux", "libhtml_to_pdf.so"]
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
arch_name = case host_cpu
|
|
112
|
+
when /aarch64|arm64/i then "aarch64"
|
|
113
|
+
when /x86_64|x64|amd64/i then "x86_64"
|
|
114
|
+
else host_cpu
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
arch_os = "#{arch_name}-#{os_name}"
|
|
118
|
+
candidates << File.expand_path("native/#{arch_os}/#{lib_name}", __dir__)
|
|
119
|
+
if os_name == "darwin" && arch_name == "aarch64"
|
|
120
|
+
candidates << File.expand_path("native/arm64-darwin/#{lib_name}", __dir__)
|
|
121
|
+
end
|
|
122
|
+
candidates << File.expand_path("native/#{lib_name}", __dir__)
|
|
123
|
+
|
|
124
|
+
repo_root = File.expand_path("../../../..", __dir__)
|
|
125
|
+
candidates << File.join(repo_root, "target", "release", lib_name)
|
|
126
|
+
candidates << File.join(repo_root, "target", "debug", lib_name)
|
|
127
|
+
candidates << lib_name
|
|
128
|
+
candidates
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def load!
|
|
132
|
+
return @funcs if @funcs
|
|
133
|
+
|
|
134
|
+
@mutex.synchronize do
|
|
135
|
+
return @funcs if @funcs
|
|
136
|
+
|
|
137
|
+
last_err = nil
|
|
138
|
+
library_candidates.each do |path|
|
|
139
|
+
next if path.include?(File::SEPARATOR) && !File.exist?(path)
|
|
140
|
+
|
|
141
|
+
begin
|
|
142
|
+
@handle = Fiddle.dlopen(path)
|
|
143
|
+
break
|
|
144
|
+
rescue Fiddle::DLError => e
|
|
145
|
+
last_err = e
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
raise LoadError, "Could not load libhtml_to_pdf: #{last_err}" unless @handle
|
|
149
|
+
|
|
150
|
+
@funcs = {
|
|
151
|
+
abi_version: fn("h2p_abi_version", [], U32),
|
|
152
|
+
product_version: fn("h2p_product_version", [PTR, PTR], U32),
|
|
153
|
+
session_create: fn("h2p_session_create", [PTR, U64, PTR], U32),
|
|
154
|
+
session_phase: fn("h2p_session_phase", [PTR, PTR], U32),
|
|
155
|
+
session_step: fn("h2p_session_step", [PTR, PTR], U32),
|
|
156
|
+
session_pending_resource_request: fn("h2p_session_pending_resource_request", [PTR, PTR], U32),
|
|
157
|
+
session_supply: fn("h2p_session_supply", [PTR, PTR, U64], U32),
|
|
158
|
+
session_drain: fn("h2p_session_drain", [PTR, PTR, U64, PTR, PTR], U32),
|
|
159
|
+
session_drain_fail: fn("h2p_session_drain_fail", [PTR, PTR, U64], U32),
|
|
160
|
+
session_cancel: fn("h2p_session_cancel", [PTR, U32], U32),
|
|
161
|
+
session_finish: fn("h2p_session_finish", [PTR, PTR], U32),
|
|
162
|
+
session_last_error: fn("h2p_session_last_error", [PTR, PTR], U32),
|
|
163
|
+
session_free: fn("h2p_session_free", [PTR], U32),
|
|
164
|
+
render_preloaded: fn("h2p_render_preloaded", [PTR, U64, PTR, PTR, PTR], U32),
|
|
165
|
+
buffer_data: fn("h2p_buffer_data", [PTR], PTR),
|
|
166
|
+
buffer_len: fn("h2p_buffer_len", [PTR], U64),
|
|
167
|
+
buffer_free: fn("h2p_buffer_free", [PTR], U32),
|
|
168
|
+
}
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def fn(name, args, ret)
|
|
173
|
+
Fiddle::Function.new(@handle[name], args, ret)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def take_buffer(buf_ptr)
|
|
177
|
+
return "".b if buf_ptr.nil? || buf_ptr.null?
|
|
178
|
+
|
|
179
|
+
funcs = load!
|
|
180
|
+
begin
|
|
181
|
+
len = funcs[:buffer_len].call(buf_ptr)
|
|
182
|
+
return "".b if len == 0
|
|
183
|
+
|
|
184
|
+
data_ptr = funcs[:buffer_data].call(buf_ptr)
|
|
185
|
+
return "".b if data_ptr.nil? || data_ptr.null?
|
|
186
|
+
|
|
187
|
+
data_ptr[0, len].b
|
|
188
|
+
ensure
|
|
189
|
+
funcs[:buffer_free].call(buf_ptr)
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def ptr_out
|
|
194
|
+
Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP, Fiddle::RUBY_FREE)
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def read_ptr(slot)
|
|
198
|
+
addr = slot[0, Fiddle::SIZEOF_VOIDP].unpack1(Fiddle::SIZEOF_VOIDP == 8 ? "Q" : "L")
|
|
199
|
+
Fiddle::Pointer.new(addr)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def u32_out
|
|
203
|
+
Fiddle::Pointer.malloc(4, Fiddle::RUBY_FREE)
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def read_u32(slot)
|
|
207
|
+
slot[0, 4].unpack1("L")
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def u64_out
|
|
211
|
+
Fiddle::Pointer.malloc(8, Fiddle::RUBY_FREE)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def read_u64(slot)
|
|
215
|
+
slot[0, 8].unpack1("Q")
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def session_finalizer(addr)
|
|
219
|
+
proc do
|
|
220
|
+
if addr != 0
|
|
221
|
+
funcs = load!
|
|
222
|
+
funcs[:session_free].call(Fiddle::Pointer.new(addr))
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def self.serialize_request(document:, input_mode: "preloaded", environment: nil, limits: nil, resources: nil, font_faces: nil)
|
|
230
|
+
payload = {
|
|
231
|
+
"schema" => REQUEST_SCHEMA,
|
|
232
|
+
"input_mode" => input_mode.to_s,
|
|
233
|
+
}
|
|
234
|
+
if document.encoding == Encoding::BINARY
|
|
235
|
+
payload["document_base64"] = Base64.strict_encode64(document)
|
|
236
|
+
else
|
|
237
|
+
payload["document_utf8"] = document.encode(Encoding::UTF_8)
|
|
238
|
+
end
|
|
239
|
+
payload["environment"] = stringify_keys(environment) if environment && !environment.empty?
|
|
240
|
+
payload["limits"] = stringify_keys(limits) if limits && !limits.empty?
|
|
241
|
+
if resources && !resources.empty?
|
|
242
|
+
payload["resources"] = resources.map do |r|
|
|
243
|
+
entry = stringify_keys(r)
|
|
244
|
+
if entry.key?("data")
|
|
245
|
+
raw = entry.delete("data")
|
|
246
|
+
entry["data_base64"] = Base64.strict_encode64(raw.b) unless raw.nil?
|
|
247
|
+
end
|
|
248
|
+
entry
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
if font_faces && !font_faces.empty?
|
|
252
|
+
payload["font_faces"] = font_faces.map { |f| stringify_keys(f) }
|
|
253
|
+
end
|
|
254
|
+
JSON.generate(payload).b
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def self.stringify_keys(hash)
|
|
258
|
+
hash.each_with_object({}) do |(k, v), acc|
|
|
259
|
+
acc[k.to_s] = v unless v.nil?
|
|
260
|
+
end
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def self.abi_version
|
|
264
|
+
Native.load![:abi_version].call
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def self.product_version
|
|
268
|
+
funcs = Native.load!
|
|
269
|
+
out_ptr = Native.ptr_out
|
|
270
|
+
out_len = Native.u64_out
|
|
271
|
+
st = funcs[:product_version].call(out_ptr, out_len)
|
|
272
|
+
raise Error.new(status: st, code: "version_failed", message: "h2p_product_version failed") if st != Status::OK
|
|
273
|
+
|
|
274
|
+
ptr = Native.read_ptr(out_ptr)
|
|
275
|
+
len = Native.read_u64(out_len)
|
|
276
|
+
ptr[0, len].force_encoding(Encoding::UTF_8)
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
def self.render_preloaded(document:, environment: nil, limits: nil, resources: nil, font_faces: nil)
|
|
280
|
+
funcs = Native.load!
|
|
281
|
+
wire = serialize_request(
|
|
282
|
+
document: document,
|
|
283
|
+
input_mode: "preloaded",
|
|
284
|
+
environment: environment,
|
|
285
|
+
limits: limits,
|
|
286
|
+
resources: resources,
|
|
287
|
+
font_faces: font_faces
|
|
288
|
+
)
|
|
289
|
+
out_pdf = Native.ptr_out
|
|
290
|
+
out_report = Native.ptr_out
|
|
291
|
+
out_err = Native.ptr_out
|
|
292
|
+
st = funcs[:render_preloaded].call(Fiddle::Pointer[wire], wire.bytesize, out_pdf, out_report, out_err)
|
|
293
|
+
pdf = Native.take_buffer(Native.read_ptr(out_pdf))
|
|
294
|
+
report_bytes = Native.take_buffer(Native.read_ptr(out_report))
|
|
295
|
+
err_bytes = Native.take_buffer(Native.read_ptr(out_err))
|
|
296
|
+
raise Error.from_native(st, err_bytes) if st != Status::OK
|
|
297
|
+
|
|
298
|
+
RenderResult.new(pdf: pdf, report: JSON.parse(report_bytes.force_encoding(Encoding::UTF_8)))
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def self.detect_font_mime(bytes, path = nil)
|
|
302
|
+
magic = bytes && bytes.bytesize >= 4 ? bytes.byteslice(0, 4).b : "".b
|
|
303
|
+
lower_path = path ? path.to_s.downcase : nil
|
|
304
|
+
if magic == "OTTO".b || (lower_path && lower_path.end_with?(".otf"))
|
|
305
|
+
"font/otf"
|
|
306
|
+
elsif magic == "wOF2".b || (lower_path && lower_path.end_with?(".woff2"))
|
|
307
|
+
"font/woff2"
|
|
308
|
+
elsif magic == "wOFF".b || (lower_path && lower_path.end_with?(".woff"))
|
|
309
|
+
"font/woff"
|
|
310
|
+
else
|
|
311
|
+
"font/ttf"
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
def self.font_ext_for_mime(mime)
|
|
316
|
+
case mime
|
|
317
|
+
when "font/otf" then "otf"
|
|
318
|
+
when "font/woff2" then "woff2"
|
|
319
|
+
when "font/woff" then "woff"
|
|
320
|
+
else "ttf"
|
|
321
|
+
end
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def self.render(
|
|
325
|
+
html,
|
|
326
|
+
paper: "a4",
|
|
327
|
+
media: "print",
|
|
328
|
+
print_backgrounds: false,
|
|
329
|
+
tagged: false,
|
|
330
|
+
document_outline: false,
|
|
331
|
+
fonts: [],
|
|
332
|
+
resources: []
|
|
333
|
+
)
|
|
334
|
+
if html.nil?
|
|
335
|
+
raise Error.new(
|
|
336
|
+
status: Status::INVALID_ARGUMENT,
|
|
337
|
+
code: "invalid_html",
|
|
338
|
+
message: "html must not be nil"
|
|
339
|
+
)
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
paper_key = (paper.nil? || paper.to_s.empty?) ? "a4" : paper.to_s.downcase
|
|
343
|
+
dims = PAPER_SIZES[paper_key]
|
|
344
|
+
unless dims
|
|
345
|
+
raise Error.new(
|
|
346
|
+
status: Status::INVALID_ARGUMENT,
|
|
347
|
+
code: "invalid_paper",
|
|
348
|
+
message: "unknown paper size '#{paper}' (expected a3, a4, a5, letter, or legal)"
|
|
349
|
+
)
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
environment = {
|
|
353
|
+
paper_width_milli_points: dims[0],
|
|
354
|
+
paper_height_milli_points: dims[1],
|
|
355
|
+
media_type: (media.nil? || media.to_s.empty?) ? "print" : media.to_s,
|
|
356
|
+
print_backgrounds: !!print_backgrounds,
|
|
357
|
+
}
|
|
358
|
+
environment[:tagging_mode] = "tagged" if tagged
|
|
359
|
+
environment[:document_outline] = true if document_outline
|
|
360
|
+
|
|
361
|
+
all_resources = []
|
|
362
|
+
font_faces = []
|
|
363
|
+
|
|
364
|
+
fonts.each_with_index do |font, idx|
|
|
365
|
+
family = font[:family] || font["family"]
|
|
366
|
+
path = font[:path] || font["path"]
|
|
367
|
+
data = font[:data] || font["data"]
|
|
368
|
+
if family.nil? || family.to_s.empty?
|
|
369
|
+
raise Error.new(
|
|
370
|
+
status: Status::INVALID_ARGUMENT,
|
|
371
|
+
code: "invalid_font",
|
|
372
|
+
message: "fonts[#{idx}][:family] must be a non-empty string"
|
|
373
|
+
)
|
|
374
|
+
end
|
|
375
|
+
data = File.binread(path) if data.nil? && path
|
|
376
|
+
if data.nil?
|
|
377
|
+
raise Error.new(
|
|
378
|
+
status: Status::INVALID_ARGUMENT,
|
|
379
|
+
code: "invalid_font",
|
|
380
|
+
message: "fonts[#{idx}] must provide either :path or :data"
|
|
381
|
+
)
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
mime = detect_font_mime(data, path)
|
|
385
|
+
ext = font_ext_for_mime(mime)
|
|
386
|
+
url = "https://ironhide.local/fonts/font-#{idx}.#{ext}"
|
|
387
|
+
all_resources << {
|
|
388
|
+
url: url,
|
|
389
|
+
kind: "font",
|
|
390
|
+
mime_type: mime,
|
|
391
|
+
requirement: "required",
|
|
392
|
+
data: data,
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
raw_weight = font.key?(:weight) ? font[:weight] : font["weight"]
|
|
396
|
+
weight = (raw_weight.nil? || raw_weight.to_i <= 0) ? 400 : raw_weight.to_i
|
|
397
|
+
raw_style = font.key?(:style) ? font[:style] : font["style"]
|
|
398
|
+
style = (raw_style.nil? || raw_style.to_s.strip.empty?) ? "normal" : raw_style.to_s
|
|
399
|
+
generics = font[:generic_families] || font["generic_families"]
|
|
400
|
+
generics = ["sans-serif"] if generics.nil? || generics.empty?
|
|
401
|
+
font_faces << {
|
|
402
|
+
resource_url: url,
|
|
403
|
+
family: family.to_s,
|
|
404
|
+
style: style,
|
|
405
|
+
weight: weight,
|
|
406
|
+
generic_families: generics,
|
|
407
|
+
face_index: 0,
|
|
408
|
+
}
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
all_resources.concat(resources) if resources && !resources.empty?
|
|
412
|
+
|
|
413
|
+
render_preloaded(
|
|
414
|
+
document: html,
|
|
415
|
+
environment: environment,
|
|
416
|
+
resources: all_resources,
|
|
417
|
+
font_faces: font_faces
|
|
418
|
+
)
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
class Session
|
|
422
|
+
def self.open(**kwargs)
|
|
423
|
+
session = new(**kwargs)
|
|
424
|
+
return session unless block_given?
|
|
425
|
+
|
|
426
|
+
begin
|
|
427
|
+
yield session
|
|
428
|
+
ensure
|
|
429
|
+
session.close
|
|
430
|
+
end
|
|
431
|
+
end
|
|
432
|
+
|
|
433
|
+
def initialize(document:, input_mode: "preloaded", environment: nil, limits: nil, resources: nil, font_faces: nil)
|
|
434
|
+
@funcs = Native.load!
|
|
435
|
+
@handle = nil
|
|
436
|
+
wire = HtmlToPdf.serialize_request(
|
|
437
|
+
document: document,
|
|
438
|
+
input_mode: input_mode,
|
|
439
|
+
environment: environment,
|
|
440
|
+
limits: limits,
|
|
441
|
+
resources: resources,
|
|
442
|
+
font_faces: font_faces
|
|
443
|
+
)
|
|
444
|
+
out_session = Native.ptr_out
|
|
445
|
+
st = @funcs[:session_create].call(Fiddle::Pointer[wire], wire.bytesize, out_session)
|
|
446
|
+
ptr = Native.read_ptr(out_session)
|
|
447
|
+
raise Error.from_native(st, "".b) if st != Status::OK || ptr.null?
|
|
448
|
+
|
|
449
|
+
@handle = ptr
|
|
450
|
+
ObjectSpace.define_finalizer(self, Native.session_finalizer(ptr.to_i))
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
def close
|
|
454
|
+
return unless @handle
|
|
455
|
+
|
|
456
|
+
h = @handle
|
|
457
|
+
@handle = nil
|
|
458
|
+
ObjectSpace.undefine_finalizer(self)
|
|
459
|
+
@funcs[:session_free].call(h)
|
|
460
|
+
nil
|
|
461
|
+
end
|
|
462
|
+
|
|
463
|
+
def closed?
|
|
464
|
+
@handle.nil?
|
|
465
|
+
end
|
|
466
|
+
|
|
467
|
+
def phase
|
|
468
|
+
h = require_handle!
|
|
469
|
+
out = Native.u32_out
|
|
470
|
+
check!(h, @funcs[:session_phase].call(h, out))
|
|
471
|
+
Native.read_u32(out)
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
def step
|
|
475
|
+
h = require_handle!
|
|
476
|
+
out = Native.u32_out
|
|
477
|
+
check!(h, @funcs[:session_step].call(h, out))
|
|
478
|
+
Native.read_u32(out)
|
|
479
|
+
end
|
|
480
|
+
|
|
481
|
+
def pending_resource_request
|
|
482
|
+
h = require_handle!
|
|
483
|
+
out = Native.ptr_out
|
|
484
|
+
check!(h, @funcs[:session_pending_resource_request].call(h, out))
|
|
485
|
+
parsed = JSON.parse(Native.take_buffer(Native.read_ptr(out)).force_encoding(Encoding::UTF_8))
|
|
486
|
+
ResourceRequest.new(sequence: parsed["sequence"], url: parsed["url"], kind: parsed["kind"])
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
def supply(sequence:, url:, data: nil, mime_type: nil, content_type_options_nosniff: false, final_url: nil, failed: false, error_code: nil, error_message: nil)
|
|
490
|
+
h = require_handle!
|
|
491
|
+
payload = {
|
|
492
|
+
"schema" => SUPPLY_SCHEMA,
|
|
493
|
+
"sequence" => sequence,
|
|
494
|
+
"url" => url,
|
|
495
|
+
"status" => failed ? "failed" : "supplied",
|
|
496
|
+
}
|
|
497
|
+
if failed
|
|
498
|
+
payload["error"] = error_code unless error_code.nil?
|
|
499
|
+
payload["message"] = error_message unless error_message.nil?
|
|
500
|
+
else
|
|
501
|
+
payload["mime_type"] = mime_type unless mime_type.nil?
|
|
502
|
+
payload["content_type_options_nosniff"] = true if content_type_options_nosniff
|
|
503
|
+
payload["final_url"] = final_url unless final_url.nil?
|
|
504
|
+
payload["data_base64"] = Base64.strict_encode64(data.b) unless data.nil?
|
|
505
|
+
end
|
|
506
|
+
wire = JSON.generate(payload).b
|
|
507
|
+
check!(h, @funcs[:session_supply].call(h, Fiddle::Pointer[wire], wire.bytesize))
|
|
508
|
+
nil
|
|
509
|
+
end
|
|
510
|
+
|
|
511
|
+
def drain
|
|
512
|
+
h = require_handle!
|
|
513
|
+
needed_slot = Native.u64_out
|
|
514
|
+
final_slot = Native.u32_out
|
|
515
|
+
st = @funcs[:session_drain].call(h, nil, 0, needed_slot, final_slot)
|
|
516
|
+
return DrainChunk.new(bytes: "".b, final: Native.read_u32(final_slot) != 0) if st == Status::OK
|
|
517
|
+
|
|
518
|
+
check!(h, st) unless st == Status::BUFFER_TOO_SMALL
|
|
519
|
+
needed = Native.read_u64(needed_slot)
|
|
520
|
+
buf = Fiddle::Pointer.malloc(needed, Fiddle::RUBY_FREE)
|
|
521
|
+
written_slot = Native.u64_out
|
|
522
|
+
check!(h, @funcs[:session_drain].call(h, buf, needed, written_slot, final_slot))
|
|
523
|
+
written = Native.read_u64(written_slot)
|
|
524
|
+
DrainChunk.new(bytes: buf[0, written].b, final: Native.read_u32(final_slot) != 0)
|
|
525
|
+
end
|
|
526
|
+
|
|
527
|
+
def drain_fail(message)
|
|
528
|
+
h = require_handle!
|
|
529
|
+
wire = message.to_s.encode(Encoding::UTF_8).b
|
|
530
|
+
check!(h, @funcs[:session_drain_fail].call(h, Fiddle::Pointer[wire], wire.bytesize))
|
|
531
|
+
nil
|
|
532
|
+
end
|
|
533
|
+
|
|
534
|
+
def cancel(reason = 0)
|
|
535
|
+
h = require_handle!
|
|
536
|
+
check!(h, @funcs[:session_cancel].call(h, reason))
|
|
537
|
+
nil
|
|
538
|
+
end
|
|
539
|
+
|
|
540
|
+
def finish
|
|
541
|
+
h = require_handle!
|
|
542
|
+
out = Native.ptr_out
|
|
543
|
+
check!(h, @funcs[:session_finish].call(h, out))
|
|
544
|
+
JSON.parse(Native.take_buffer(Native.read_ptr(out)).force_encoding(Encoding::UTF_8))
|
|
545
|
+
end
|
|
546
|
+
|
|
547
|
+
def drive_to_stream(io, &on_resource)
|
|
548
|
+
loop do
|
|
549
|
+
case phase
|
|
550
|
+
when Phase::READY
|
|
551
|
+
step
|
|
552
|
+
when Phase::AWAITING_RESOURCE
|
|
553
|
+
unless on_resource
|
|
554
|
+
raise Error.new(
|
|
555
|
+
status: Status::RESOURCE_UNAVAILABLE,
|
|
556
|
+
code: "resource_handler_missing",
|
|
557
|
+
message: "session requested a resource but no block was provided"
|
|
558
|
+
)
|
|
559
|
+
end
|
|
560
|
+
req = pending_resource_request
|
|
561
|
+
ans = on_resource.call(req)
|
|
562
|
+
supply(**ans)
|
|
563
|
+
when Phase::AWAITING_DRAIN
|
|
564
|
+
chunk = drain
|
|
565
|
+
unless chunk.bytes.empty?
|
|
566
|
+
begin
|
|
567
|
+
io.write(chunk.bytes)
|
|
568
|
+
rescue StandardError => e
|
|
569
|
+
begin
|
|
570
|
+
if phase == Phase::AWAITING_DRAIN
|
|
571
|
+
drain_fail(e.message)
|
|
572
|
+
else
|
|
573
|
+
cancel
|
|
574
|
+
end
|
|
575
|
+
rescue Error
|
|
576
|
+
# Session already transitioned to FAILED/CANCELLED.
|
|
577
|
+
end
|
|
578
|
+
raise Error.new(status: Status::SINK_FAILED, code: "sink_failed", message: e.message)
|
|
579
|
+
end
|
|
580
|
+
end
|
|
581
|
+
when Phase::COMPLETE
|
|
582
|
+
return finish
|
|
583
|
+
when Phase::CANCELLED
|
|
584
|
+
raise Error.from_native(Status::CANCELLED, last_error_bytes(require_handle!))
|
|
585
|
+
when Phase::FAILED
|
|
586
|
+
raise Error.from_native(Status::INTERNAL, last_error_bytes(require_handle!))
|
|
587
|
+
else
|
|
588
|
+
raise Error.new(
|
|
589
|
+
status: Status::ILLEGAL_TRANSITION,
|
|
590
|
+
code: "illegal_transition",
|
|
591
|
+
message: "session is in terminal phase"
|
|
592
|
+
)
|
|
593
|
+
end
|
|
594
|
+
end
|
|
595
|
+
end
|
|
596
|
+
|
|
597
|
+
private
|
|
598
|
+
|
|
599
|
+
def require_handle!
|
|
600
|
+
return @handle if @handle
|
|
601
|
+
|
|
602
|
+
raise Error.new(status: Status::INVALID_ARGUMENT, code: "session_closed", message: "session is closed")
|
|
603
|
+
end
|
|
604
|
+
|
|
605
|
+
def last_error_bytes(handle)
|
|
606
|
+
out = Native.ptr_out
|
|
607
|
+
st = @funcs[:session_last_error].call(handle, out)
|
|
608
|
+
return "".b if st != Status::OK
|
|
609
|
+
|
|
610
|
+
Native.take_buffer(Native.read_ptr(out))
|
|
611
|
+
end
|
|
612
|
+
|
|
613
|
+
def check!(handle, status)
|
|
614
|
+
return if status == Status::OK
|
|
615
|
+
|
|
616
|
+
raise Error.from_native(status, last_error_bytes(handle))
|
|
617
|
+
end
|
|
618
|
+
end
|
|
619
|
+
end
|
|
620
|
+
end
|
|
Binary file
|
metadata
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: ironhide_html_to_pdf
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: x86_64-linux
|
|
6
|
+
authors:
|
|
7
|
+
- Oxbloom Software LLC
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-11-28 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Zero-compilation Ruby SDK over the Ironhide libhtml_to_pdf C ABI v1.
|
|
14
|
+
email:
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- LICENSE-COMMERCIAL.md
|
|
20
|
+
- LICENSE-PERSONAL.md
|
|
21
|
+
- NOTICE.txt
|
|
22
|
+
- README.md
|
|
23
|
+
- SOURCES.md
|
|
24
|
+
- lib/ironhide/html_to_pdf.rb
|
|
25
|
+
- lib/ironhide/native/x86_64-linux/libhtml_to_pdf.so
|
|
26
|
+
homepage: https://ironhidepdf.com
|
|
27
|
+
licenses:
|
|
28
|
+
- Nonstandard
|
|
29
|
+
metadata:
|
|
30
|
+
homepage_uri: https://ironhidepdf.com
|
|
31
|
+
source_code_uri: https://gitlab.com/oxbloom/ironhide-releases
|
|
32
|
+
documentation_uri: https://ironhidepdf.com/docs
|
|
33
|
+
post_install_message:
|
|
34
|
+
rdoc_options: []
|
|
35
|
+
require_paths:
|
|
36
|
+
- lib
|
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '3.1'
|
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
requirements: []
|
|
48
|
+
rubygems_version: 3.4.20
|
|
49
|
+
signing_key:
|
|
50
|
+
specification_version: 4
|
|
51
|
+
summary: Deterministic HTML-to-PDF renderer for Ruby (Fiddle over C ABI v1)
|
|
52
|
+
test_files: []
|