import { detectPdfPage, withMediaOption } from '../../utils/pdfPageMedia' import { stagePrintFile, printFile, applyPrintResult } from '../../utils/printDispatch' // Map shipping printer to Tisch number for A4 printer naming const SHIPPING_PRINTER_TO_TISCH: Record = { 'labelprinter-1': '1', // Tisch 1 'labelprinter-4': '2', // Tisch 2 'labelprinter-5': '3' // Tisch 3 } export default defineEventHandler(async (event) => { let data: any = {} const body = await readBody(event) const shippingPrinter = body.shippingPrinter || 'labelprinter-1' // Prefer the A4 printer configured on the commissioning table // (CUST_CommissionTable.a4_printer, passed by the client). Fall back to the // legacy printer→Tisch map for callers that don't send it. const tischNumber = SHIPPING_PRINTER_TO_TISCH[shippingPrinter] || '1' const a4Printer = body.a4Printer || `commission-a4-${tischNumber}` try { const fullFilePath = await stagePrintFile(body.pathName, body.fileName, Buffer.from(body.fileContent, 'base64')) // Page size decides the queue: labels → shipping label printer (fit-to-page); // A5 / A4 paper → the commissioning table's A4 printer with an explicit media // size, so an A5 Lieferschein pulls A5 from the tray instead of landing on the // Zebra. See server/utils/pdfPageMedia.ts. const page = await detectPdfPage(fullFilePath) const pdfWidth = page.width const pdfHeight = page.height const isSmallLabel = page.kind === 'label' console.log(`[Smart Print] PDF dimensions: ${pdfWidth} x ${pdfHeight} pts (${page.kind}${page.detected ? '' : ', assumed'})`) const printerToUse = isSmallLabel ? shippingPrinter : a4Printer const printOptions = isSmallLabel ? '-o fit-to-page' : withMediaOption('-o sides=one-sided', page) console.log(`[Smart Print] Printing to: ${printerToUse} with options: ${printOptions}`) const result = await printFile(event, { queue: printerToUse, options: printOptions, filePath: fullFilePath, source: 'print/attachment-smart' }) data = applyPrintResult({ ...body, pdfAnalysis: { width: pdfWidth, height: pdfHeight, isSmallLabel: isSmallLabel, kind: page.kind, media: page.mediaOption || null, printerUsed: printerToUse } }, result) if (!result.ok) data.error = result.error } catch (err: any) { console.error('[Smart Print] Error:', err) data = { message: 'failed', error: err.message || err } } return data })