@oxog/printkit 1.0.0 → 1.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/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
- | **Tiny Bundle** | < 40KB minified |
32
+ | **Small Bundle** | ~46 KB minified (~14 KB gzipped), tree-shakeable |
33
33
 
34
34
  ## Installation
35
35
 
@@ -94,20 +94,38 @@ 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
+
99
116
  ## React Integration
100
117
 
101
118
  ```tsx
102
- import {
103
- PrintProvider,
104
- usePrint,
119
+ import { useRef } from 'react'
120
+ import {
121
+ PrintProvider,
122
+ usePrint,
105
123
  usePDF,
106
- PrintButton,
107
124
  Printable,
108
125
  PageBreak,
109
126
  NoPrint,
110
- PrintOnly
127
+ PrintOnly,
128
+ type PrintableRef,
111
129
  } from '@oxog/printkit/react'
112
130
 
113
131
  function App() {
@@ -121,17 +139,26 @@ function App() {
121
139
  function Invoice() {
122
140
  const { print, isPrinting } = usePrint()
123
141
  const { download, isExporting } = usePDF()
124
- const contentRef = useRef(null)
142
+ // A Printable ref exposes print() and getElement()
143
+ const contentRef = useRef<PrintableRef>(null)
144
+
145
+ const handlePrint = () => contentRef.current?.print()
146
+ const handleDownload = () => {
147
+ const element = contentRef.current?.getElement()
148
+ if (element) void download(element, 'invoice.pdf')
149
+ }
125
150
 
126
151
  return (
127
152
  <div>
128
153
  <NoPrint>
129
- <button onClick={() => print(contentRef.current)}>
154
+ <button onClick={handlePrint}>
130
155
  {isPrinting ? 'Printing...' : 'Print'}
131
156
  </button>
132
- <button onClick={() => download(contentRef.current, 'invoice.pdf')}>
157
+ <button onClick={handleDownload}>
133
158
  {isExporting ? 'Exporting...' : 'Download PDF'}
134
159
  </button>
160
+ {/* usePrint().print takes an element or a selector */}
161
+ <button onClick={() => print('#invoice-summary')}>Print summary</button>
135
162
  </NoPrint>
136
163
 
137
164
  <Printable ref={contentRef}>
@@ -147,6 +174,8 @@ function Invoice() {
147
174
  <p>Printed on: {new Date().toLocaleDateString()}</p>
148
175
  </PrintOnly>
149
176
  </Printable>
177
+
178
+ <div id="invoice-summary">Total: $1,234</div>
150
179
  </div>
151
180
  )
152
181
  }
@@ -212,11 +241,12 @@ await printer.printTable({
212
241
  ```typescript
213
242
  import { createPrintQueue } from '@oxog/printkit'
214
243
 
215
- const queue = createPrintQueue({ concurrency: 1 })
244
+ // Jobs run one at a time; the options are defaults for every job
245
+ const queue = createPrintQueue({ pageSize: 'A4' })
216
246
 
217
- // Add jobs
218
- queue.add({ content: '#invoice1', priority: 'high' })
219
- queue.add({ content: '#invoice2', priority: 'normal' })
247
+ // Add jobs (retries: automatic extra attempts after a failure)
248
+ queue.add({ content: '#invoice1', priority: 'high', retries: 2 })
249
+ const id = queue.add({ content: '#invoice2', priority: 'normal' })
220
250
  queue.add({ content: '#invoice3', priority: 'low' })
221
251
 
222
252
  // Event listeners
@@ -229,6 +259,9 @@ await queue.printAll()
229
259
 
230
260
  // Or process one by one
231
261
  await queue.printNext()
262
+
263
+ // Put a failed job back in the queue
264
+ if (queue.getJob(id)?.status === 'failed') queue.retry(id)
232
265
  ```
233
266
 
234
267
  ## Configuration Options
@@ -240,13 +273,13 @@ interface PrintOptions {
240
273
  pageSize?: 'A3' | 'A4' | 'A5' | 'Letter' | 'Legal' | 'Tabloid' | { width, height }
241
274
  orientation?: 'portrait' | 'landscape'
242
275
 
243
- // Margins (number = mm, or use '1in', '2cm', etc.)
244
- margin?: number | { top, right, bottom, left }
276
+ // Margins (number = mm, or a CSS length like '1in', '2cm'), all sides or per side
277
+ margin?: number | string | { top, right, bottom, left }
245
278
 
246
- // Header/Footer (string or function)
279
+ // Header/Footer (HTML template string or function returning HTML)
247
280
  header?: string | ((info: PageInfo) => string)
248
281
  footer?: string | ((info: PageInfo) => string)
249
- // Placeholders: {page}, {pages}, {title}, {date}, {time}
282
+ // Placeholders: {page}, {pages}, {title}, {date}, {time} ({title} is HTML-escaped)
250
283
 
251
284
  // Styling
252
285
  styles?: string | string[] // Custom CSS
@@ -267,6 +300,23 @@ interface PrintOptions {
267
300
  }
268
301
  ```
269
302
 
303
+ ## Security
304
+
305
+ The print document is rendered in a hidden same-origin iframe, so anything that
306
+ runs there runs with your page's privileges. PrintKit therefore:
307
+
308
+ - sanitizes `printHTML()` / `printURL()` content and `PDFExporter.downloadHTML()`
309
+ input (removes `<script>`, `<iframe>`, `<object>`, `<embed>`, `<base>`,
310
+ `<meta>`, `on*` handlers and `javascript:` URLs); `sanitizeHTML()` is exported
311
+ for your own use,
312
+ - HTML-escapes the `title`, image URLs/alt text, table cells and style options,
313
+ - strips `<script>` elements and inline handlers from printed page elements, so
314
+ your page scripts do not run a second time inside the print frame,
315
+ - never copies the value of password or file inputs into the print document.
316
+
317
+ `header`/`footer` templates and functions are HTML that you control: escape any
318
+ user data you put into them yourself (the `{title}` placeholder is escaped for you).
319
+
270
320
  ## CSS Classes
271
321
 
272
322
  PrintKit provides utility classes for print control: