@oxog/printkit 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Ersin KOC
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,367 @@
1
+ # PrintKit
2
+
3
+ <div align="center">
4
+ <h3>Zero-dependency print management toolkit for web applications</h3>
5
+ <p>Print preview, PDF export, and React integration - all in one tiny package</p>
6
+ </div>
7
+
8
+ <div align="center">
9
+
10
+ [![npm version](https://img.shields.io/npm/v/@oxog/printkit.svg)](https://www.npmjs.com/package/@oxog/printkit)
11
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/@oxog/printkit)](https://bundlephobia.com/package/@oxog/printkit)
12
+ [![license](https://img.shields.io/npm/l/@oxog/printkit.svg)](LICENSE)
13
+ [![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue.svg)](https://www.typescriptlang.org/)
14
+
15
+ </div>
16
+
17
+ ---
18
+
19
+ ## Features
20
+
21
+ | Feature | Description |
22
+ |---------|-------------|
23
+ | **Print Anything** | Elements, HTML strings, URLs, images, JSON, tables |
24
+ | **Print Preview** | Modal with zoom, navigation, and keyboard shortcuts |
25
+ | **PDF Export** | Download, Blob, Base64, Data URL - no external deps |
26
+ | **Page Layouts** | A3, A4, A5, Letter, Legal, Tabloid, custom sizes |
27
+ | **Headers/Footers** | Template strings with `{page}`, `{pages}`, `{title}`, `{date}` |
28
+ | **Page Breaks** | CSS classes and programmatic control |
29
+ | **Print Queue** | Batch printing with priority and retry |
30
+ | **React Integration** | Hooks (`usePrint`, `usePDF`) and Components |
31
+ | **Zero Dependencies** | No runtime dependencies |
32
+ | **Tiny Bundle** | < 40KB minified |
33
+
34
+ ## Installation
35
+
36
+ ```bash
37
+ npm install @oxog/printkit
38
+ ```
39
+
40
+ ## Quick Start
41
+
42
+ ### Basic Printing
43
+
44
+ ```typescript
45
+ import { createPrinter } from '@oxog/printkit'
46
+
47
+ const printer = createPrinter()
48
+
49
+ // Print an element
50
+ await printer.print('#invoice')
51
+
52
+ // Print with options
53
+ await printer.print('#report', {
54
+ title: 'Sales Report',
55
+ pageSize: 'A4',
56
+ orientation: 'landscape',
57
+ header: 'Confidential',
58
+ footer: 'Page {page} of {pages}'
59
+ })
60
+ ```
61
+
62
+ ### Print Preview
63
+
64
+ ```typescript
65
+ import { createPrintPreview } from '@oxog/printkit'
66
+
67
+ const preview = createPrintPreview('#content', {
68
+ pageSize: 'A4',
69
+ modal: { zoom: 75 }
70
+ })
71
+
72
+ preview.open() // Open preview modal
73
+ preview.zoomIn() // Zoom controls
74
+ preview.print() // Print from preview
75
+ preview.download() // Download as PDF
76
+ preview.close() // Close modal
77
+ ```
78
+
79
+ ### PDF Export
80
+
81
+ ```typescript
82
+ import { createPDFExporter } from '@oxog/printkit'
83
+
84
+ const pdf = createPDFExporter()
85
+
86
+ // Download as PDF file
87
+ await pdf.download('#content', 'document.pdf')
88
+
89
+ // Get as Blob (for upload, etc.)
90
+ const blob = await pdf.toBlob('#content')
91
+
92
+ // Get as Base64
93
+ const base64 = await pdf.toBase64('#content')
94
+
95
+ // Get as Data URL
96
+ const dataUrl = await pdf.toDataURL('#content')
97
+ ```
98
+
99
+ ## React Integration
100
+
101
+ ```tsx
102
+ import {
103
+ PrintProvider,
104
+ usePrint,
105
+ usePDF,
106
+ PrintButton,
107
+ Printable,
108
+ PageBreak,
109
+ NoPrint,
110
+ PrintOnly
111
+ } from '@oxog/printkit/react'
112
+
113
+ function App() {
114
+ return (
115
+ <PrintProvider defaultOptions={{ pageSize: 'A4' }}>
116
+ <Invoice />
117
+ </PrintProvider>
118
+ )
119
+ }
120
+
121
+ function Invoice() {
122
+ const { print, isPrinting } = usePrint()
123
+ const { download, isExporting } = usePDF()
124
+ const contentRef = useRef(null)
125
+
126
+ return (
127
+ <div>
128
+ <NoPrint>
129
+ <button onClick={() => print(contentRef.current)}>
130
+ {isPrinting ? 'Printing...' : 'Print'}
131
+ </button>
132
+ <button onClick={() => download(contentRef.current, 'invoice.pdf')}>
133
+ {isExporting ? 'Exporting...' : 'Download PDF'}
134
+ </button>
135
+ </NoPrint>
136
+
137
+ <Printable ref={contentRef}>
138
+ <h1>Invoice #12345</h1>
139
+ <p>Content here...</p>
140
+
141
+ <PageBreak />
142
+
143
+ <h2>Page 2</h2>
144
+ <p>More content...</p>
145
+
146
+ <PrintOnly>
147
+ <p>Printed on: {new Date().toLocaleDateString()}</p>
148
+ </PrintOnly>
149
+ </Printable>
150
+ </div>
151
+ )
152
+ }
153
+ ```
154
+
155
+ ### React Hooks
156
+
157
+ | Hook | Description |
158
+ |------|-------------|
159
+ | `usePrint()` | Print with loading/error states |
160
+ | `usePDF()` | PDF export with progress tracking |
161
+ | `usePrintPreview()` | Control preview modal |
162
+ | `usePrintContext()` | Access global print options |
163
+
164
+ ### React Components
165
+
166
+ | Component | Description |
167
+ |-----------|-------------|
168
+ | `<PrintProvider>` | Context provider for global options |
169
+ | `<Printable>` | Wrapper with ref-based print control |
170
+ | `<PrintButton>` | Customizable print trigger |
171
+ | `<PrintPreview>` | Preview modal component |
172
+ | `<PageBreak>` | Insert page break |
173
+ | `<NoPrint>` | Hide content when printing |
174
+ | `<PrintOnly>` | Show only when printing |
175
+
176
+ ## Print Methods
177
+
178
+ ```typescript
179
+ const printer = createPrinter()
180
+
181
+ // Print element by selector or reference
182
+ await printer.print('#content')
183
+ await printer.print(document.getElementById('content'))
184
+
185
+ // Print HTML string
186
+ await printer.printHTML('<h1>Hello World</h1>')
187
+
188
+ // Print from URL
189
+ await printer.printURL('/report.html')
190
+
191
+ // Print image
192
+ await printer.printImage('/chart.png')
193
+ await printer.printImages(['/chart1.png', '/chart2.png'])
194
+
195
+ // Print JSON with syntax highlighting
196
+ await printer.printJSON({ user: 'John', age: 30 }, {
197
+ syntaxHighlight: true
198
+ })
199
+
200
+ // Print table
201
+ await printer.printTable({
202
+ headers: ['Name', 'Email', 'Role'],
203
+ rows: [
204
+ ['John Doe', 'john@example.com', 'Admin'],
205
+ ['Jane Smith', 'jane@example.com', 'User']
206
+ ]
207
+ })
208
+ ```
209
+
210
+ ## Print Queue
211
+
212
+ ```typescript
213
+ import { createPrintQueue } from '@oxog/printkit'
214
+
215
+ const queue = createPrintQueue({ concurrency: 1 })
216
+
217
+ // Add jobs
218
+ queue.add({ content: '#invoice1', priority: 'high' })
219
+ queue.add({ content: '#invoice2', priority: 'normal' })
220
+ queue.add({ content: '#invoice3', priority: 'low' })
221
+
222
+ // Event listeners
223
+ queue.on('jobStart', (job) => console.log('Started:', job.id))
224
+ queue.on('jobComplete', (job) => console.log('Done:', job.id))
225
+ queue.on('jobError', (job, error) => console.error('Failed:', error))
226
+
227
+ // Process queue
228
+ await queue.printAll()
229
+
230
+ // Or process one by one
231
+ await queue.printNext()
232
+ ```
233
+
234
+ ## Configuration Options
235
+
236
+ ```typescript
237
+ interface PrintOptions {
238
+ // Document
239
+ title?: string
240
+ pageSize?: 'A3' | 'A4' | 'A5' | 'Letter' | 'Legal' | 'Tabloid' | { width, height }
241
+ orientation?: 'portrait' | 'landscape'
242
+
243
+ // Margins (number = mm, or use '1in', '2cm', etc.)
244
+ margin?: number | { top, right, bottom, left }
245
+
246
+ // Header/Footer (string or function)
247
+ header?: string | ((info: PageInfo) => string)
248
+ footer?: string | ((info: PageInfo) => string)
249
+ // Placeholders: {page}, {pages}, {title}, {date}, {time}
250
+
251
+ // Styling
252
+ styles?: string | string[] // Custom CSS
253
+ styleSheets?: string[] // External stylesheets
254
+ copyStyles?: boolean // Copy page styles (default: true)
255
+ printBackground?: boolean // Print backgrounds (default: true)
256
+
257
+ // Page breaks (CSS selectors)
258
+ pageBreakBefore?: string // e.g., '.chapter'
259
+ pageBreakAfter?: string // e.g., '.section-end'
260
+ avoidBreakInside?: string // e.g., '.card, table'
261
+
262
+ // Callbacks
263
+ onBeforePrint?: () => void | Promise<void>
264
+ onAfterPrint?: () => void
265
+ onError?: (error: PrintError) => void
266
+ onProgress?: (percent: number) => void // PDF export only
267
+ }
268
+ ```
269
+
270
+ ## CSS Classes
271
+
272
+ PrintKit provides utility classes for print control:
273
+
274
+ ```html
275
+ <!-- Hide when printing -->
276
+ <nav class="print-hide">Navigation</nav>
277
+
278
+ <!-- Show only when printing -->
279
+ <div class="print-only">Printed on: 2024-01-01</div>
280
+
281
+ <!-- Page breaks -->
282
+ <div class="print-break-before">Starts on new page</div>
283
+ <div class="print-break-after">Next content on new page</div>
284
+ <div class="print-avoid-break">Keep together on same page</div>
285
+ ```
286
+
287
+ ## Utility Functions
288
+
289
+ ```typescript
290
+ import {
291
+ // Detection
292
+ isPrintSupported,
293
+ isPDFSupported,
294
+ isBrowser,
295
+
296
+ // Page breaks
297
+ pageBreak, // Create page break element
298
+ avoidBreak, // Mark element to avoid breaks
299
+ breakBefore, // Mark element to break before
300
+ breakAfter, // Mark element to break after
301
+
302
+ // Page dimensions
303
+ getPageDimensions,
304
+ getPagePixelDimensions,
305
+
306
+ // Style injection
307
+ injectPrintStyles,
308
+ removePrintStyles,
309
+ removeAllPrintStyles,
310
+
311
+ // Constants
312
+ PAGE_SIZES, // { A4: {...}, Letter: {...}, ... }
313
+ PRINT_HIDE_CLASS, // 'print-hide'
314
+ PRINT_ONLY_CLASS, // 'print-only'
315
+ } from '@oxog/printkit'
316
+ ```
317
+
318
+ ## Browser Support
319
+
320
+ | Browser | Version |
321
+ |---------|---------|
322
+ | Chrome | 80+ |
323
+ | Firefox | 75+ |
324
+ | Safari | 13.1+ |
325
+ | Edge | 80+ |
326
+
327
+ ## TypeScript
328
+
329
+ PrintKit is written in TypeScript and provides full type definitions:
330
+
331
+ ```typescript
332
+ import type {
333
+ PrintOptions,
334
+ PDFOptions,
335
+ PreviewOptions,
336
+ PrintTarget,
337
+ PageSize,
338
+ Orientation,
339
+ Printer,
340
+ PDFExporter,
341
+ PrintQueue,
342
+ PrintJob,
343
+ } from '@oxog/printkit'
344
+ ```
345
+
346
+ ## Documentation
347
+
348
+ For detailed documentation, visit [printkit.oxog.dev](https://printkit.oxog.dev)
349
+
350
+ - [Getting Started](https://printkit.oxog.dev/docs/getting-started)
351
+ - [API Reference](https://printkit.oxog.dev/docs/api)
352
+ - [Examples](https://printkit.oxog.dev/examples)
353
+ - [React Guide](https://printkit.oxog.dev/docs/react)
354
+
355
+ ## Contributing
356
+
357
+ Contributions are welcome! Please read [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
358
+
359
+ ## License
360
+
361
+ MIT - see [LICENSE](LICENSE) for details.
362
+
363
+ ---
364
+
365
+ <div align="center">
366
+ Made with care by <a href="https://github.com/ersinkoc">Ersin KOC</a>
367
+ </div>