diff --git a/src/main/java/com/utopiaindustries/controller/CuttingController.java b/src/main/java/com/utopiaindustries/controller/CuttingController.java index d608c7e..cc07476 100644 --- a/src/main/java/com/utopiaindustries/controller/CuttingController.java +++ b/src/main/java/com/utopiaindustries/controller/CuttingController.java @@ -5,6 +5,7 @@ import com.utopiaindustries.dao.ctp.BundleWrapper; import com.utopiaindustries.model.ctp.JobCardWrapper; import com.utopiaindustries.service.*; import com.utopiaindustries.util.StringUtils; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; @@ -155,7 +156,13 @@ public class CuttingController { redirectAttributes.addFlashAttribute("error", "Please Select At least One CheckBox." ); return "redirect:/cutting/bundles"; } - return barcodeService.generateBarcodes( Arrays.asList( ids ), artifactType ); + try { + barcodeService.generateBarcodes( Arrays.asList( ids ), artifactType ); + return ResponseEntity.ok(); + }catch (Exception e){ + redirectAttributes.addFlashAttribute( "error", e ); + return "redirect:/cutting/bundles"; + } } @PostMapping( "/generate-master-barcodes" ) @@ -165,6 +172,12 @@ public class CuttingController { redirectAttributes.addFlashAttribute("error", "Please Select At least One CheckBox." ); return "redirect:/cutting/master-bundles"; } - return barcodeService.generateBarcodes( Arrays.asList( ids ), artifactType ); + try { + barcodeService.generateBarcodes( Arrays.asList( ids ), artifactType ); + return ResponseEntity.ok(); + }catch (Exception e){ + redirectAttributes.addFlashAttribute( "error", e ); + return "redirect:/cutting/master-bundles"; + } } } diff --git a/src/main/java/com/utopiaindustries/controller/StitchingController.java b/src/main/java/com/utopiaindustries/controller/StitchingController.java index b96c5cd..0de9873 100644 --- a/src/main/java/com/utopiaindustries/controller/StitchingController.java +++ b/src/main/java/com/utopiaindustries/controller/StitchingController.java @@ -5,6 +5,7 @@ import com.utopiaindustries.model.ctp.JobCard; import com.utopiaindustries.model.ctp.StitchingOfflineItem; import com.utopiaindustries.service.*; import com.utopiaindustries.util.StringUtils; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; @@ -130,11 +131,17 @@ public class StitchingController { @PostMapping( "/generate-barcodes" ) public Object generateBarcode(@RequestParam( name = "ids" ,required = false) Long[] ids, - @RequestParam( name = "artifactType" ) String artifactType, RedirectAttributes redirectAttributes ) throws Exception { + @RequestParam( name = "artifactType" ) String artifactType, RedirectAttributes redirectAttributes ) throws Exception { if (ids == null){ redirectAttributes.addFlashAttribute( "error", "Select At least One CheckBox" ); return "redirect:/stitching/stitching-offline-items"; } - return barcodeService.generateBarcodes( Arrays.asList( ids ), artifactType ); + try { + barcodeService.generateBarcodes( Arrays.asList( ids ), artifactType ); + return ResponseEntity.ok(); + }catch (Exception e){ + redirectAttributes.addFlashAttribute( "error", e ); + return "redirect:/stitching/stitching-offline-items"; + } } } diff --git a/src/main/java/com/utopiaindustries/service/BarcodeService.java b/src/main/java/com/utopiaindustries/service/BarcodeService.java index d6fda35..dbb203b 100644 --- a/src/main/java/com/utopiaindustries/service/BarcodeService.java +++ b/src/main/java/com/utopiaindustries/service/BarcodeService.java @@ -4,6 +4,8 @@ import com.google.zxing.BarcodeFormat; import com.itextpdf.html2pdf.ConverterProperties; import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider; import com.itextpdf.text.*; +import com.itextpdf.text.Image; +import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.*; import com.utopiaindustries.dao.ctp.BundleDAO; import com.utopiaindustries.dao.ctp.FinishedItemDAO; @@ -12,6 +14,11 @@ import com.utopiaindustries.dao.ctp.StitchingOfflineItemDAO; import com.utopiaindustries.model.ctp.*; import com.utopiaindustries.util.BarcodeUtils; import com.utopiaindustries.util.StringUtils; +import com.zebra.sdk.comm.Connection; +import com.zebra.sdk.comm.TcpConnection; +import com.zebra.sdk.graphics.internal.ZebraImage; +import com.zebra.sdk.printer.ZebraPrinter; +import com.zebra.sdk.printer.ZebraPrinterFactory; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.ResourceLoader; @@ -20,6 +27,10 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.Font; +import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -44,7 +55,7 @@ public class BarcodeService { /* * generate barcodes here * */ - public ResponseEntity generateBarcodes(List ids, String artifactType) throws Exception { + public void generateBarcodes(List ids, String artifactType) throws Exception { String size = BarcodeStickerSize.SIZE_1_75_X_3_5.name(); BarcodeStickerSize stickerSize = BarcodeStickerSize.getSize(size); @@ -57,89 +68,77 @@ public class BarcodeService { list = stitchingOfflineItemDAO.findByIds(ids); } - // get input stream - ByteArrayInputStream inputStream = null; - // get input stream - inputStream = getInputStream(list, stickerSize, artifactType); - - String headerContentDispositionStr = String.format("%s; filename=%s.pdf", "attachment", artifactType); - // return response - return ResponseEntity - .ok() - .header(HttpHeaders.CONTENT_DISPOSITION, headerContentDispositionStr) - .contentType(MediaType.APPLICATION_PDF) - .body(new InputStreamResource(inputStream)); + getBarcodeImages(list, stickerSize, artifactType); } - private ByteArrayInputStream getInputStream(List artifacts, - BarcodeStickerSize stickerSize, - String artifactType) throws Exception { - // resource loader - ResourceLoader resourceLoader = new DefaultResourceLoader(); - // output stream - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - // converter properties - ConverterProperties properties = new ConverterProperties(); - properties.setFontProvider(new DefaultFontProvider(false, false, true)); - // generate pdf - Document document = new Document(new Rectangle(stickerSize.getWidth(), stickerSize.getHeight()), stickerSize.getMarginLeft(), stickerSize.getMarginRight(), stickerSize.getMarginTop(), stickerSize.getMarginBottom()); - PdfWriter.getInstance(document, outputStream); - document.open(); + public void getBarcodeImages(List artifacts, + BarcodeStickerSize stickerSize, + String artifactType) throws Exception { + for (InventoryArtifact artifact : artifacts) { - document.newPage(); - // add sku - Paragraph skuPara = new Paragraph(artifact.getSku(), new Font(Font.FontFamily.HELVETICA, stickerSize.getTextSizeSmall(), Font.BOLD)); - skuPara.setAlignment(Element.ALIGN_CENTER); - document.add(skuPara); + // Create a blank BufferedImage (an image with the size of the sticker) + BufferedImage stickerImage = new BufferedImage((int) stickerSize.getWidth(), (int) stickerSize.getHeight(), BufferedImage.TYPE_INT_ARGB); + Graphics2D g2d = stickerImage.createGraphics(); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - // add barcode image + // Set background color (white for the sticker) + g2d.setColor(Color.WHITE); + g2d.fillRect(0, 0, (int) stickerSize.getWidth(), (int) stickerSize.getHeight()); + + // Set font for SKU and barcode + Font font = new Font("Helvetica", Font.BOLD, stickerSize.getTextSizeSmall()); + g2d.setFont(font); + + // Add SKU to the image + String sku = artifact.getSku(); + FontMetrics fontMetrics = g2d.getFontMetrics(); + int textWidth = fontMetrics.stringWidth(sku); + int x = (int) ((stickerSize.getWidth() - textWidth) / 2); + g2d.setColor(Color.BLACK); + g2d.drawString(sku, x, stickerSize.getMarginTop() + fontMetrics.getAscent()); + + // Create the barcode image byte[] imgBytes = BarcodeUtils.getBarcodeImageByteArray(artifact.getBarcode(), BarcodeFormat.CODE_128, stickerSize.getImageWidthBarcode(), stickerSize.getImageHeightBarcode()); - Image image = Image.getInstance(imgBytes); - image.setAlignment(Element.ALIGN_CENTER); - document.add(image); + BufferedImage barcodeImage = ImageIO.read(new ByteArrayInputStream(imgBytes)); - Paragraph barcodePara = new Paragraph(artifact.getBarcode(), new Font(Font.FontFamily.HELVETICA, stickerSize.getTextSizeSmall(), Font.BOLD)); - barcodePara.setAlignment(Element.ALIGN_CENTER); - document.add(barcodePara); - // add Artifact mark B / M / F - if ( artifactType.equalsIgnoreCase( Bundle.class.getSimpleName()) ) { - Paragraph artifactPara = new Paragraph(String.format("%s : %d", artifact.getType(), artifact.getId() ), new Font(Font.FontFamily.HELVETICA, stickerSize.getTextSizeExtraSmall(), Font.NORMAL)); - artifactPara.setAlignment(Element.ALIGN_CENTER); + // Draw the barcode image on the sticker + int barcodeX =(int) (stickerSize.getWidth() - barcodeImage.getWidth()) / 2; + int barcodeY = stickerSize.getMarginTop() + fontMetrics.getAscent() + 10; // Add some margin + g2d.drawImage(barcodeImage, barcodeX, barcodeY, null); - Paragraph type = new Paragraph(String.valueOf(artifactType.toCharArray()[0]), new Font(Font.FontFamily.HELVETICA, stickerSize.getTextSize(), Font.BOLD)); - type.setAlignment(Element.ALIGN_CENTER); - - // Create a table with 2 columns - PdfPTable table = new PdfPTable(2); - table.setWidthPercentage(100); - table.setWidths(new int[]{55, 45}); // Adjust column widths as needed - - // Add cells to the table - PdfPCell artifactCell = new PdfPCell(artifactPara); - artifactCell.setBorder(Rectangle.NO_BORDER); - artifactCell.setHorizontalAlignment(Element.ALIGN_RIGHT); - artifactCell.setPaddingTop(5); - - PdfPCell typeCell = new PdfPCell(type); - typeCell.setBorder(Rectangle.NO_BORDER); - typeCell.setHorizontalAlignment(Element.ALIGN_LEFT); - - table.addCell(artifactCell); - table.addCell(typeCell); - // Add the table to the document - document.add(table); + // Add the barcode value below the barcode image + g2d.drawString(artifact.getBarcode(), (stickerSize.getWidth() - fontMetrics.stringWidth(artifact.getBarcode())) / 2, + barcodeY + barcodeImage.getHeight() + fontMetrics.getAscent()); + // If artifactType is Bundle, add additional info + if (artifactType.equalsIgnoreCase(Bundle.class.getSimpleName())) { + String typeText = String.format("%s : %d", artifact.getType(), artifact.getId()); + g2d.drawString(typeText, (stickerSize.getWidth() - fontMetrics.stringWidth(typeText)) / 2, + barcodeY + barcodeImage.getHeight() + fontMetrics.getAscent() + 20); } else { - Paragraph type = new Paragraph(String.valueOf(artifactType.toCharArray()[0]), new Font(Font.FontFamily.HELVETICA, stickerSize.getTextSize(), Font.BOLD)); - type.setAlignment(Element.ALIGN_CENTER); - document.add(type); + // Add first character of artifact type + String type = String.valueOf(artifactType.charAt(0)); + g2d.setFont(new Font("Helvetica", Font.BOLD, stickerSize.getTextSize())); + g2d.drawString(type, (stickerSize.getWidth() - fontMetrics.stringWidth(type)) / 2, + barcodeY + barcodeImage.getHeight() + fontMetrics.getAscent() + 40); } + + // Finalize drawing + g2d.dispose(); + + printLabel(stickerImage); } - document.close(); - // input stream - return new ByteArrayInputStream(outputStream.toByteArray()); } - + public void printLabel( BufferedImage bufferedImage ) throws Exception { + String ipAddr = "192.168.90.18"; + int port = 9100; + Connection connection = new TcpConnection( ipAddr, port ); + connection.open(); + ZebraPrinter printer = ZebraPrinterFactory.getInstance( connection ); + ZebraImage zebraImage = new ZebraImage( bufferedImage ); + printer.printImage( zebraImage, 0, 0, 0, 0, false ); + connection.close(); + } } \ No newline at end of file