@papack/pdf 1.0.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/dist/index.cjs +342 -0
- package/dist/index.d.cts +453 -0
- package/dist/index.d.mts +454 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +337 -0
- package/dist/index.mjs.map +1 -0
- package/licence.md +41 -0
- package/package.json +29 -0
- package/readme.md +194 -0
package/licence.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Matthias Steiner
|
|
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.
|
|
22
|
+
|
|
23
|
+
## Third-Party Software
|
|
24
|
+
|
|
25
|
+
This software bundles and/or includes code from the following third-party
|
|
26
|
+
projects. These components are distributed under their respective licenses.
|
|
27
|
+
|
|
28
|
+
### React
|
|
29
|
+
|
|
30
|
+
Copyright (c) Meta Platforms, Inc. and affiliates
|
|
31
|
+
License: MIT
|
|
32
|
+
https://github.com/facebook/react
|
|
33
|
+
|
|
34
|
+
### @react-pdf/renderer
|
|
35
|
+
|
|
36
|
+
Copyright (c) Diego Muracciole
|
|
37
|
+
License: MIT
|
|
38
|
+
https://github.com/diegomura/react-pdf
|
|
39
|
+
|
|
40
|
+
The original license texts of these projects apply to the portions of the
|
|
41
|
+
software derived from them and are not superseded by the license above.
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@papack/pdf",
|
|
3
|
+
"description": "Minimal PDF rendering wrapper built on React and react-pdf",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"author": "Matthias Steiner",
|
|
6
|
+
"repository": "github:papack/pdf",
|
|
7
|
+
"homepage": "https://github.com/papack/pdf",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"module": "dist/index.mjs",
|
|
12
|
+
"main": "./dist/index.cjs",
|
|
13
|
+
"types": "./dist/index.d.mts",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsdown"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@react-pdf/renderer": "^4.3.1",
|
|
24
|
+
"@types/bun": "latest",
|
|
25
|
+
"@types/react": "^19.2.7",
|
|
26
|
+
"react": "^19.2.3",
|
|
27
|
+
"tsdown": "^0.18.2"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# @papack/pdf
|
|
2
|
+
|
|
3
|
+
Minimal PDF rendering wrapper built on React and react-pdf.
|
|
4
|
+
|
|
5
|
+
This package provides a very small abstraction layer around **React** and **@react-pdf/renderer**.
|
|
6
|
+
|
|
7
|
+
## Core Idea
|
|
8
|
+
|
|
9
|
+
- JSX rendered directly to PDF (buffer)
|
|
10
|
+
- Built on React + @react-pdf/renderer
|
|
11
|
+
|
|
12
|
+
This is **PDF rendering only**:
|
|
13
|
+
define document → render → buffer / file.
|
|
14
|
+
|
|
15
|
+
## What this is
|
|
16
|
+
|
|
17
|
+
- A thin wrapper
|
|
18
|
+
- Some helpers (`<For />`, `<Show />`, `<Repeat />`)
|
|
19
|
+
- A custom JSX runtime for controlled usage
|
|
20
|
+
- Convenience utilities for layout and styling
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @papack/pdf
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { jsx, render } from "@papack/pdf/core";
|
|
32
|
+
import { writeFile } from "fs/promises";
|
|
33
|
+
import { Document, Page, Text } from "@papack/pdf/layout";
|
|
34
|
+
|
|
35
|
+
const MyDocument = () => (
|
|
36
|
+
<Document title="Example">
|
|
37
|
+
<Page size="A4">
|
|
38
|
+
<Text>Hello PDF</Text>
|
|
39
|
+
</Page>
|
|
40
|
+
</Document>
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const buffer = await render(<MyDocument />);
|
|
44
|
+
await writeFile("example.pdf", buffer);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Rendering
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { render } from "@papack/pdf/core";
|
|
51
|
+
|
|
52
|
+
const buf = await render(<MyDocument />);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
- Returns a `Buffer`
|
|
56
|
+
- Internally calls `@react-pdf/renderer/pdf().toBuffer()`
|
|
57
|
+
|
|
58
|
+
## JSX Runtime
|
|
59
|
+
|
|
60
|
+
The package ships with a minimal JSX runtime.
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { jsx, fragment } from "@papack/pdf/core";
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
- Uses `React.createElement`
|
|
67
|
+
- No transforms, no extensions
|
|
68
|
+
- Intended for controlled environments (Node.js)
|
|
69
|
+
|
|
70
|
+
## Components
|
|
71
|
+
|
|
72
|
+
### `<For />`
|
|
73
|
+
|
|
74
|
+
```tsx
|
|
75
|
+
<For each={items}>{(item, index) => <Text>{item}</Text>}</For>
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
- Simple array iteration
|
|
79
|
+
- No keys
|
|
80
|
+
- No diffing
|
|
81
|
+
- Render-only
|
|
82
|
+
|
|
83
|
+
### `<Show />`
|
|
84
|
+
|
|
85
|
+
```tsx
|
|
86
|
+
<Show when={loggedIn}>
|
|
87
|
+
<Text>Dashboard</Text>
|
|
88
|
+
</Show>
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
- Conditional rendering
|
|
92
|
+
- Returns `null` when false
|
|
93
|
+
|
|
94
|
+
### `<Repeat />`
|
|
95
|
+
|
|
96
|
+
```tsx
|
|
97
|
+
<Repeat n={3}>
|
|
98
|
+
<Text>Line</Text>
|
|
99
|
+
</Repeat>
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
- Repeats children `n` times
|
|
103
|
+
- Internally uses fragments
|
|
104
|
+
|
|
105
|
+
## Layout Helpers
|
|
106
|
+
|
|
107
|
+
The library provides basic layout primitives built on react-pdf:
|
|
108
|
+
|
|
109
|
+
- `Box`
|
|
110
|
+
- `Flex`
|
|
111
|
+
- `Stack`
|
|
112
|
+
- `Page`
|
|
113
|
+
- `Document`
|
|
114
|
+
- `Text`
|
|
115
|
+
|
|
116
|
+
These are thin wrappers with predictable props.
|
|
117
|
+
|
|
118
|
+
Example:
|
|
119
|
+
|
|
120
|
+
```tsx
|
|
121
|
+
<Page size="A4" p="2cm">
|
|
122
|
+
<Stack g="1cm">
|
|
123
|
+
<Text>Title</Text>
|
|
124
|
+
<Text>Content</Text>
|
|
125
|
+
</Stack>
|
|
126
|
+
</Page>
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Absolute Positioning
|
|
130
|
+
|
|
131
|
+
```tsx
|
|
132
|
+
import { Absolute } from "@papack/pdf/layout";
|
|
133
|
+
|
|
134
|
+
<Absolute top="2cm" left="2cm">
|
|
135
|
+
<Text>Overlay</Text>
|
|
136
|
+
</Absolute>;
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
- Maps directly to `position: absolute`
|
|
140
|
+
- No layout engine involved
|
|
141
|
+
|
|
142
|
+
## Styling
|
|
143
|
+
|
|
144
|
+
Shared style constants are provided:
|
|
145
|
+
|
|
146
|
+
- `font`
|
|
147
|
+
- `color`
|
|
148
|
+
- `size`
|
|
149
|
+
- `space`
|
|
150
|
+
|
|
151
|
+
Example:
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
font.size.lg;
|
|
155
|
+
font.weight.bold;
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Font Registration
|
|
159
|
+
|
|
160
|
+
Use static imports (e.g. via bundler `?url`):
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
import sourceRegular from "./assets/source-code-pro-regular.ttf?url";
|
|
164
|
+
import sourceItalic from "./assets/source-code-pro-italic.ttf?url";
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Register Fonts
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
import { Font } from "@papack/pdf";
|
|
171
|
+
|
|
172
|
+
Font.register({
|
|
173
|
+
family: family.mono,
|
|
174
|
+
src: new URL(sourceRegular, import.meta.url).pathname,
|
|
175
|
+
fontWeight: 400,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
Font.register({
|
|
179
|
+
family: family.mono,
|
|
180
|
+
src: new URL(sourceItalic, import.meta.url).pathname,
|
|
181
|
+
fontWeight: 400,
|
|
182
|
+
fontStyle: "italic",
|
|
183
|
+
});
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
- One `Font.register` call per weight/style
|
|
187
|
+
- `src` must resolve to a filesystem path
|
|
188
|
+
- `fontWeight` and `fontStyle` must match usage exactly
|
|
189
|
+
|
|
190
|
+
### Usage
|
|
191
|
+
|
|
192
|
+
```tsx
|
|
193
|
+
<Text style={{ fontFamily: family.mono, fontWeight: 400 }}>Code</Text>
|
|
194
|
+
```
|