refactor: streamline printing API and update version check logic
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
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';
|
||||
|
||||
@@ -20,14 +18,15 @@ class PrintingService {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
private async printLabelAsync(pdf: string) {
|
||||
private async printLabelAsync(pdf: { labels: string[] }): Promise<void> {
|
||||
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);
|
||||
// const images = await convertPdfToBitmaps(pdf);
|
||||
// const codes = await this.generateZplCodes(images);
|
||||
const codes = pdf.labels;
|
||||
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
// Create and connect the socket
|
||||
@@ -60,46 +59,8 @@ class PrintingService {
|
||||
});
|
||||
}
|
||||
|
||||
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,
|
||||
},
|
||||
});
|
||||
|
||||
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> {
|
||||
private async print(printer: string, type: string, bytes: { labels: string[] }): Promise<boolean> {
|
||||
try {
|
||||
|
||||
await this.printLabelAsync(bytes);
|
||||
@@ -110,7 +71,8 @@ class PrintingService {
|
||||
}
|
||||
}
|
||||
|
||||
public async printPdf(printer: string, pdfBytes: string): Promise<boolean> {
|
||||
public async printPdf(printer: string, pdfBytes: { labels: string[] }): Promise<boolean> {
|
||||
|
||||
return this.print(printer, "pdf", pdfBytes);
|
||||
}
|
||||
|
||||
@@ -118,7 +80,6 @@ class PrintingService {
|
||||
public getPrinter(baseMarketplace: number): string {
|
||||
if (baseMarketplace == 2) return 'ozon';
|
||||
return "wildberries";
|
||||
// return "ozon";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user