u
This commit is contained in:
@@ -6,6 +6,11 @@ import * as db from '../../../db/article.js';
|
||||
import * as printerDb from '../../../db/printer.js';
|
||||
import * as agentprinterDb from '../../../db/agentprinter.js';
|
||||
import * as labeltemplateDb from '../../../db/labeltemplate.js';
|
||||
import * as printerjobDb from '../../../db/printerjob.js';
|
||||
import { emit } from '../../sse.js';
|
||||
import { notifyAgentJob } from '../../agent/index.js';
|
||||
import { pngToZpl } from '../../../services/zplService.js';
|
||||
import { applyZplCopies } from '../zpljob.js';
|
||||
import {
|
||||
AVIF_BODY_LIMIT,
|
||||
emitArticleSignal,
|
||||
@@ -395,13 +400,16 @@ export function register(app, basePath = '') {
|
||||
|
||||
try {
|
||||
let printer = await printerDb.getById(req.user.id, parsedPrinterId.id);
|
||||
let agentPrinter = null;
|
||||
if (!printer || printer.company_id !== companyId) {
|
||||
const agentPrinter = await agentprinterDb.getById(req.user.id, parsedPrinterId.id);
|
||||
agentPrinter = await agentprinterDb.getById(req.user.id, parsedPrinterId.id);
|
||||
if (agentPrinter) {
|
||||
printer = agentPrinter;
|
||||
} else {
|
||||
return res.status(404).json({ error: 'Printer not found' });
|
||||
}
|
||||
} else if (printer.printeragentId != null) {
|
||||
agentPrinter = printer;
|
||||
}
|
||||
if (printer.type && printer.type.toLowerCase() !== 'zpl') {
|
||||
return res.status(400).json({ error: 'Printer is not a ZPL printer' });
|
||||
@@ -425,42 +433,67 @@ export function register(app, basePath = '') {
|
||||
return res.status(404).json({ error: 'Article not found' });
|
||||
}
|
||||
|
||||
// Extract and interpolate canvas objects
|
||||
// Extract and interpolate canvas objects or raw ZPL
|
||||
const content = template.content;
|
||||
const objects = Array.isArray(content?.objects)
|
||||
? content.objects
|
||||
: Array.isArray(content)
|
||||
? content
|
||||
: [];
|
||||
let zplData;
|
||||
|
||||
const interpolatedObjects = objects.map(obj => {
|
||||
const cloned = { ...obj };
|
||||
if (typeof cloned.text === 'string') {
|
||||
cloned.text = interpolateString(cloned.text, article);
|
||||
if (typeof content === 'string' && content.includes('^XA')) {
|
||||
let interpolated = interpolateString(content, article);
|
||||
if (parsedQuantity.id > 1) {
|
||||
interpolated = applyZplCopies(interpolated, parsedQuantity.id);
|
||||
}
|
||||
if (typeof cloned.code === 'string') {
|
||||
cloned.code = interpolateString(cloned.code, article);
|
||||
zplData = interpolated;
|
||||
} else {
|
||||
const objects = Array.isArray(content?.objects)
|
||||
? content.objects
|
||||
: Array.isArray(content)
|
||||
? content
|
||||
: [];
|
||||
|
||||
const interpolatedObjects = objects.map(obj => {
|
||||
const cloned = { ...obj };
|
||||
if (typeof cloned.text === 'string') {
|
||||
cloned.text = interpolateString(cloned.text, article);
|
||||
}
|
||||
if (typeof cloned.code === 'string') {
|
||||
cloned.code = interpolateString(cloned.code, article);
|
||||
}
|
||||
return cloned;
|
||||
});
|
||||
|
||||
const printPayload = await renderLabel({
|
||||
width: template.width,
|
||||
height: template.height,
|
||||
objects: interpolatedObjects,
|
||||
});
|
||||
|
||||
zplData = await pngToZpl(printPayload, parsedQuantity.id);
|
||||
}
|
||||
|
||||
if (agentPrinter) {
|
||||
const job = await printerjobDb.create(req.user.id, {
|
||||
agentprinterId: agentPrinter.id,
|
||||
type: 'zpl',
|
||||
data: zplData,
|
||||
});
|
||||
|
||||
if (job.printeragentId) {
|
||||
notifyAgentJob(job.printeragentId, 'created', job);
|
||||
}
|
||||
return cloned;
|
||||
});
|
||||
|
||||
const printPayload = await renderLabel({
|
||||
width: template.width,
|
||||
height: template.height,
|
||||
objects: interpolatedObjects,
|
||||
});
|
||||
emit('printerjob', { companyId, data: { job } });
|
||||
return res.status(200).json({ success: true, printerjob: job });
|
||||
}
|
||||
|
||||
// Mock network dispatch
|
||||
const spoolKey = printer.address ? `${printer.address}_${printer.type}` : `agent_${printer.id}_${printer.type || 'zpl'}`;
|
||||
// Fallback for legacy network printers with static IP / address
|
||||
const spoolKey = printer.address ? `${printer.address}_${printer.type}` : `printer_${printer.id}_${printer.type || 'zpl'}`;
|
||||
const printDir = path.join(process.cwd(), 'print_spool', spoolKey);
|
||||
await fs.mkdir(printDir, { recursive: true });
|
||||
|
||||
for (let i = 0; i < parsedQuantity.id; i++) {
|
||||
const timestamp = Date.now();
|
||||
const filename = parsedQuantity.id > 1 ? `${timestamp}_${i + 1}.png` : `${timestamp}.png`;
|
||||
const filePath = path.join(printDir, filename);
|
||||
await fs.writeFile(filePath, printPayload);
|
||||
}
|
||||
const timestamp = Date.now();
|
||||
const filename = `${timestamp}.zpl`;
|
||||
const filePath = path.join(printDir, filename);
|
||||
await fs.writeFile(filePath, zplData, 'utf8');
|
||||
|
||||
res.status(200).json({ success: true });
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user