qrlayout-core 1.0.0 → 1.0.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 +99 -72
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,108 +1,135 @@
|
|
|
1
1
|
# qrlayout-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A powerful, framework-agnostic engine for designing and printing QR code sticker layouts. Create pixel-perfect labels with text, QR codes, and images, and export them to **PNG**, **PDF**, or **ZPL** (Zebra Programming Language).
|
|
4
4
|
|
|
5
5
|
> [!NOTE]
|
|
6
|
-
> This package
|
|
6
|
+
> This package is the core rendering engine. for a visual drag-and-drop designer, check out the [QR Layout Workspace](https://github.com/shashi089/qr-code-layout-generate-tool).
|
|
7
7
|
|
|
8
|
-
##
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- 📏 **Precise Layouts**: Define stickers in `mm`, `cm`, `in`, or `px`.
|
|
11
|
+
- 🖼️ **Multiple Formats**: Export to Canvas (preview), PNG/JPEG (image), PDF (print), or ZPL (industrial thermal printers).
|
|
12
|
+
- 🧩 **Dynamic Content**: Use variable placeholders (e.g., `{{name}}`, `{{sku}}`) to batch generate unique stickers.
|
|
13
|
+
- 📦 **Lightweight**: Minimal dependencies. PDF export is optional to keep bundle size small.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
9
16
|
|
|
10
17
|
```bash
|
|
11
18
|
npm install qrlayout-core
|
|
12
19
|
```
|
|
13
20
|
|
|
14
|
-
## Quick
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
### 1. Define a Layout
|
|
24
|
+
|
|
25
|
+
A layout is a JSON object describing the sticker dimensions and elements.
|
|
15
26
|
|
|
16
27
|
```ts
|
|
17
|
-
import {
|
|
28
|
+
import { type StickerLayout } from "qrlayout-core";
|
|
18
29
|
|
|
19
|
-
const
|
|
20
|
-
id: "layout
|
|
21
|
-
name: "Badge",
|
|
22
|
-
width: 100,
|
|
23
|
-
height: 60,
|
|
30
|
+
const myLayout: StickerLayout = {
|
|
31
|
+
id: "badge-layout",
|
|
32
|
+
name: "Conference Badge",
|
|
33
|
+
width: 100, // 100mm width
|
|
34
|
+
height: 60, // 60mm height
|
|
24
35
|
unit: "mm",
|
|
25
36
|
elements: [
|
|
26
|
-
{
|
|
27
|
-
|
|
28
|
-
|
|
37
|
+
{
|
|
38
|
+
id: "title",
|
|
39
|
+
type: "text",
|
|
40
|
+
x: 0, y: 5, w: 100, h: 10,
|
|
41
|
+
content: "VISITOR PASS",
|
|
42
|
+
style: { fontSize: 14, fontWeight: "bold", textAlign: "center" }
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
id: "name",
|
|
46
|
+
type: "text",
|
|
47
|
+
x: 5, y: 25, w: 60, h: 10,
|
|
48
|
+
content: "{{name}}", // Placeholder
|
|
49
|
+
style: { fontSize: 12 }
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
id: "qr-code",
|
|
53
|
+
type: "qr",
|
|
54
|
+
x: 70, y: 20, w: 25, h: 25,
|
|
55
|
+
content: "{{visitorId}}" // Placeholder
|
|
56
|
+
}
|
|
29
57
|
]
|
|
30
58
|
};
|
|
59
|
+
```
|
|
31
60
|
|
|
32
|
-
|
|
33
|
-
const printer = new StickerPrinter();
|
|
61
|
+
### 2. Render or Export
|
|
34
62
|
|
|
35
|
-
|
|
36
|
-
const canvas = document.getElementById("preview-canvas") as HTMLCanvasElement;
|
|
37
|
-
await printer.renderToCanvas(layout, data, canvas);
|
|
63
|
+
Pass data to the `StickerPrinter` to generate outputs.
|
|
38
64
|
|
|
39
|
-
|
|
40
|
-
|
|
65
|
+
```ts
|
|
66
|
+
import { StickerPrinter } from "qrlayout-core";
|
|
41
67
|
|
|
42
|
-
//
|
|
43
|
-
const
|
|
44
|
-
|
|
68
|
+
// Data to fill placeholders
|
|
69
|
+
const data = {
|
|
70
|
+
name: "Alice Johnson",
|
|
71
|
+
visitorId: "https://example.com/visitors/alice"
|
|
72
|
+
};
|
|
45
73
|
|
|
46
|
-
|
|
47
|
-
const zpl = printer.exportToZPL(layout, [data]);
|
|
48
|
-
console.log(zpl[0]);
|
|
49
|
-
```
|
|
74
|
+
const printer = new StickerPrinter();
|
|
50
75
|
|
|
51
|
-
|
|
76
|
+
// --- Option A: Render to HTML Canvas (Browser) ---
|
|
77
|
+
const canvas = document.querySelector("#preview") as HTMLCanvasElement;
|
|
78
|
+
await printer.renderToCanvas(myLayout, data, canvas);
|
|
79
|
+
|
|
80
|
+
// --- Option B: Get an Image URL (PNG) ---
|
|
81
|
+
const imageUrl = await printer.renderToDataURL(myLayout, data, { format: "png", dpi: 300 });
|
|
82
|
+
console.log(imageUrl); // "data:image/png;base64,..."
|
|
83
|
+
|
|
84
|
+
// --- Option C: Generate ZPL for Thermal Printers ---
|
|
85
|
+
const zplCommands = printer.exportToZPL(myLayout, [data]);
|
|
86
|
+
console.log(zplCommands[0]); // "^XA^FO..."
|
|
52
87
|
|
|
53
|
-
```ts
|
|
54
|
-
type Unit = "mm" | "px" | "cm" | "in";
|
|
55
|
-
type ElementType = "text" | "qr" | "image";
|
|
56
|
-
|
|
57
|
-
interface StickerElement {
|
|
58
|
-
id: string;
|
|
59
|
-
type: ElementType;
|
|
60
|
-
x: number;
|
|
61
|
-
y: number;
|
|
62
|
-
w: number;
|
|
63
|
-
h: number;
|
|
64
|
-
content: string; // supports {{placeholders}}
|
|
65
|
-
style?: {
|
|
66
|
-
fontFamily?: string;
|
|
67
|
-
fontSize?: number;
|
|
68
|
-
fontWeight?: string | number;
|
|
69
|
-
textAlign?: "left" | "center" | "right";
|
|
70
|
-
color?: string;
|
|
71
|
-
backgroundColor?: string;
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
interface StickerLayout {
|
|
76
|
-
id: string;
|
|
77
|
-
name: string;
|
|
78
|
-
width: number;
|
|
79
|
-
height: number;
|
|
80
|
-
unit: Unit;
|
|
81
|
-
elements: StickerElement[];
|
|
82
|
-
backgroundColor?: string;
|
|
83
|
-
backgroundImage?: string;
|
|
84
|
-
}
|
|
85
88
|
```
|
|
86
89
|
|
|
87
|
-
##
|
|
90
|
+
## PDF Export (Optional)
|
|
91
|
+
|
|
92
|
+
To enable PDF export, you must install `jspdf`. This is an optional peer dependency to prevent bloating the core library for users who don't need PDF support.
|
|
88
93
|
|
|
89
|
-
|
|
90
|
-
- `renderToDataURL` for PNG/JPEG/WebP export
|
|
91
|
-
- `exportToPDF` for print-ready PDF (optional)
|
|
92
|
-
- `exportToZPL` for Zebra printers
|
|
94
|
+
### 1. Install jspdf
|
|
93
95
|
|
|
94
|
-
|
|
96
|
+
```bash
|
|
97
|
+
npm install jspdf
|
|
98
|
+
```
|
|
95
99
|
|
|
96
|
-
|
|
100
|
+
### 2. Use the PDF Module
|
|
97
101
|
|
|
98
102
|
```ts
|
|
99
103
|
import { exportToPDF } from "qrlayout-core/pdf";
|
|
104
|
+
|
|
105
|
+
// Generate a PDF with multiple stickers (e.g., standard A4 sheet logic can be handled by caller,
|
|
106
|
+
// this function currently outputs one page per sticker or as configured).
|
|
107
|
+
const pdfDoc = await exportToPDF(myLayout, [data, data2, data3]);
|
|
108
|
+
|
|
109
|
+
// Save the PDF
|
|
110
|
+
pdfDoc.save("badges.pdf");
|
|
100
111
|
```
|
|
101
112
|
|
|
102
|
-
|
|
113
|
+
## API Reference
|
|
114
|
+
|
|
115
|
+
### `StickerLayout` Interface
|
|
116
|
+
|
|
117
|
+
| Property | Type | Description |
|
|
118
|
+
|----------|------|-------------|
|
|
119
|
+
| `width` | `number` | Width of the sticker. |
|
|
120
|
+
| `height` | `number` | Height of the sticker. |
|
|
121
|
+
| `unit` | `"mm" \| "cm" \| "in" \| "px"` | Unit of measurement. |
|
|
122
|
+
| `elements` | `StickerElement[]` | List of items on the sticker. |
|
|
123
|
+
|
|
124
|
+
### `StickerElement` Interface
|
|
125
|
+
|
|
126
|
+
| Property | Type | Description |
|
|
127
|
+
|----------|------|-------------|
|
|
128
|
+
| `type` | `"text" \| "qr" \| "image"` | Type of element. |
|
|
129
|
+
| `x`, `y` | `number` | Position from top-left. |
|
|
130
|
+
| `w`, `h` | `number` | Width and height. |
|
|
131
|
+
| `content` | `string` | Text, URL, or image source. Supports `{{key}}` syntax. |
|
|
103
132
|
|
|
104
|
-
##
|
|
133
|
+
## License
|
|
105
134
|
|
|
106
|
-
|
|
107
|
-
- For ZPL, images are not embedded (text and QR only).
|
|
108
|
-
- When using external images in the browser, CORS headers are required or the canvas export will fail.
|
|
135
|
+
MIT
|