A TypeScript library for generating TSPL (Taiwan Semiconductor Printer Language) / TSPL2 commands for thermal label printers.
npm install tspl-generator
For direct browser usage, you can download the latest browser bundle from:
- Strongly typed API with TypeScript
- Fluent interface for easy command chaining
- Support for all common TSPL/TSPL2 commands
- Measurement system support (inches and millimeters)
- Browser support via UMD bundle
import { TSPLPrinter, MeasurementSystem, Font } from "tspl-generator";
// Create a new printer instance
const label = new TSPLPrinter(MeasurementSystem.METRIC);
// Initialize with label size and settings
label.initialize({
width: 100,
height: 60,
speed: 4,
density: 8,
gap: 3,
});
// Add text
label.addText({
x: 10,
y: 10,
font: Font.FONT_1,
text: "Hello World!",
});
// Add a barcode
label.addBarcode(10, 30, "128", 30, "12345678", 1);
// Print 2 copies
label.print(2);
// Get the generated TSPL code
const tsplCode = label.getBuffer();
console.log(tsplCode);
You can use the library directly in a browser by including the UMD bundle:
<!-- Using CDN (recommended) -->
<script src="https://cdn.jsdelivr.net/npm/tspl-generator/dist/browser/tspl-generator.min.js"></script>
<!-- Or using a local file -->
<script src="path/to/tspl-generator.min.js"></script>
<script>
// Create a new printer instance
const printer = new TSPLGenerator.TSPLPrinter(
TSPLGenerator.MeasurementSystem.METRIC
);
// Initialize with label size and settings
printer.initialize({
width: 100,
height: 60,
speed: 4,
density: 8,
gap: 3,
});
// Add text
printer.addText({
x: 10,
y: 10,
font: TSPLGenerator.Font.FONT_1,
text: "Hello World!",
});
// Get the generated TSPL code
const tsplCode = printer.getBuffer();
console.log(tsplCode);
</script>
See the examples/browser-example.html
file for a complete browser example.
The library supports the following TSPL/TSPL2 commands:
SIZE
- Set label sizeGAP
- Set gap distanceSPEED
- Set print speedDENSITY
- Set print densityCLS
- Clear image buffer
TEXT
- Print textBLOCK
- Print text block with word wrap
BARCODE
- Print various barcode typesQRCODE
- Print QR codes
BOX
- Draw rectangleLINE
- Draw lineCIRCLE
- Draw circleELLIPSE
- Draw ellipse
BITMAP
- Print bitmap imagePUTBMP
- Print BMP filePUTPCX
- Print PCX fileDOWNLOAD
- Download graphic to printer
PRINT
- Print labels
The main class for generating TSPL commands.
constructor(measurementSystem: MeasurementSystem = MeasurementSystem.ENGLISH)
initialize(config: LabelConfig): this
- Initialize printer with basic settingssetGap(gap: number, offset: number = 0): this
- Set gap between labelsaddText(options: TextOptions): this
- Add text to the labeladdTextBlock(x: number, y: number, width: number, height: number, font: Font | string, text: string, ...): this
- Add a text block with word wrapaddBarcode(x: number, y: number, barcodeType: BarcodeType | string, height: number, content: string, ...): this
- Add a barcodeaddQRCode(x: number, y: number, content: string, ...): this
- Add a QR codeaddBox(x: number, y: number, xEnd: number, yEnd: number, thickness: number = 1): this
- Add a boxaddLine(x: number, y: number, xEnd: number, yEnd: number, thickness: number = 1): this
- Add a lineaddCircle(x: number, y: number, diameter: number, thickness: number = 1): this
- Add a circleaddEllipse(x: number, y: number, width: number, height: number, thickness: number = 1): this
- Add an ellipseaddBitmap(x: number, y: number, width: number, height: number, bitmap: string, mode: 0 | 1 = 0): this
- Add a bitmap imageaddBMP(x: number, y: number, filename: string): this
- Add a BMP imageprint(copies: number = 1): this
- Print labelsclear(): this
- Clear the image buffergetBuffer(): string
- Get the generated TSPL codereset(): this
- Reset the buffer
ISC
Contributions are welcome! Please feel free to submit a Pull Request.