@oxog/printkit 1.0.0 → 1.1.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 +85 -18
- package/dist/chunk-E7TXCY3N.cjs +491 -0
- package/dist/chunk-E7TXCY3N.cjs.map +1 -0
- package/dist/chunk-N44HS34F.js +491 -0
- package/dist/chunk-N44HS34F.js.map +1 -0
- package/dist/errors-COjJm-gl.d.cts +291 -0
- package/dist/errors-COjJm-gl.d.ts +291 -0
- package/dist/index.cjs +1 -486
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +32 -273
- package/dist/index.d.ts +32 -273
- package/dist/index.js +1 -486
- package/dist/index.js.map +1 -1
- package/dist/react.cjs +1 -486
- package/dist/react.cjs.map +1 -1
- package/dist/react.d.cts +10 -125
- package/dist/react.d.ts +10 -125
- package/dist/react.js +1 -486
- package/dist/react.js.map +1 -1
- package/package.json +19 -11
package/README.md
CHANGED
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
| **Print Queue** | Batch printing with priority and retry |
|
|
30
30
|
| **React Integration** | Hooks (`usePrint`, `usePDF`) and Components |
|
|
31
31
|
| **Zero Dependencies** | No runtime dependencies |
|
|
32
|
-
| **
|
|
32
|
+
| **Small Bundle** | ~46 KB minified (~14 KB gzipped), tree-shakeable |
|
|
33
33
|
|
|
34
34
|
## Installation
|
|
35
35
|
|
|
@@ -94,20 +94,44 @@ const base64 = await pdf.toBase64('#content')
|
|
|
94
94
|
|
|
95
95
|
// Get as Data URL
|
|
96
96
|
const dataUrl = await pdf.toDataURL('#content')
|
|
97
|
+
|
|
98
|
+
// Options: page layout, JPEG quality, render scale, metadata, progress
|
|
99
|
+
await pdf.download('#content', 'report.pdf', {
|
|
100
|
+
pageSize: 'A4',
|
|
101
|
+
margin: '15mm',
|
|
102
|
+
metadata: { title: 'Quarterly report', author: 'Finance' },
|
|
103
|
+
onProgress: (percent) => console.log(percent),
|
|
104
|
+
})
|
|
97
105
|
```
|
|
98
106
|
|
|
107
|
+
How it works: the element is laid out at the printable width of the page,
|
|
108
|
+
captured with its computed styles into an SVG `<foreignObject>`, rasterized
|
|
109
|
+
to a canvas and written as one JPEG image per page (content taller than a page
|
|
110
|
+
is split across pages). Text in the PDF is therefore not selectable. Same-origin
|
|
111
|
+
and data-URL images, canvases and form values are included; cross-origin images
|
|
112
|
+
without CORS headers, web fonts (the system fallback font is used), CSS
|
|
113
|
+
`::before`/`::after` content and CSS background images are not. Browsers that
|
|
114
|
+
refuse to read back such canvases reject with a `PDF_EXPORT_FAILED` error.
|
|
115
|
+
|
|
116
|
+
Several elements (`['#cover', '#report']`) are exported one after another, each
|
|
117
|
+
starting on a new page. The `styles` option is applied to the exported content
|
|
118
|
+
only (including its `@media print` rules), and `header`/`footer` are drawn on
|
|
119
|
+
every page with the real `{page}` and `{pages}`. `styleSheets` is not used for
|
|
120
|
+
PDF export.
|
|
121
|
+
|
|
99
122
|
## React Integration
|
|
100
123
|
|
|
101
124
|
```tsx
|
|
102
|
-
import {
|
|
103
|
-
|
|
104
|
-
|
|
125
|
+
import { useRef } from 'react'
|
|
126
|
+
import {
|
|
127
|
+
PrintProvider,
|
|
128
|
+
usePrint,
|
|
105
129
|
usePDF,
|
|
106
|
-
PrintButton,
|
|
107
130
|
Printable,
|
|
108
131
|
PageBreak,
|
|
109
132
|
NoPrint,
|
|
110
|
-
PrintOnly
|
|
133
|
+
PrintOnly,
|
|
134
|
+
type PrintableRef,
|
|
111
135
|
} from '@oxog/printkit/react'
|
|
112
136
|
|
|
113
137
|
function App() {
|
|
@@ -121,17 +145,26 @@ function App() {
|
|
|
121
145
|
function Invoice() {
|
|
122
146
|
const { print, isPrinting } = usePrint()
|
|
123
147
|
const { download, isExporting } = usePDF()
|
|
124
|
-
|
|
148
|
+
// A Printable ref exposes print() and getElement()
|
|
149
|
+
const contentRef = useRef<PrintableRef>(null)
|
|
150
|
+
|
|
151
|
+
const handlePrint = () => contentRef.current?.print()
|
|
152
|
+
const handleDownload = () => {
|
|
153
|
+
const element = contentRef.current?.getElement()
|
|
154
|
+
if (element) void download(element, 'invoice.pdf')
|
|
155
|
+
}
|
|
125
156
|
|
|
126
157
|
return (
|
|
127
158
|
<div>
|
|
128
159
|
<NoPrint>
|
|
129
|
-
<button onClick={
|
|
160
|
+
<button onClick={handlePrint}>
|
|
130
161
|
{isPrinting ? 'Printing...' : 'Print'}
|
|
131
162
|
</button>
|
|
132
|
-
<button onClick={
|
|
163
|
+
<button onClick={handleDownload}>
|
|
133
164
|
{isExporting ? 'Exporting...' : 'Download PDF'}
|
|
134
165
|
</button>
|
|
166
|
+
{/* usePrint().print takes an element or a selector */}
|
|
167
|
+
<button onClick={() => print('#invoice-summary')}>Print summary</button>
|
|
135
168
|
</NoPrint>
|
|
136
169
|
|
|
137
170
|
<Printable ref={contentRef}>
|
|
@@ -147,6 +180,8 @@ function Invoice() {
|
|
|
147
180
|
<p>Printed on: {new Date().toLocaleDateString()}</p>
|
|
148
181
|
</PrintOnly>
|
|
149
182
|
</Printable>
|
|
183
|
+
|
|
184
|
+
<div id="invoice-summary">Total: $1,234</div>
|
|
150
185
|
</div>
|
|
151
186
|
)
|
|
152
187
|
}
|
|
@@ -212,11 +247,12 @@ await printer.printTable({
|
|
|
212
247
|
```typescript
|
|
213
248
|
import { createPrintQueue } from '@oxog/printkit'
|
|
214
249
|
|
|
215
|
-
|
|
250
|
+
// Jobs run one at a time; the options are defaults for every job
|
|
251
|
+
const queue = createPrintQueue({ pageSize: 'A4' })
|
|
216
252
|
|
|
217
|
-
// Add jobs
|
|
218
|
-
queue.add({ content: '#invoice1', priority: 'high' })
|
|
219
|
-
queue.add({ content: '#invoice2', priority: 'normal' })
|
|
253
|
+
// Add jobs (retries: automatic extra attempts after a failure)
|
|
254
|
+
queue.add({ content: '#invoice1', priority: 'high', retries: 2 })
|
|
255
|
+
const id = queue.add({ content: '#invoice2', priority: 'normal' })
|
|
220
256
|
queue.add({ content: '#invoice3', priority: 'low' })
|
|
221
257
|
|
|
222
258
|
// Event listeners
|
|
@@ -229,6 +265,9 @@ await queue.printAll()
|
|
|
229
265
|
|
|
230
266
|
// Or process one by one
|
|
231
267
|
await queue.printNext()
|
|
268
|
+
|
|
269
|
+
// Put a failed job back in the queue
|
|
270
|
+
if (queue.getJob(id)?.status === 'failed') queue.retry(id)
|
|
232
271
|
```
|
|
233
272
|
|
|
234
273
|
## Configuration Options
|
|
@@ -240,19 +279,25 @@ interface PrintOptions {
|
|
|
240
279
|
pageSize?: 'A3' | 'A4' | 'A5' | 'Letter' | 'Legal' | 'Tabloid' | { width, height }
|
|
241
280
|
orientation?: 'portrait' | 'landscape'
|
|
242
281
|
|
|
243
|
-
// Margins (number = mm, or
|
|
244
|
-
margin?: number | { top, right, bottom, left }
|
|
282
|
+
// Margins (number = mm, or a CSS length like '1in', '2cm'), all sides or per side
|
|
283
|
+
margin?: number | string | { top, right, bottom, left }
|
|
245
284
|
|
|
246
|
-
// Header/Footer (string or function)
|
|
285
|
+
// Header/Footer (HTML template string or function returning HTML)
|
|
247
286
|
header?: string | ((info: PageInfo) => string)
|
|
248
287
|
footer?: string | ((info: PageInfo) => string)
|
|
249
|
-
// Placeholders: {page}, {pages}, {title}, {date}, {time}
|
|
288
|
+
// Placeholders: {page}, {pages}, {title}, {date}, {time} ({title} is HTML-escaped)
|
|
289
|
+
// Printing: a string using {page}/{pages} is shown on every page with the real
|
|
290
|
+
// numbers via CSS @page margin boxes (as plain text) in Chromium 131+ (Chrome,
|
|
291
|
+
// Edge). Firefox and Safari do not support margin boxes: there it prints once
|
|
292
|
+
// and {page}/{pages} are left empty. Other templates print once, at the top or
|
|
293
|
+
// bottom of the document; functions get page 1 of 1. PDF export draws header
|
|
294
|
+
// and footer on every page with the real numbers.
|
|
250
295
|
|
|
251
296
|
// Styling
|
|
252
297
|
styles?: string | string[] // Custom CSS
|
|
253
298
|
styleSheets?: string[] // External stylesheets
|
|
254
299
|
copyStyles?: boolean // Copy page styles (default: true)
|
|
255
|
-
printBackground?: boolean // Print backgrounds (default: true)
|
|
300
|
+
printBackground?: boolean // Print backgrounds (default: true); false removes them
|
|
256
301
|
|
|
257
302
|
// Page breaks (CSS selectors)
|
|
258
303
|
pageBreakBefore?: string // e.g., '.chapter'
|
|
@@ -267,6 +312,28 @@ interface PrintOptions {
|
|
|
267
312
|
}
|
|
268
313
|
```
|
|
269
314
|
|
|
315
|
+
## Security
|
|
316
|
+
|
|
317
|
+
The print document is rendered in a hidden same-origin iframe, so anything that
|
|
318
|
+
runs there runs with your page's privileges. PrintKit therefore:
|
|
319
|
+
|
|
320
|
+
- sanitizes `printHTML()` / `printURL()` content and `PDFExporter.downloadHTML()`
|
|
321
|
+
input with an allow-list of HTML and SVG elements and attributes (no scripts,
|
|
322
|
+
frames, plugins, `<base>`, `<meta>`, MathML, SVG animation, `on*` handlers or
|
|
323
|
+
`javascript:` URLs), re-parsing its own output until it is stable to defeat
|
|
324
|
+
mutation XSS; `sanitizeHTML()` is exported for your own use. For untrusted
|
|
325
|
+
HTML, a dedicated sanitizer such as [DOMPurify](https://github.com/cure53/DOMPurify)
|
|
326
|
+
is still the better choice,
|
|
327
|
+
- gives the print document a Content-Security-Policy (`script-src 'none'`),
|
|
328
|
+
so script cannot run in the print frame even if something slips through,
|
|
329
|
+
- HTML-escapes the `title`, image URLs/alt text, table cells and style options,
|
|
330
|
+
- strips `<script>` elements and inline handlers from printed page elements, so
|
|
331
|
+
your page scripts do not run a second time inside the print frame,
|
|
332
|
+
- never copies the value of password or file inputs into the print document.
|
|
333
|
+
|
|
334
|
+
`header`/`footer` templates and functions are HTML that you control: escape any
|
|
335
|
+
user data you put into them yourself (the `{title}` placeholder is escaped for you).
|
|
336
|
+
|
|
270
337
|
## CSS Classes
|
|
271
338
|
|
|
272
339
|
PrintKit provides utility classes for print control:
|