update dependencies, improve PDF handling, and enhance printer integration
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import axios from "axios";
|
||||
import {blue100} from "react-native-paper/lib/typescript/styles/themes/v2/colors";
|
||||
import {Zpl} from "react-native-zpl-code";
|
||||
import {convertPdfToBitmaps} from "../connectors/PdfToBitmap";
|
||||
import TcpSocket from "react-native-tcp-socket";
|
||||
import * as SecureStore from 'expo-secure-store';
|
||||
|
||||
class PrintingService {
|
||||
private static instance: PrintingService | null = null;
|
||||
@@ -18,33 +20,100 @@ class PrintingService {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
private newAbortSignal(timeoutMs: number) {
|
||||
const abortController = new AbortController();
|
||||
setTimeout(() => abortController.abort(), timeoutMs || 0);
|
||||
private async printLabelAsync(pdf: string) {
|
||||
const printerIp = await SecureStore.getItemAsync("printerIp");
|
||||
if (!printerIp) {
|
||||
throw new Error("Printer IP not found in secure store");
|
||||
}
|
||||
// Fetch the PDF, convert to bitmaps, generate ZPL codes
|
||||
const images = await convertPdfToBitmaps(pdf);
|
||||
const codes = await this.generateZplCodes(images);
|
||||
|
||||
return abortController.signal;
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
// Create and connect the socket
|
||||
const client = TcpSocket.createConnection({host: printerIp, port: 9100}, () => {
|
||||
|
||||
// Write each ZPL chunk
|
||||
codes.forEach((code, idx) => {
|
||||
console.log(`Printing ${idx}`);
|
||||
client.write(code);
|
||||
});
|
||||
|
||||
client.end();
|
||||
});
|
||||
|
||||
// Reject on socket error
|
||||
client.on("error", (err) => {
|
||||
console.error("Socket error:", err);
|
||||
reject(err);
|
||||
});
|
||||
|
||||
// Resolve when the socket fully closes
|
||||
client.on("close", (hadError) => {
|
||||
if (hadError) {
|
||||
// close after an error will have already rejected
|
||||
return;
|
||||
}
|
||||
console.log("Connection closed");
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private async print(printer: string, type: string, bytes: Uint8Array): Promise<boolean> {
|
||||
try {
|
||||
let response = await axios.post(`http://${this.apiUrl}:${this.port}/print/${printer}/${type}`, bytes.buffer, {
|
||||
timeout: 25 * 1000,
|
||||
signal: this.newAbortSignal(25 * 1000)
|
||||
private async generateZplCodes(images: string[]) {
|
||||
const zplCodes = [];
|
||||
|
||||
for (const image of images) {
|
||||
const zplBuilder = new Zpl.Builder();
|
||||
zplBuilder.setup({
|
||||
size: {
|
||||
heightDots: 320, // 40mm at 203 DPI
|
||||
widthDots: 464, // 58mm at 203 DPI
|
||||
},
|
||||
labelHome: {
|
||||
x: 0,
|
||||
y: 0,
|
||||
},
|
||||
labelTop: 0,
|
||||
labeShift: 0,
|
||||
orientation: "NORMAL",
|
||||
media: {
|
||||
type: "MARK_SENSING",
|
||||
dots: 0,
|
||||
},
|
||||
});
|
||||
return response.data.ok;
|
||||
|
||||
zplBuilder.image({
|
||||
uri: `data:image/png;base64,${image}`,
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 464,
|
||||
height: 320,
|
||||
dither: true,
|
||||
});
|
||||
|
||||
const zplCode = await zplBuilder.build();
|
||||
zplCodes.push(zplCode);
|
||||
}
|
||||
|
||||
return zplCodes;
|
||||
}
|
||||
|
||||
private async print(printer: string, type: string, bytes: string): Promise<boolean> {
|
||||
try {
|
||||
|
||||
await this.printLabelAsync(bytes);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async printPdf(printer: string, pdfBytes: Uint8Array): Promise<boolean> {
|
||||
public async printPdf(printer: string, pdfBytes: string): Promise<boolean> {
|
||||
return this.print(printer, "pdf", pdfBytes);
|
||||
}
|
||||
|
||||
public async printImage(printer: string, imageBytes: Uint8Array): Promise<boolean> {
|
||||
return this.print(printer, "image", imageBytes);
|
||||
}
|
||||
|
||||
public getPrinter(baseMarketplace: number): string {
|
||||
if (baseMarketplace == 2) return 'ozon';
|
||||
|
||||
Reference in New Issue
Block a user