prova-pdf 0.1.0__tar.gz
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.
- prova_pdf-0.1.0/LICENSE +21 -0
- prova_pdf-0.1.0/PKG-INFO +15 -0
- prova_pdf-0.1.0/prova_pdf/__init__.py +242 -0
- prova_pdf-0.1.0/prova_pdf/prova_pdf.wasm +0 -0
- prova_pdf-0.1.0/prova_pdf/py.typed +0 -0
- prova_pdf-0.1.0/prova_pdf.egg-info/PKG-INFO +15 -0
- prova_pdf-0.1.0/prova_pdf.egg-info/SOURCES.txt +10 -0
- prova_pdf-0.1.0/prova_pdf.egg-info/dependency_links.txt +1 -0
- prova_pdf-0.1.0/prova_pdf.egg-info/requires.txt +1 -0
- prova_pdf-0.1.0/prova_pdf.egg-info/top_level.txt +1 -0
- prova_pdf-0.1.0/pyproject.toml +28 -0
- prova_pdf-0.1.0/setup.cfg +4 -0
prova_pdf-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dickson Pinheiro
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
prova_pdf-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: prova-pdf
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Exam PDF generator powered by WebAssembly
|
|
5
|
+
Author: Dickson Pinheiro
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Dickson-Pinheiro/prova-pdf
|
|
8
|
+
Project-URL: Repository, https://github.com/Dickson-Pinheiro/prova-pdf
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Topic :: Text Processing :: General
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: wasmtime<27,>=25
|
|
15
|
+
Dynamic: license-file
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
"""prova-pdf: Exam PDF generator powered by WebAssembly."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__version__ = "0.1.0"
|
|
6
|
+
__all__ = ["generate_pdf", "FontInput", "ProvaPdfError"]
|
|
7
|
+
|
|
8
|
+
import ctypes
|
|
9
|
+
import json
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Any, TypedDict
|
|
12
|
+
|
|
13
|
+
import wasmtime
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class FontInput(TypedDict):
|
|
17
|
+
"""Font registration input."""
|
|
18
|
+
|
|
19
|
+
family: str
|
|
20
|
+
variant: int # 0=regular, 1=bold, 2=italic, 3=bold-italic
|
|
21
|
+
data: bytes
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class ProvaPdfError(Exception):
|
|
25
|
+
"""Raised when the WASM module reports an error."""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _find_wasm() -> Path:
|
|
29
|
+
"""Locate prova_pdf.wasm -- bundled in package or in repo wasm/ dir."""
|
|
30
|
+
pkg_dir = Path(__file__).parent
|
|
31
|
+
candidates = [
|
|
32
|
+
pkg_dir / "prova_pdf.wasm",
|
|
33
|
+
pkg_dir.parent.parent.parent / "wasm" / "prova_pdf.wasm",
|
|
34
|
+
]
|
|
35
|
+
for p in candidates:
|
|
36
|
+
if p.exists():
|
|
37
|
+
return p
|
|
38
|
+
raise FileNotFoundError(
|
|
39
|
+
"prova_pdf.wasm not found. Run `make build-wasi` first."
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class _Runtime:
|
|
44
|
+
"""Lazy-loaded WASI runtime singleton."""
|
|
45
|
+
|
|
46
|
+
_instance: _Runtime | None = None
|
|
47
|
+
|
|
48
|
+
@classmethod
|
|
49
|
+
def get(cls) -> _Runtime:
|
|
50
|
+
if cls._instance is None:
|
|
51
|
+
cls._instance = cls()
|
|
52
|
+
return cls._instance
|
|
53
|
+
|
|
54
|
+
def __init__(self) -> None:
|
|
55
|
+
wasm_path = _find_wasm()
|
|
56
|
+
engine = wasmtime.Engine()
|
|
57
|
+
self._store = wasmtime.Store(engine)
|
|
58
|
+
linker = wasmtime.Linker(engine)
|
|
59
|
+
linker.define_wasi()
|
|
60
|
+
wasi_config = wasmtime.WasiConfig()
|
|
61
|
+
self._store.set_wasi(wasi_config)
|
|
62
|
+
module = wasmtime.Module.from_file(engine, str(wasm_path))
|
|
63
|
+
instance = linker.instantiate(self._store, module)
|
|
64
|
+
|
|
65
|
+
exports = instance.exports(self._store)
|
|
66
|
+
self._memory: wasmtime.Memory = exports["memory"]
|
|
67
|
+
|
|
68
|
+
# Cache exported functions
|
|
69
|
+
self._alloc = exports["prova_pdf_alloc"]
|
|
70
|
+
self._free = exports["prova_pdf_free"]
|
|
71
|
+
self._add_font = exports["prova_pdf_add_font"]
|
|
72
|
+
self._set_font_rules = exports["prova_pdf_set_font_rules"]
|
|
73
|
+
self._add_image = exports["prova_pdf_add_image"]
|
|
74
|
+
self._clear_all = exports["prova_pdf_clear_all"]
|
|
75
|
+
self._generate = exports["prova_pdf_generate"]
|
|
76
|
+
self._output_ptr = exports["prova_pdf_output_ptr"]
|
|
77
|
+
self._output_len = exports["prova_pdf_output_len"]
|
|
78
|
+
self._last_error_len = exports["prova_pdf_last_error_len"]
|
|
79
|
+
self._last_error_message = exports["prova_pdf_last_error_message"]
|
|
80
|
+
|
|
81
|
+
# -- memory helpers ------------------------------------------------
|
|
82
|
+
|
|
83
|
+
def _mem_base(self) -> int:
|
|
84
|
+
"""Return the integer base address of WASM linear memory."""
|
|
85
|
+
raw = self._memory.data_ptr(self._store)
|
|
86
|
+
return ctypes.cast(raw, ctypes.c_void_p).value or 0
|
|
87
|
+
|
|
88
|
+
def _write_bytes(self, data: bytes) -> tuple[int, int]:
|
|
89
|
+
"""Allocate WASM memory and write *data*. Returns (ptr, len)."""
|
|
90
|
+
n = len(data)
|
|
91
|
+
if n == 0:
|
|
92
|
+
return 0, 0
|
|
93
|
+
ptr: int = self._alloc(self._store, n)
|
|
94
|
+
base = self._mem_base()
|
|
95
|
+
src = (ctypes.c_ubyte * n).from_buffer_copy(data)
|
|
96
|
+
ctypes.memmove(base + ptr, src, n)
|
|
97
|
+
return ptr, n
|
|
98
|
+
|
|
99
|
+
def _read_bytes(self, ptr: int, n: int) -> bytes:
|
|
100
|
+
"""Read *n* bytes from WASM memory at *ptr*."""
|
|
101
|
+
if n == 0:
|
|
102
|
+
return b""
|
|
103
|
+
base = self._mem_base()
|
|
104
|
+
return bytes((ctypes.c_ubyte * n).from_address(base + ptr))
|
|
105
|
+
|
|
106
|
+
def _free_pair(self, ptr: int, length: int) -> None:
|
|
107
|
+
"""Free a previously allocated region."""
|
|
108
|
+
if length > 0:
|
|
109
|
+
self._free(self._store, ptr, length)
|
|
110
|
+
|
|
111
|
+
# -- error handling ------------------------------------------------
|
|
112
|
+
|
|
113
|
+
def _read_last_error(self) -> str:
|
|
114
|
+
"""Read the last error message from the WASM module."""
|
|
115
|
+
err_len: int = self._last_error_len(self._store)
|
|
116
|
+
if err_len == 0:
|
|
117
|
+
return "unknown error"
|
|
118
|
+
buf_ptr: int = self._alloc(self._store, err_len)
|
|
119
|
+
self._last_error_message(self._store, buf_ptr)
|
|
120
|
+
data = self._read_bytes(buf_ptr, err_len)
|
|
121
|
+
self._free(self._store, buf_ptr, err_len)
|
|
122
|
+
return data.decode("utf-8", errors="replace")
|
|
123
|
+
|
|
124
|
+
def _check(self, rc: int) -> int:
|
|
125
|
+
"""Raise :class:`ProvaPdfError` if *rc* < 0."""
|
|
126
|
+
if rc < 0:
|
|
127
|
+
raise ProvaPdfError(self._read_last_error())
|
|
128
|
+
return rc
|
|
129
|
+
|
|
130
|
+
# -- public operations ---------------------------------------------
|
|
131
|
+
|
|
132
|
+
def clear_all(self) -> None:
|
|
133
|
+
self._clear_all(self._store)
|
|
134
|
+
|
|
135
|
+
def add_font(self, family: str, variant: int, data: bytes) -> None:
|
|
136
|
+
family_bytes = family.encode("utf-8")
|
|
137
|
+
fam_ptr, fam_len = self._write_bytes(family_bytes)
|
|
138
|
+
dat_ptr, dat_len = self._write_bytes(data)
|
|
139
|
+
try:
|
|
140
|
+
rc: int = self._add_font(
|
|
141
|
+
self._store, fam_ptr, fam_len, variant, dat_ptr, dat_len
|
|
142
|
+
)
|
|
143
|
+
self._check(rc)
|
|
144
|
+
finally:
|
|
145
|
+
self._free_pair(fam_ptr, fam_len)
|
|
146
|
+
self._free_pair(dat_ptr, dat_len)
|
|
147
|
+
|
|
148
|
+
def set_font_rules(self, rules: dict[str, Any]) -> None:
|
|
149
|
+
payload = json.dumps(rules).encode("utf-8")
|
|
150
|
+
ptr, length = self._write_bytes(payload)
|
|
151
|
+
try:
|
|
152
|
+
rc: int = self._set_font_rules(self._store, ptr, length)
|
|
153
|
+
self._check(rc)
|
|
154
|
+
finally:
|
|
155
|
+
self._free_pair(ptr, length)
|
|
156
|
+
|
|
157
|
+
def add_image(self, key: str, data: bytes) -> None:
|
|
158
|
+
key_bytes = key.encode("utf-8")
|
|
159
|
+
key_ptr, key_len = self._write_bytes(key_bytes)
|
|
160
|
+
dat_ptr, dat_len = self._write_bytes(data)
|
|
161
|
+
try:
|
|
162
|
+
rc: int = self._add_image(
|
|
163
|
+
self._store, key_ptr, key_len, dat_ptr, dat_len
|
|
164
|
+
)
|
|
165
|
+
self._check(rc)
|
|
166
|
+
finally:
|
|
167
|
+
self._free_pair(key_ptr, key_len)
|
|
168
|
+
self._free_pair(dat_ptr, dat_len)
|
|
169
|
+
|
|
170
|
+
def generate(self, spec_json: bytes) -> bytes:
|
|
171
|
+
"""Run the two-call generate protocol and return PDF bytes."""
|
|
172
|
+
ptr, length = self._write_bytes(spec_json)
|
|
173
|
+
try:
|
|
174
|
+
# First call: out_buf=0, out_cap=0 -> stages output internally
|
|
175
|
+
rc: int = self._generate(self._store, ptr, length, 0, 0)
|
|
176
|
+
self._check(rc)
|
|
177
|
+
finally:
|
|
178
|
+
self._free_pair(ptr, length)
|
|
179
|
+
|
|
180
|
+
# Read staged output
|
|
181
|
+
out_ptr: int = self._output_ptr(self._store)
|
|
182
|
+
out_len: int = self._output_len(self._store)
|
|
183
|
+
if out_len == 0:
|
|
184
|
+
raise ProvaPdfError("generate produced zero-length output")
|
|
185
|
+
return self._read_bytes(out_ptr, out_len)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
# -- public API --------------------------------------------------------
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def generate_pdf(
|
|
192
|
+
spec: dict[str, Any],
|
|
193
|
+
fonts: list[FontInput],
|
|
194
|
+
images: dict[str, bytes] | None = None,
|
|
195
|
+
font_rules: dict[str, Any] | None = None,
|
|
196
|
+
) -> bytes:
|
|
197
|
+
"""Generate a PDF from *spec*.
|
|
198
|
+
|
|
199
|
+
Parameters
|
|
200
|
+
----------
|
|
201
|
+
spec:
|
|
202
|
+
The exam specification dict (serialised to JSON internally).
|
|
203
|
+
fonts:
|
|
204
|
+
List of fonts to register before generation.
|
|
205
|
+
images:
|
|
206
|
+
Optional mapping of image key -> raw image bytes.
|
|
207
|
+
font_rules:
|
|
208
|
+
Optional font-rule configuration dict.
|
|
209
|
+
|
|
210
|
+
Returns
|
|
211
|
+
-------
|
|
212
|
+
bytes
|
|
213
|
+
The raw PDF file content.
|
|
214
|
+
|
|
215
|
+
Raises
|
|
216
|
+
------
|
|
217
|
+
ProvaPdfError
|
|
218
|
+
If the WASM module reports an error at any step.
|
|
219
|
+
FileNotFoundError
|
|
220
|
+
If prova_pdf.wasm cannot be located.
|
|
221
|
+
"""
|
|
222
|
+
rt = _Runtime.get()
|
|
223
|
+
|
|
224
|
+
# 1. Clean slate
|
|
225
|
+
rt.clear_all()
|
|
226
|
+
|
|
227
|
+
# 2. Register fonts
|
|
228
|
+
for font in fonts:
|
|
229
|
+
rt.add_font(font["family"], font["variant"], font["data"])
|
|
230
|
+
|
|
231
|
+
# 3. Register images
|
|
232
|
+
if images:
|
|
233
|
+
for key, data in images.items():
|
|
234
|
+
rt.add_image(key, data)
|
|
235
|
+
|
|
236
|
+
# 4. Font rules
|
|
237
|
+
if font_rules is not None:
|
|
238
|
+
rt.set_font_rules(font_rules)
|
|
239
|
+
|
|
240
|
+
# 5. Generate
|
|
241
|
+
spec_bytes = json.dumps(spec).encode("utf-8")
|
|
242
|
+
return rt.generate(spec_bytes)
|
|
Binary file
|
|
File without changes
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: prova-pdf
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Exam PDF generator powered by WebAssembly
|
|
5
|
+
Author: Dickson Pinheiro
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Dickson-Pinheiro/prova-pdf
|
|
8
|
+
Project-URL: Repository, https://github.com/Dickson-Pinheiro/prova-pdf
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Topic :: Text Processing :: General
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: wasmtime<27,>=25
|
|
15
|
+
Dynamic: license-file
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
pyproject.toml
|
|
3
|
+
prova_pdf/__init__.py
|
|
4
|
+
prova_pdf/prova_pdf.wasm
|
|
5
|
+
prova_pdf/py.typed
|
|
6
|
+
prova_pdf.egg-info/PKG-INFO
|
|
7
|
+
prova_pdf.egg-info/SOURCES.txt
|
|
8
|
+
prova_pdf.egg-info/dependency_links.txt
|
|
9
|
+
prova_pdf.egg-info/requires.txt
|
|
10
|
+
prova_pdf.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
wasmtime<27,>=25
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
prova_pdf
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "prova-pdf"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Exam PDF generator powered by WebAssembly"
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
authors = [{ name = "Dickson Pinheiro" }]
|
|
13
|
+
dependencies = ["wasmtime>=25,<27"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
"Topic :: Text Processing :: General",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[project.urls]
|
|
21
|
+
Homepage = "https://github.com/Dickson-Pinheiro/prova-pdf"
|
|
22
|
+
Repository = "https://github.com/Dickson-Pinheiro/prova-pdf"
|
|
23
|
+
|
|
24
|
+
[tool.setuptools.packages.find]
|
|
25
|
+
include = ["prova_pdf*"]
|
|
26
|
+
|
|
27
|
+
[tool.setuptools.package-data]
|
|
28
|
+
prova_pdf = ["*.wasm", "py.typed"]
|