react-vector-pdf 0.5.1 → 0.6.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.
- package/README.md +81 -3
- package/dist/index.d.ts +59 -3
- package/dist/react-vector-pdf.css +1 -0
- package/dist/react-vector-pdf.js +1194 -656
- package/package.json +70 -70
package/README.md
CHANGED
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
- **Advanced Components**:
|
|
17
17
|
- **PdfTable**: Supports auto-wrapping, **row spans**, **col spans**, **vertical alignment**, custom cell styling, **striped rows**, and **intelligent page breaking** (keeps spanned rows together).
|
|
18
18
|
- **PdfImage**: Renders images from URLs with options for flow or absolute positioning. Automatically handles page breaks in flow mode.
|
|
19
|
-
- **PdfList**: Bullet and numbered lists with auto-wrapping
|
|
20
|
-
- **PdfView**:
|
|
19
|
+
- **PdfList**: Bullet and numbered lists with auto-wrapping and **orphan protection**.
|
|
20
|
+
- **PdfView**: Advanced container with **perfect multi-page spanning**, support for borders, background colors (rendered strictly behind content), and granular **persistent padding** (padding is maintained across page breaks).
|
|
21
21
|
- **Global Document Options**:
|
|
22
22
|
- **Formatting**: A4, Letter, custom sizes, portrait/landscape orientation, distinct margins, base fonts.
|
|
23
23
|
- **Headers & Footers**: Custom render functions with full control.
|
|
@@ -95,6 +95,76 @@ import { PdfPreview, PdfText } from "react-vector-pdf";
|
|
|
95
95
|
|
|
96
96
|
**Note:** Changing props (like options, colors, fonts) will automatically regenerate and update the preview.
|
|
97
97
|
|
|
98
|
+
## Styling with CSS Classes
|
|
99
|
+
|
|
100
|
+
You can use standard CSS classes (including generic CSS or utility frameworks like **Tailwind CSS**) to style PDF components using the `className` prop. The library uses a special `useClassStyles` hook that renders a hidden DOM element to compute CSS styles and converts them into PDF-compatible properties.
|
|
101
|
+
|
|
102
|
+
### Supported CSS Properties
|
|
103
|
+
|
|
104
|
+
| CSS Property | PDF Mapping | Notes |
|
|
105
|
+
| ------------------ | ------------- | -------------------------------- |
|
|
106
|
+
| `color` | `color` | Supports hex and RGBA |
|
|
107
|
+
| `background-color` | `fillColor` | Supports hex and RGBA |
|
|
108
|
+
| `font-size` | `fontSize` | Converted from px to points (pt) |
|
|
109
|
+
| `font-weight` | `fontStyle` | ≥700 maps to 'bold' |
|
|
110
|
+
| `font-style` | `fontStyle` | Supports italic/bold/bolditalic |
|
|
111
|
+
| `text-align` | `align` | left/center/right/justify |
|
|
112
|
+
| `line-height` | `lineHeight` | Converted to multiplier |
|
|
113
|
+
| `padding-*` | `padding` | Converted from px to mm |
|
|
114
|
+
| `margin-*` | `margin` | Converted from px to mm |
|
|
115
|
+
| `border-width` | `borderWidth` | Uniform borders only, px to mm |
|
|
116
|
+
| `border-color` | `borderColor` | Supports hex and RGBA |
|
|
117
|
+
| `border-radius` | `radius` | Converted from px to mm |
|
|
118
|
+
| `gap` | `gap` | PdfView only, px to mm |
|
|
119
|
+
| `width` | `width` | Explicit widths, px to mm |
|
|
120
|
+
| `height` | `height` | Explicit heights, px to mm |
|
|
121
|
+
|
|
122
|
+
### Unsupported Properties
|
|
123
|
+
|
|
124
|
+
The following CSS properties are **not supported** and will trigger console warnings if used:
|
|
125
|
+
|
|
126
|
+
- `box-shadow`, `text-shadow`
|
|
127
|
+
- `z-index`
|
|
128
|
+
- `float`
|
|
129
|
+
- `overflow`
|
|
130
|
+
- `transform`
|
|
131
|
+
- Layout properties: `display: flex/grid`, `position: absolute/fixed`
|
|
132
|
+
|
|
133
|
+
> **Note**: PDF layout uses standard flow or absolute positioning. Complex CSS layout features like flexbox and grid are not available.
|
|
134
|
+
|
|
135
|
+
### Usage Examples
|
|
136
|
+
|
|
137
|
+
```tsx
|
|
138
|
+
// Using Tailwind CSS classes
|
|
139
|
+
<PdfText className="text-red-600 font-bold text-xl mb-4">
|
|
140
|
+
Styled with Tailwind!
|
|
141
|
+
</PdfText>
|
|
142
|
+
|
|
143
|
+
<PdfView className="border border-gray-300 p-4 bg-gray-50 rounded gap-4">
|
|
144
|
+
<PdfText className="text-gray-800">
|
|
145
|
+
This box uses utility classes for padding, border, and background.
|
|
146
|
+
</PdfText>
|
|
147
|
+
<PdfText>
|
|
148
|
+
Items inside this view are separated by a 1rem (approx 4mm) gap.
|
|
149
|
+
</PdfText>
|
|
150
|
+
</PdfView>
|
|
151
|
+
|
|
152
|
+
// Mixing className with direct props (direct props take precedence)
|
|
153
|
+
<PdfText className="text-blue-500" color="#FF0000">
|
|
154
|
+
This text will be red, not blue
|
|
155
|
+
</PdfText>
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### How It Works
|
|
159
|
+
|
|
160
|
+
The library:
|
|
161
|
+
|
|
162
|
+
1. Creates a hidden `<div>` element with the provided `className`
|
|
163
|
+
2. Uses `getComputedStyle()` to read the final computed CSS values
|
|
164
|
+
3. Converts pixel values to PDF units (mm for dimensions, pt for fonts)
|
|
165
|
+
4. Maps CSS properties to PDF renderer properties
|
|
166
|
+
5. Validates and warns about unsupported properties
|
|
167
|
+
|
|
98
168
|
## Components
|
|
99
169
|
|
|
100
170
|
### 1. `PdfDocument`
|
|
@@ -125,6 +195,8 @@ Renders a paragraph of text. Automatically wraps to the content width.
|
|
|
125
195
|
- `align` ('left' | 'center' | 'right' | 'justify'): Text alignment.
|
|
126
196
|
- `lineHeight` (number): Line height multiplier.
|
|
127
197
|
- `spacingBelow` (number): Vertical space (mm) to add after the paragraph.
|
|
198
|
+
- `className` (string): CSS class names for styling (supports Tailwind and other frameworks).
|
|
199
|
+
- `style` (React.CSSProperties): Inline styles object.
|
|
128
200
|
- `showInAllPages` (boolean): Make this text appear on multiple pages.
|
|
129
201
|
- `scope` ('all' | 'first-only' | 'except-first' | number[]): Which pages to show on.
|
|
130
202
|
|
|
@@ -171,6 +243,8 @@ A robust table component designed for dynamic data.
|
|
|
171
243
|
- `cellPadding`: Default padding for cells (number or object).
|
|
172
244
|
- `striped` (boolean): Enable alternating row colors.
|
|
173
245
|
- `repeatHeader` (boolean): Repeat header on each page (default: true).
|
|
246
|
+
- `className` (string): CSS class names for styling.
|
|
247
|
+
- `style` (React.CSSProperties): Inline styles object.
|
|
174
248
|
|
|
175
249
|
**Complex Cell Example:**
|
|
176
250
|
|
|
@@ -223,6 +297,8 @@ Embed images (JPEG/PNG).
|
|
|
223
297
|
- `align` ('left' | 'center' | 'right'): Horizontal alignment (flow mode only).
|
|
224
298
|
- `layout` ('fixed' | 'flow'): Layout mode (default: 'fixed').
|
|
225
299
|
- `sizing` ('fit' | 'fill' | 'auto'): How to size the image (default: 'fit').
|
|
300
|
+
- `className` (string): CSS class names for styling.
|
|
301
|
+
- `style` (React.CSSProperties): Inline styles object.
|
|
226
302
|
- `showInAllPages` (boolean): Make this image appear on multiple pages.
|
|
227
303
|
- `scope` ('all' | 'first-only' | 'except-first' | number[]): Which pages to show on.
|
|
228
304
|
|
|
@@ -252,7 +328,8 @@ A container component for grouping content, adding borders, backgrounds, or marg
|
|
|
252
328
|
|
|
253
329
|
**Props:**
|
|
254
330
|
|
|
255
|
-
- `style`: Object with `margin`, `padding` (granular: `marginTop`, `paddingLeft` etc.), `borderWidth`, `borderColor`, `fillColor`, `radius`.
|
|
331
|
+
- `style`: Object with `margin`, `padding` (granular: `marginTop`, `paddingLeft` etc.), `borderWidth`, `borderColor`, `fillColor`, `radius`, `gap`.
|
|
332
|
+
- `className` (string): CSS class names for styling (supports gap, padding, margin, borders, backgrounds).
|
|
256
333
|
- `x`, `y`, `w`, `h`: Optional props for **absolute positioning**. If provided, the view will be placed at exactly these coordinates.
|
|
257
334
|
- `children`: Nested components to render inside the view.
|
|
258
335
|
- `showInAllPages` (boolean): Make this view appear on multiple pages.
|
|
@@ -284,6 +361,7 @@ Renders bulleted or numbered lists.
|
|
|
284
361
|
- `indent`: Number (mm) for indentation. Default 5.
|
|
285
362
|
- `markerWidth`: Number (mm) for the marker area. Default 5.
|
|
286
363
|
- `spacing`: Number (mm) for space between items. Default 2.
|
|
364
|
+
- `className` (string): CSS class names for styling list items.
|
|
287
365
|
- `style`: TextStyle object for the list items.
|
|
288
366
|
|
|
289
367
|
```tsx
|
package/dist/index.d.ts
CHANGED
|
@@ -18,6 +18,7 @@ export declare interface BoxStyle {
|
|
|
18
18
|
paddingRight?: number;
|
|
19
19
|
paddingBottom?: number;
|
|
20
20
|
paddingLeft?: number;
|
|
21
|
+
gap?: number;
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
export declare interface CenterLabelOptions {
|
|
@@ -84,6 +85,8 @@ declare interface PdfImageProps {
|
|
|
84
85
|
align?: "left" | "center" | "right";
|
|
85
86
|
showInAllPages?: boolean;
|
|
86
87
|
scope?: "all" | "first-only" | "except-first" | number[];
|
|
88
|
+
className?: string;
|
|
89
|
+
style?: default_2.CSSProperties;
|
|
87
90
|
}
|
|
88
91
|
|
|
89
92
|
export declare const PdfList: default_2.FC<PdfListProps>;
|
|
@@ -95,6 +98,7 @@ declare interface PdfListProps {
|
|
|
95
98
|
indent?: number;
|
|
96
99
|
markerWidth?: number;
|
|
97
100
|
spacing?: number;
|
|
101
|
+
className?: string;
|
|
98
102
|
}
|
|
99
103
|
|
|
100
104
|
export declare interface PDFOptions {
|
|
@@ -135,7 +139,13 @@ export declare class PdfRenderer {
|
|
|
135
139
|
private cursorX;
|
|
136
140
|
private cursorY;
|
|
137
141
|
private contentWidth;
|
|
138
|
-
private
|
|
142
|
+
private reservedBottomHeight;
|
|
143
|
+
margin: {
|
|
144
|
+
top: number;
|
|
145
|
+
right: number;
|
|
146
|
+
bottom: number;
|
|
147
|
+
left: number;
|
|
148
|
+
};
|
|
139
149
|
private defaultFont;
|
|
140
150
|
private defaultColor;
|
|
141
151
|
private defaultLineHeight;
|
|
@@ -143,9 +153,14 @@ export declare class PdfRenderer {
|
|
|
143
153
|
private footerDrawer?;
|
|
144
154
|
private pendingTasks;
|
|
145
155
|
private opQueue;
|
|
146
|
-
|
|
156
|
+
generation: number;
|
|
157
|
+
private recordingStack;
|
|
147
158
|
private recurringItems;
|
|
148
159
|
constructor(opts?: PDFOptions);
|
|
160
|
+
startRecording(): void;
|
|
161
|
+
stopRecording(): (() => void)[];
|
|
162
|
+
playback(ops: Array<() => void>): void;
|
|
163
|
+
private drawOp;
|
|
149
164
|
get instance(): jsPDF;
|
|
150
165
|
get width(): number;
|
|
151
166
|
get height(): number;
|
|
@@ -153,6 +168,15 @@ export declare class PdfRenderer {
|
|
|
153
168
|
get contentRight(): number;
|
|
154
169
|
get contentTop(): number;
|
|
155
170
|
get contentBottom(): number;
|
|
171
|
+
private indentStack;
|
|
172
|
+
private currentIndent;
|
|
173
|
+
pushIndent(left: number, right: number): void;
|
|
174
|
+
popIndent(): void;
|
|
175
|
+
/**
|
|
176
|
+
* Returns the top Y position for content on a given page, accounting for recurring items.
|
|
177
|
+
* This is useful for drawing background boxes that shouldn't overlap headers.
|
|
178
|
+
*/
|
|
179
|
+
getSafeContentTop(pageNum: number): number;
|
|
156
180
|
get contentHeight(): number;
|
|
157
181
|
get contentAreaWidth(): number;
|
|
158
182
|
get baseFont(): {
|
|
@@ -161,6 +185,14 @@ export declare class PdfRenderer {
|
|
|
161
185
|
size: number;
|
|
162
186
|
};
|
|
163
187
|
get baseLineHeight(): number;
|
|
188
|
+
private verticalPaddingStack;
|
|
189
|
+
pushVerticalPadding(top: number, bottom: number): void;
|
|
190
|
+
popVerticalPadding(): void;
|
|
191
|
+
get currentVerticalPadding(): {
|
|
192
|
+
top: number;
|
|
193
|
+
bottom: number;
|
|
194
|
+
};
|
|
195
|
+
setReservedHeight(h: number): void;
|
|
164
196
|
resetFlowCursor(): void;
|
|
165
197
|
reset(): void;
|
|
166
198
|
setHeaderFooter(header?: (pdf: jsPDF, pageNum: number, pageCount: number, renderer: PdfRenderer) => void, footer?: (pdf: jsPDF, pageNum: number, pageCount: number, renderer: PdfRenderer) => void): void;
|
|
@@ -178,6 +210,7 @@ export declare class PdfRenderer {
|
|
|
178
210
|
h?: number;
|
|
179
211
|
mime?: "PNG" | "JPEG";
|
|
180
212
|
align?: "left" | "center" | "right";
|
|
213
|
+
radius?: number;
|
|
181
214
|
}): Promise<{
|
|
182
215
|
width: number;
|
|
183
216
|
height: number;
|
|
@@ -215,6 +248,21 @@ export declare class PdfRenderer {
|
|
|
215
248
|
}): void;
|
|
216
249
|
save(filename: string): void;
|
|
217
250
|
getBlobUrl(): URL;
|
|
251
|
+
/**
|
|
252
|
+
* Injects a filled rectangle at the beginning of the page stream.
|
|
253
|
+
* This ensures the background is drawn BEHIND all other content on the page.
|
|
254
|
+
*/
|
|
255
|
+
injectFill(pageNum: number, rect: {
|
|
256
|
+
x: number;
|
|
257
|
+
y: number;
|
|
258
|
+
w: number;
|
|
259
|
+
h: number;
|
|
260
|
+
}, color: string, radius?: number | {
|
|
261
|
+
tl?: number;
|
|
262
|
+
tr?: number;
|
|
263
|
+
br?: number;
|
|
264
|
+
bl?: number;
|
|
265
|
+
}): void;
|
|
218
266
|
}
|
|
219
267
|
|
|
220
268
|
export declare const PdfTable: default_2.FC<PdfTableProps>;
|
|
@@ -237,6 +285,9 @@ declare interface PdfTableProps {
|
|
|
237
285
|
headerHeight?: number;
|
|
238
286
|
repeatHeader?: boolean;
|
|
239
287
|
striped?: boolean;
|
|
288
|
+
className?: string;
|
|
289
|
+
style?: default_2.CSSProperties;
|
|
290
|
+
stripeColor?: string;
|
|
240
291
|
}
|
|
241
292
|
|
|
242
293
|
export declare const PdfText: default_2.FC<PdfTextProps>;
|
|
@@ -247,18 +298,23 @@ declare interface PdfTextProps extends TextStyle {
|
|
|
247
298
|
y?: number;
|
|
248
299
|
maxWidth?: number;
|
|
249
300
|
spacingBelow?: number;
|
|
301
|
+
className?: string;
|
|
302
|
+
style?: default_2.CSSProperties;
|
|
250
303
|
}
|
|
251
304
|
|
|
252
305
|
export declare const PdfView: default_2.FC<PdfViewProps>;
|
|
253
306
|
|
|
254
307
|
declare interface PdfViewProps {
|
|
255
|
-
style?: ViewStyle;
|
|
308
|
+
style?: ViewStyle | default_2.CSSProperties;
|
|
309
|
+
className?: string;
|
|
256
310
|
children?: default_2.ReactNode;
|
|
257
311
|
debug?: boolean;
|
|
258
312
|
x?: number;
|
|
259
313
|
y?: number;
|
|
260
314
|
w?: number;
|
|
261
315
|
h?: number;
|
|
316
|
+
showInAllPages?: boolean;
|
|
317
|
+
scope?: "all" | "first-only" | "except-first" | number[];
|
|
262
318
|
}
|
|
263
319
|
|
|
264
320
|
declare interface TableColumn {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
.pdf-preview-hidden{display:none!important}.pdf-preview-iframe{border:none}.pdf-preview-placeholder{width:100%;height:100%;display:flex;align-items:center;justify-content:center;background-color:#f3f4f6;color:#6b7280}
|