html-to-pdf-crab-js 0.1.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.
- package/LICENSE +21 -0
- package/README.md +80 -0
- package/browser.js +26 -0
- package/index.cjs +738 -0
- package/index.d.ts +40 -0
- package/index.js +742 -0
- package/package.json +103 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 N-API for Rust
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# html-to-pdf-crab-js
|
|
2
|
+
|
|
3
|
+
Native Node.js package built with Rust and NAPI-RS for HTML-to-PDF rendering.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
Node.js should use the native NAPI entrypoint:
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
import { writeFileSync } from 'node:fs'
|
|
11
|
+
import { createPdfFromHtml } from 'html-to-pdf-crab-js'
|
|
12
|
+
|
|
13
|
+
const pdf = await createPdfFromHtml({
|
|
14
|
+
html: '<!doctype html><html><body><main><h1>Invoice</h1><p>Generated from HTML.</p></main></body></html>',
|
|
15
|
+
css: 'body { font-family: sans-serif; } h1 { color: #111827; }',
|
|
16
|
+
page: {
|
|
17
|
+
size: 'A4',
|
|
18
|
+
margin: 12,
|
|
19
|
+
},
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
writeFileSync('html-example.pdf', pdf)
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The public API is intentionally small:
|
|
26
|
+
|
|
27
|
+
- `createPdfFromHtml(input): Promise<Buffer>`
|
|
28
|
+
|
|
29
|
+
Use `pdf-crab-js` when the document is already structured as explicit PDF pages and elements.
|
|
30
|
+
|
|
31
|
+
## WebAssembly
|
|
32
|
+
|
|
33
|
+
Browser, Deno, Bun, and portable runtimes can use the NAPI-RS WebAssembly build:
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
import { createPdfFromHtml } from 'html-to-pdf-crab-js/wasm'
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Browser deployments must enable `SharedArrayBuffer`, which requires these response headers:
|
|
40
|
+
|
|
41
|
+
```text
|
|
42
|
+
Cross-Origin-Embedder-Policy: require-corp
|
|
43
|
+
Cross-Origin-Opener-Policy: same-origin
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Development
|
|
47
|
+
|
|
48
|
+
Install dependencies from the workspace root:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pnpm install --filter html-to-pdf-crab-js
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Build and test:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
pnpm --filter html-to-pdf-crab-js build
|
|
58
|
+
pnpm --filter html-to-pdf-crab-js test
|
|
59
|
+
pnpm --filter html-to-pdf-crab-js check
|
|
60
|
+
pnpm --filter html-to-pdf-crab-js lint
|
|
61
|
+
pnpm --filter html-to-pdf-crab-js fmt:check
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Run local examples:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
pnpm --filter html-to-pdf-crab-js-examples invoice
|
|
68
|
+
pnpm --filter html-to-pdf-crab-js-examples report
|
|
69
|
+
pnpm --filter html-to-pdf-crab-js-examples browser
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Generated files are written to `examples/html-to-pdf-crab-js/output/`.
|
|
73
|
+
|
|
74
|
+
Build and smoke-test the WebAssembly binding:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
rustup target add wasm32-wasip1-threads
|
|
78
|
+
pnpm --filter html-to-pdf-crab-js build:wasm
|
|
79
|
+
pnpm --filter html-to-pdf-crab-js test:wasm
|
|
80
|
+
```
|
package/browser.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Buffer as __Buffer } from 'buffer'
|
|
2
|
+
|
|
3
|
+
globalThis.Buffer ??= __Buffer
|
|
4
|
+
|
|
5
|
+
const __binding = await import('html-to-pdf-crab-js-wasm32-wasi')
|
|
6
|
+
|
|
7
|
+
export default __binding.default ?? __binding
|
|
8
|
+
|
|
9
|
+
function toBase64(value) {
|
|
10
|
+
return __Buffer.from(value).toString('base64')
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function normalizeInput(input) {
|
|
14
|
+
return {
|
|
15
|
+
...input,
|
|
16
|
+
fonts: input.fonts?.map(toBase64),
|
|
17
|
+
images: input.images?.map((image) => ({
|
|
18
|
+
...image,
|
|
19
|
+
data: toBase64(image.data),
|
|
20
|
+
})),
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createPdfFromHtml(input) {
|
|
25
|
+
return Promise.resolve(__binding.createPdfFromHtml(normalizeInput(input)))
|
|
26
|
+
}
|