oicana 0.10.0.rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.md +131 -0
- data/README.md +40 -0
- data/lib/oicana/errors.rb +62 -0
- data/lib/oicana/native.rb +71 -0
- data/lib/oicana/oicana_uniffi.rb +2037 -0
- data/lib/oicana/template.rb +213 -0
- data/lib/oicana/types.rb +159 -0
- data/lib/oicana/version.rb +5 -0
- data/lib/oicana.rb +60 -0
- metadata +66 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 662a0cf75e712bac0ebaa8c29e469b3ec493c30538b5a8a2e00f2d874891c5a4
|
|
4
|
+
data.tar.gz: 4232490425ad9ab79340f413fba01adb28087d523c9e7284f1e6c29d3ec5ad91
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 15549c67a07e494f283b99f89688a04131fac6d8f789b640eec0ee6038c6fd5c79d4b9f2a42315f1c60958d02c00d0d8498d8b900a784904de6724335dfb7fa9
|
|
7
|
+
data.tar.gz: 0a7d2dcb343c1ba1409b94408177f21820ac7b4e662e212e326eaefe70ed83a06db32520c14f8cca7b94e2943b38364c670fa4e0b74f5dca6534efaf4ad152af
|
data/LICENSE.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# PolyForm Noncommercial License 1.0.0
|
|
2
|
+
|
|
3
|
+
<https://polyformproject.org/licenses/noncommercial/1.0.0>
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
In order to get any license under these terms, you must agree
|
|
8
|
+
to them as both strict obligations and conditions to all
|
|
9
|
+
your licenses.
|
|
10
|
+
|
|
11
|
+
## Copyright License
|
|
12
|
+
|
|
13
|
+
The licensor grants you a copyright license for the
|
|
14
|
+
software to do everything you might do with the software
|
|
15
|
+
that would otherwise infringe the licensor's copyright
|
|
16
|
+
in it for any permitted purpose. However, you may
|
|
17
|
+
only distribute the software according to [Distribution
|
|
18
|
+
License](#distribution-license) and make changes or new works
|
|
19
|
+
based on the software according to [Changes and New Works
|
|
20
|
+
License](#changes-and-new-works-license).
|
|
21
|
+
|
|
22
|
+
## Distribution License
|
|
23
|
+
|
|
24
|
+
The licensor grants you an additional copyright license
|
|
25
|
+
to distribute copies of the software. Your license
|
|
26
|
+
to distribute covers distributing the software with
|
|
27
|
+
changes and new works permitted by [Changes and New Works
|
|
28
|
+
License](#changes-and-new-works-license).
|
|
29
|
+
|
|
30
|
+
## Notices
|
|
31
|
+
|
|
32
|
+
You must ensure that anyone who gets a copy of any part of
|
|
33
|
+
the software from you also gets a copy of these terms or the
|
|
34
|
+
URL for them above, as well as copies of any plain-text lines
|
|
35
|
+
beginning with `Required Notice:` that the licensor provided
|
|
36
|
+
with the software. For example:
|
|
37
|
+
|
|
38
|
+
> Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
|
|
39
|
+
|
|
40
|
+
## Changes and New Works License
|
|
41
|
+
|
|
42
|
+
The licensor grants you an additional copyright license to
|
|
43
|
+
make changes and new works based on the software for any
|
|
44
|
+
permitted purpose.
|
|
45
|
+
|
|
46
|
+
## Patent License
|
|
47
|
+
|
|
48
|
+
The licensor grants you a patent license for the software that
|
|
49
|
+
covers patent claims the licensor can license, or becomes able
|
|
50
|
+
to license, that you would infringe by using the software.
|
|
51
|
+
|
|
52
|
+
## Noncommercial Purposes
|
|
53
|
+
|
|
54
|
+
Any noncommercial purpose is a permitted purpose.
|
|
55
|
+
|
|
56
|
+
## Personal Uses
|
|
57
|
+
|
|
58
|
+
Personal use for research, experiment, and testing for
|
|
59
|
+
the benefit of public knowledge, personal study, private
|
|
60
|
+
entertainment, hobby projects, amateur pursuits, or religious
|
|
61
|
+
observance, without any anticipated commercial application,
|
|
62
|
+
is use for a permitted purpose.
|
|
63
|
+
|
|
64
|
+
## Noncommercial Organizations
|
|
65
|
+
|
|
66
|
+
Use by any charitable organization, educational institution,
|
|
67
|
+
public research organization, public safety or health
|
|
68
|
+
organization, environmental protection organization,
|
|
69
|
+
or government institution is use for a permitted purpose
|
|
70
|
+
regardless of the source of funding or obligations resulting
|
|
71
|
+
from the funding.
|
|
72
|
+
|
|
73
|
+
## Fair Use
|
|
74
|
+
|
|
75
|
+
You may have "fair use" rights for the software under the
|
|
76
|
+
law. These terms do not limit them.
|
|
77
|
+
|
|
78
|
+
## No Other Rights
|
|
79
|
+
|
|
80
|
+
These terms do not allow you to sublicense or transfer any of
|
|
81
|
+
your licenses to anyone else, or prevent the licensor from
|
|
82
|
+
granting licenses to anyone else. These terms do not imply
|
|
83
|
+
any other licenses.
|
|
84
|
+
|
|
85
|
+
## Patent Defense
|
|
86
|
+
|
|
87
|
+
If you make any written claim that the software infringes or
|
|
88
|
+
contributes to infringement of any patent, your patent license
|
|
89
|
+
for the software granted under these terms ends immediately. If
|
|
90
|
+
your company makes such a claim, your patent license ends
|
|
91
|
+
immediately for work on behalf of your company.
|
|
92
|
+
|
|
93
|
+
## Violations
|
|
94
|
+
|
|
95
|
+
The first time you are notified in writing that you have
|
|
96
|
+
violated any of these terms, or done anything with the software
|
|
97
|
+
not covered by your licenses, your licenses can nonetheless
|
|
98
|
+
continue if you come into full compliance with these terms,
|
|
99
|
+
and take practical steps to correct past violations, within
|
|
100
|
+
32 days of receiving notice. Otherwise, all your licenses
|
|
101
|
+
end immediately.
|
|
102
|
+
|
|
103
|
+
## No Liability
|
|
104
|
+
|
|
105
|
+
***As far as the law allows, the software comes as is, without
|
|
106
|
+
any warranty or condition, and the licensor will not be liable
|
|
107
|
+
to you for any damages arising out of these terms or the use
|
|
108
|
+
or nature of the software, under any kind of legal claim.***
|
|
109
|
+
|
|
110
|
+
## Definitions
|
|
111
|
+
|
|
112
|
+
The **licensor** is the individual or entity offering these
|
|
113
|
+
terms, and the **software** is the software the licensor makes
|
|
114
|
+
available under these terms.
|
|
115
|
+
|
|
116
|
+
**You** refers to the individual or entity agreeing to these
|
|
117
|
+
terms.
|
|
118
|
+
|
|
119
|
+
**Your company** is any legal entity, sole proprietorship,
|
|
120
|
+
or other kind of organization that you work for, plus all
|
|
121
|
+
organizations that have control over, are under the control of,
|
|
122
|
+
or are under common control with that organization. **Control**
|
|
123
|
+
means ownership of substantially all the assets of an entity,
|
|
124
|
+
or the power to direct its management and policies by vote,
|
|
125
|
+
contract, or otherwise. Control can be direct or indirect.
|
|
126
|
+
|
|
127
|
+
**Your licenses** are all the licenses granted to you for the
|
|
128
|
+
software under these terms.
|
|
129
|
+
|
|
130
|
+
**Use** means anything you do with the software requiring one
|
|
131
|
+
of your licenses.
|
data/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Oicana for Ruby
|
|
2
|
+
|
|
3
|
+
*Generate PDFs in Ruby without a headless browser.*
|
|
4
|
+
|
|
5
|
+
Ruby apps generating PDFs usually wrap the abandoned wkhtmltopdf through wicked_pdf or PDFKit, drive headless Chrome through Grover, or write every layout as code with Prawn.
|
|
6
|
+
|
|
7
|
+
Oicana compiles PDFs in process through a native library instead. You design documents as [Typst](https://typst.app/) templates, load them once at startup, and render them from JSON in single-digit milliseconds. No browser process, no per-document fees, no document data leaving your infrastructure.
|
|
8
|
+
|
|
9
|
+
> **Free for noncommercial use.** Commercial use is free for 30 days, then needs a [per-application subscription](https://oicana.com/#pricing) with unlimited seats.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
gem install oicana # or: bundle add oicana
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Requires Ruby 3.2 or newer. Platform gems bring the native library for Linux (x64 and arm64, glibc 2.28 or newer, or musl), macOS (x64 and arm64), and Windows (x64 and arm64).
|
|
18
|
+
|
|
19
|
+
## Quick start
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
require "oicana"
|
|
23
|
+
|
|
24
|
+
template = Oicana::Template.new(File.binread("invoice-0.1.0.zip"))
|
|
25
|
+
|
|
26
|
+
pdf = template.export_pdf(
|
|
27
|
+
json_inputs: { "invoice" => { number: "2026-001", customer: "Acme GmbH", total: "€1,190.00" } }
|
|
28
|
+
)
|
|
29
|
+
File.binwrite("invoice.pdf", pdf)
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
JSON inputs take Strings holding JSON, or any value `JSON.generate` accepts. Blob inputs take binary Strings or `Oicana::BlobInput` values with metadata. `export_png` and `export_svg` produce the other formats, `pages:` takes a page index or a Range, `compile` keeps a document to export its pages separately, and `Template.export_once` renders a template once without caching it. Errors are subclasses of `Oicana::Error`.
|
|
33
|
+
|
|
34
|
+
## Running in a web service
|
|
35
|
+
|
|
36
|
+
Creating a `Template` compiles it once to warm up the Typst cache, so create it once per process, not per request. Oicana cannot compile in a process forked after its parent compiled, so with forking servers and job runners like Puma in cluster mode or Solid Queue, create templates on first use instead of during boot. A template is safe to use from multiple threads, and native calls release the GVL. Templates and compiled documents are freed by the garbage collector, or right away with `close`.
|
|
37
|
+
|
|
38
|
+
## Licensing
|
|
39
|
+
|
|
40
|
+
Oicana is source-available under the [PolyForm Noncommercial License 1.0.0](https://github.com/oicana/oicana/blob/main/LICENSE.md) and free for noncommercial use. Commercial use is free for 30 days; see [pricing](https://oicana.com/#pricing) for subscriptions, or write to `hello@oicana.com`.
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Oicana
|
|
4
|
+
# Base class of all errors raised by Oicana. The message includes rendered
|
|
5
|
+
# compilation diagnostics.
|
|
6
|
+
class Error < StandardError; end
|
|
7
|
+
|
|
8
|
+
# The packed template could not be read or its manifest is invalid.
|
|
9
|
+
class InvalidTemplateError < Error; end
|
|
10
|
+
|
|
11
|
+
# An input value, its blob metadata, or a page range is invalid.
|
|
12
|
+
class InvalidInputError < Error; end
|
|
13
|
+
|
|
14
|
+
# The template failed to compile.
|
|
15
|
+
class CompilationError < Error; end
|
|
16
|
+
|
|
17
|
+
# The requested file is not part of the template.
|
|
18
|
+
class FileNotFoundError < Error; end
|
|
19
|
+
|
|
20
|
+
# The compiled document could not be exported.
|
|
21
|
+
class ExportError < Error; end
|
|
22
|
+
|
|
23
|
+
# A template or document is no longer registered.
|
|
24
|
+
class NotFoundError < Error; end
|
|
25
|
+
|
|
26
|
+
# An unexpected failure inside Oicana.
|
|
27
|
+
class InternalError < Error; end
|
|
28
|
+
|
|
29
|
+
# A template or document was used after #close.
|
|
30
|
+
class ClosedError < Error; end
|
|
31
|
+
|
|
32
|
+
ERRORS = {
|
|
33
|
+
"InvalidTemplate" => InvalidTemplateError,
|
|
34
|
+
"InvalidInput" => InvalidInputError,
|
|
35
|
+
"Compilation" => CompilationError,
|
|
36
|
+
"FileNotFound" => FileNotFoundError,
|
|
37
|
+
"Export" => ExportError,
|
|
38
|
+
"NotFound" => NotFoundError,
|
|
39
|
+
"Internal" => InternalError
|
|
40
|
+
}.freeze
|
|
41
|
+
private_constant :ERRORS
|
|
42
|
+
|
|
43
|
+
@compiling_pid = nil
|
|
44
|
+
|
|
45
|
+
# @api private
|
|
46
|
+
# Runs the block and translates errors of the generated bindings.
|
|
47
|
+
def self.translate_errors
|
|
48
|
+
if @compiling_pid && @compiling_pid != Process.pid
|
|
49
|
+
raise Error, "Oicana cannot be used in a forked process after the parent process #{@compiling_pid} " \
|
|
50
|
+
"used it. Create templates after forking, for example lazily on first use."
|
|
51
|
+
end
|
|
52
|
+
@compiling_pid = Process.pid
|
|
53
|
+
yield
|
|
54
|
+
rescue OicanaUniffi::OicanaError::InvalidTemplate, OicanaUniffi::OicanaError::InvalidInput,
|
|
55
|
+
OicanaUniffi::OicanaError::Compilation, OicanaUniffi::OicanaError::FileNotFound,
|
|
56
|
+
OicanaUniffi::OicanaError::Export, OicanaUniffi::OicanaError::NotFound,
|
|
57
|
+
OicanaUniffi::OicanaError::Internal => e
|
|
58
|
+
raise ERRORS.fetch(e.class.name.split("::").last), e.message
|
|
59
|
+
rescue OicanaUniffi::InternalError => e
|
|
60
|
+
raise InternalError, e.message
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ffi"
|
|
4
|
+
|
|
5
|
+
module Oicana
|
|
6
|
+
# Loads the native library and adjusts the generated bindings in lib/oicana/oicana_uniffi.rb.
|
|
7
|
+
module Native
|
|
8
|
+
# Makes every native call release the GVL, so other Ruby threads keep running while
|
|
9
|
+
# a template compiles or exports.
|
|
10
|
+
module BlockingCalls
|
|
11
|
+
def attach_function(*args)
|
|
12
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
|
13
|
+
super(*args, options.merge(blocking: true))
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Copies strings into native buffers at once. The generated code goes through
|
|
18
|
+
# String#bytes, which allocates an Integer for every byte.
|
|
19
|
+
module BulkWrites
|
|
20
|
+
def write(value)
|
|
21
|
+
reserve(value.bytesize) do
|
|
22
|
+
@rust_buf.data.put_bytes(@rust_buf.len, value)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def write_String(value)
|
|
27
|
+
value = OicanaUniffi.uniffi_utf8(value)
|
|
28
|
+
pack_into 4, "l>", value.bytesize
|
|
29
|
+
write value
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def write_Bytes(value)
|
|
33
|
+
value = OicanaUniffi.uniffi_bytes(value)
|
|
34
|
+
pack_into 4, "l>", value.bytesize
|
|
35
|
+
write value
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# The native library, either from OICANA_LIBRARY_PATH or shipped in the platform gem.
|
|
40
|
+
def self.library_path
|
|
41
|
+
ENV.fetch("OICANA_LIBRARY_PATH") do
|
|
42
|
+
path = File.expand_path("native/#{FFI.map_library_name("oicana_uniffi")}", __dir__)
|
|
43
|
+
return path if File.exist?(path)
|
|
44
|
+
|
|
45
|
+
raise LoadError, missing_library_message
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.missing_library_message
|
|
50
|
+
platform = Gem::Platform.local
|
|
51
|
+
if %w[x86_64 x64 aarch64 arm64].include?(platform.cpu) && %w[linux darwin mingw].include?(platform.os)
|
|
52
|
+
"The oicana gem without a native library is installed, but #{platform} has a platform gem that " \
|
|
53
|
+
"contains it. Check that Bundler's force_ruby_platform is not set, and add the platform to your " \
|
|
54
|
+
"Gemfile.lock with `bundle lock --add-platform #{platform}`."
|
|
55
|
+
else
|
|
56
|
+
"Oicana has no native library for #{platform}. Supported are Linux (glibc and musl), macOS and " \
|
|
57
|
+
"Windows, each on x64 and arm64. Set OICANA_LIBRARY_PATH to use a library built from source."
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Called by the generated bindings, see uniffi.toml.
|
|
62
|
+
def self.prepare(library)
|
|
63
|
+
library.singleton_class.prepend(BlockingCalls)
|
|
64
|
+
library_path
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def self.patch_bindings
|
|
68
|
+
OicanaUniffi.const_get(:RustBufferBuilder).prepend(BulkWrites)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|