From da93b548e3d5cca7b8c2bdd6d321c66d1a5870ae Mon Sep 17 00:00:00 2001 From: linfso Date: Tue, 8 Jul 2025 09:56:17 +0800 Subject: [PATCH] md --- .../erp/web/emis/EmisWaybillController.java | 1 + .../tanex/tmp/TanexSubBillPrintImpl.java | 268 ++++++++++++ .../print/tanex/tmp/TanexTplPrintImpl.java | 254 ++++++++++++ .../service/print/tanex/tmp/ZPLConveter.java | 200 +++++++++ .../print/tanex/tmp/ZPLConveterUtil.java | 381 ++++++++++++++++++ 5 files changed, 1104 insertions(+) create mode 100644 emis-biz/src/main/java/com/xdadan/erp/emis/service/print/tanex/tmp/TanexSubBillPrintImpl.java create mode 100644 emis-biz/src/main/java/com/xdadan/erp/emis/service/print/tanex/tmp/TanexTplPrintImpl.java create mode 100644 emis-biz/src/main/java/com/xdadan/erp/emis/service/print/tanex/tmp/ZPLConveter.java create mode 100644 emis-biz/src/main/java/com/xdadan/erp/emis/service/print/tanex/tmp/ZPLConveterUtil.java diff --git a/emis-biz-web/src/main/java/com/xdadan/erp/web/emis/EmisWaybillController.java b/emis-biz-web/src/main/java/com/xdadan/erp/web/emis/EmisWaybillController.java index 8c03f6415..ef3ac3813 100644 --- a/emis-biz-web/src/main/java/com/xdadan/erp/web/emis/EmisWaybillController.java +++ b/emis-biz-web/src/main/java/com/xdadan/erp/web/emis/EmisWaybillController.java @@ -36,6 +36,7 @@ import com.xdadan.erp.emis.service.*; import com.xdadan.erp.emis.service.print.IXddTplPrint; import com.xdadan.erp.emis.service.print.cpcl.CvtTplToCpcl; import com.xdadan.erp.emis.service.print.tanex.TanexPdfPrinter; +import com.xdadan.erp.emis.service.print.tanex.tmp.TanexTplPrintImpl; import com.xdadan.erp.emis.utils.PrintUtils; import com.xdadan.erp.emis.utils.WaybillHelper; import com.xdadan.erp.system.domain.SysFileInfo; diff --git a/emis-biz/src/main/java/com/xdadan/erp/emis/service/print/tanex/tmp/TanexSubBillPrintImpl.java b/emis-biz/src/main/java/com/xdadan/erp/emis/service/print/tanex/tmp/TanexSubBillPrintImpl.java new file mode 100644 index 000000000..f1086ef33 --- /dev/null +++ b/emis-biz/src/main/java/com/xdadan/erp/emis/service/print/tanex/tmp/TanexSubBillPrintImpl.java @@ -0,0 +1,268 @@ +package com.xdadan.erp.emis.service.print.tanex.tmp; + +import com.alibaba.fastjson.JSON; +import com.itextpdf.text.Element; +import com.xdadan.erp.common.core.domain.AjaxResult; +import com.xdadan.erp.common.utils.DateUtils; +import com.xdadan.erp.emis.service.print.IXddTplPrint; +import com.xdadan.erp.emis.service.print.ItemCell; +import com.xdadan.erp.emis.service.print.XddBasePrint; +import com.xdadan.erp.emis.utils.sensitive.Convert; +import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.rendering.ImageType; +import org.apache.pdfbox.rendering.PDFRenderer; +import org.springframework.core.io.ClassPathResource; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.*; +import java.util.*; + +public class TanexSubBillPrintImpl extends XddBasePrint { + + /** + * 模版文件 + */ +// private static String tplName="/Users/alan/work/git/tanex56/emis-service/emis-biz/src/main/resources/tpl/tanex_zd_tpl.pdf"; + private static String tplName="tpl/tanex_zd_tpl.pdf"; + + /** + * 单元条目配置 + * @param orderMap + * @return + */ + @Override + protected ArrayList getItemCelconfig(Map orderMap,String tplCode) { + ArrayList printItems = new ArrayList<>(); + + String package3dm = ""; + String packageWJC = ""; + + String pdfInfo =MapUtils.getString(orderMap,"pdfInfo",""); + if(!StringUtils.isEmpty(pdfInfo)) { + String[] pdfInfoArr = pdfInfo.split("]"); + if (pdfInfoArr.length > 0) { + package3dm = pdfInfoArr[0].replace("[", ""); + packageWJC = pdfInfoArr[1].replace("[", ""); + } + } + + // 设置打印参数信息 + String orderSn="订单号:"+MapUtils.getString(orderMap,"order_sn",""); + printItems.add(new ItemCell("package3dm",package3dm,12,true,Element.ALIGN_CENTER,"Text")); + printItems.add(new ItemCell("packageWJC",packageWJC,16,true,Element.ALIGN_CENTER,"Text")); + printItems.add(new ItemCell("orderSn", orderSn,6,true,"Text")); + + String logisticNo=""+MapUtils.getString(orderMap,"ydSn",""); + printItems.add(new ItemCell("logisticNo", logisticNo,7,true,Element.ALIGN_CENTER,"Text")); + + // 条码显示 + printItems.add(new ItemCell("barcodeImg1", MapUtils.getString(orderMap,"ydSn",""),1,true,"BarCode")); +// printItems.add(new ItemCell("barcodeImg2", MapUtils.getString(orderMap,"yd_sn",""),22,true,"BarCode")); + + String mobile=MapUtils.getString(orderMap,"mobile",""); +// mobile= Convert.mobile(mobile); +// String address= Convert.address(address); + String consignee=MapUtils.getString(orderMap,"consignee",""); +// consignee= Convert.chineseNameFirst(consignee); + + printItems.add(new ItemCell("company", MapUtils.getString(orderMap,"company",""),7,true,"Text")); + printItems.add(new ItemCell("consignee", consignee,7,true,"Text")); + printItems.add(new ItemCell("mobile", mobile,7,true,"Text")); + printItems.add(new ItemCell("address", MapUtils.getString(orderMap,"address",""),7,true,"Text")); + + String goodsInfo=""+MapUtils.getString(orderMap,"goodsInfo",""); + printItems.add(new ItemCell("goodsInfo", goodsInfo,7,true,"Text")); + + String parcelQty = MapUtils.getString(orderMap,"subSeqNum","")+ + "/"+MapUtils.getString(orderMap,"parcelQty","") + +"|"+MapUtils.getString(orderMap,"billWeight","")+"kg" + +"|"+MapUtils.getString(orderMap,"totalVolume","")+"m3"; + printItems.add(new ItemCell("parcelQty", parcelQty,7,true,Element.ALIGN_CENTER,"Text")); + printItems.add(new ItemCell("sendDate", MapUtils.getString(orderMap,"sendDate",""),7,true,"Text")); + printItems.add(new ItemCell("packType", MapUtils.getString(orderMap,"packType",""),7,true,"Text")); + printItems.add(new ItemCell("paymentType", MapUtils.getString(orderMap,"paymentType",""),7,true,"Text")); + printItems.add(new ItemCell("dispatchType", MapUtils.getString(orderMap,"dispatchType",""),7,true,"Text")); + printItems.add(new ItemCell("shortLogisticNo", MapUtils.getString(orderMap,"shortLogisticNo",""),20,true,Element.ALIGN_CENTER,"Text")); + + String printInfo=""+MapUtils.getString(orderMap,"printDate","") + + "/" +MapUtils.getString(orderMap,"printMan","") + + "/" +MapUtils.getString(orderMap,"printCount",""); + printItems.add(new ItemCell("printInfo", printInfo,7,true,"Text")); + + String remark="备注:"+MapUtils.getString(orderMap,"remark",""); + printItems.add(new ItemCell("remark", remark,7,true,"Text")); + + String extData =MapUtils.getString(orderMap,"extData",""); + if(!StringUtils.isEmpty(extData)){ + // 解析gson数据 + try + { + Map mapExtData = JSON.parseObject(extData,Map.class); +// printItems.add(new ItemCell("k1", MapUtils.getString(mapExtData,"k1",""),8,true,"Text")); +// printItems.add(new ItemCell("k2", MapUtils.getString(mapExtData,"k2",""),8,true,"Text")); + printItems.add(new ItemCell("k3", MapUtils.getString(mapExtData,"k3",""),15,true,Element.ALIGN_CENTER,"Text")); +// printItems.add(new ItemCell("k4", MapUtils.getString(mapExtData,"k4",""),8,true,"Text")); +// printItems.add(new ItemCell("k5", MapUtils.getString(mapExtData,"k5",""),8,true,"Text")); +// printItems.add(new ItemCell("k6", MapUtils.getString(mapExtData,"k6",""),8,true,"Text")); + } + catch (Exception ex) + { + } + } + return printItems; + } + + /** + * 主要控制模版的字体显示 + * @param orderMap + * @param os + * @return + * @throws Exception + */ + @Override + public AjaxResult genPDF(Map orderMap,String tplCode,String tplStrBase64, OutputStream os) throws Exception { + AjaxResult ar = new AjaxResult(); + + + ClassPathResource classPathResource = new ClassPathResource(tplName); + InputStream tplInStream = classPathResource.getInputStream(); +// InputStream tplInStream = new FileInputStream(new File(tplName)); +// + // 生成pdf文件 + genPdfByTpl(getItemCelconfig(orderMap,tplCode),tplInStream,os); + + return ar; + } + + + @Override + public AjaxResult genPNG(Map order,String tplCode,String tplStrBase64, OutputStream os) throws Exception { + AjaxResult ar = new AjaxResult(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + genPDF(order,tplCode,tplStrBase64, bos); + PDDocument document = PDDocument.load(bos.toByteArray()); + PDFRenderer renderer = new PDFRenderer(document); + BufferedImage bufferedImage = renderer.renderImageWithDPI(0, 200, ImageType.RGB); + ImageIO.write(bufferedImage, "png", os); + return ar; + } + + @Override + public String genPNGBase64(Map order,String tplCode,String tplStrBase64) throws Exception { + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + // 生成pdf + genPDF(order,tplCode,tplStrBase64,bos); + PDDocument document = PDDocument.load(bos.toByteArray()); +// bos.close(); + PDFRenderer renderer = new PDFRenderer(document); + BufferedImage bufferedImage = renderer.renderImageWithDPI(0, 200, ImageType.RGB); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ImageIO.write(bufferedImage, "jpg", out); + + Base64.Encoder encoder = Base64.getEncoder(); + + String base64Str = "data:image/jpg;base64,"+encoder.encodeToString(out.toByteArray()); + System.out.println(base64Str); + + return base64Str; + } + + @Override + public String genPDFBase64(Map order,String tplCode,String tplStrBase64) throws Exception { + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + // 生成pdfbase64 + genPDF(order,tplCode,tplStrBase64,bos); + byte[] pdfBytes = bos.toByteArray(); + String pdfBase64Str = Base64.getEncoder().encodeToString(pdfBytes); + return pdfBase64Str; + } + + + public static void testPrint(int i) throws Exception { + +// String newPDFPath = "/Users/alan/Downloads/ynmb_blank_t21.pdf"; + String newPDFPath = "/Users/alan/Downloads/ynmb_blank_t_x"+i+".png"; + FileOutputStream out = new FileOutputStream(newPDFPath);//输出流 + + + Map order=new HashMap<>(); + order.put("orderSn", "DT20210901001"); + order.put("ydSn", "SF1374689807723"); + order.put("kdgs", "TANEX"); + order.put("pdfInfo", "[山东淄博-胡志明][越南DN]"); +// order.put("packageWJC", "."); +// order.put("address", Convert.address("上海市黄浦区河南南路1009弄3号902室上海市黄浦区河南南路1009弄3号902室上海市黄浦区河南南路1009弄3号902室")); + order.put("address", "上海市黄浦区河南南路1009弄3号902室上海市黄浦区河南南路1009弄3号902室上海市黄浦区河南南路1009弄3号902室"); + order.put("company", "SDSDS"); + order.put("mobile", Convert.mobile("13818228246")); + order.put("consignee", Convert.chineseNameFirst("张三")); + +// order.put("consigneeAndMobile", Convert.chineseNameFirst("张三"+" "+"13818228246")); + order.put("send_address", "Vancouver"); + order.put("send_company", "2324"); + order.put("send_mobile", "1604-683-1088"); + order.put("send_person", "Vancouver"); + order.put("extData", "{\"k1\":\"阿尔巴尼亚\",\"k2\":\"阿尔巴尼亚快递\",\"k3\":\"阿尔巴尼亚国际特快\",\"k4\":\"1-10工作日\",\"k5\":\"阿尔AL\",\"k6\":\"山东淄博-阿尔巴尼亚地拉那\"}"); +// order.put("sendPersonAndMobile", "Vancouver"+" "+"1604-683-1088"); + order.put("goodsInfo", "日常用品"); + order.put("parcelQty", "1"); + order.put("packType", "纸箱"); + order.put("paymentType", "现金"); + order.put("dispatchType", "派送"); + order.put("shortLogisticNo", "123213"); + order.put("remark", "test"); + order.put("billWeight", "12"); + order.put("totalVolume", "12"); + order.put("subSeqNum", "1"); + order.put("sendDate", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,new Date())); + order.put("printDay", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,new Date())); + order.put("printDate", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,new Date())); + order.put("printMan", "12321"); + order.put("printCount", 1); + + ByteArrayOutputStream os = new ByteArrayOutputStream(); + + IXddTplPrint xtp = new TanexSubBillPrintImpl(); +// xtp.genPDF(order,os); +// xtp.genPNG(order,out); + + // 生成pdf base64 +// byte[] pdfBytes = os.toByteArray(); +// String pdfBase64Str = Base64.getEncoder().encodeToString(pdfBytes); + String tplStrBase64=""; + String tplCode=""; + String pdfBase64Str =xtp.genPDFBase64(order,tplCode,tplStrBase64); + System.out.println(pdfBase64Str); + +// xtp.genPNGBase64(order); + } + + + public static void main(String[] args) { + try { + + for(int i=0;i<1;i++){ + testPrint(i); + } + +// xtp.testPdfPrint("/Users/alan/work/git/doitinfo/ai/logistic/src/main/resources/print_tpl/ynmb_blank.pdf","/Users/alan/Downloads/ynmb_blank_t21.pdf"); +// testPdfPrint("/Users/alan/work/git/doitinfo/ai/logistic/src/main/resources/print_tpl/ynmb_blank.pdf","/Users/alan/Downloads/ynmb_blank_t21.pdf"); +// testPdfPrint2Png("/Users/alan/work/git/doitinfo/ai/logistic/src/main/resources/print_tpl/lt_blank_bd.pdf","/Users/alan/Downloads/ynmb_blank_t10.png"); +// testPdfPrint2Png("/Users/alan/Downloads/ynmb_blank_t21.pdf","/Users/alan/Downloads/ynmb_blank_x1.png"); + + + } catch (Exception e) { + e.printStackTrace(); + } + } + + +} diff --git a/emis-biz/src/main/java/com/xdadan/erp/emis/service/print/tanex/tmp/TanexTplPrintImpl.java b/emis-biz/src/main/java/com/xdadan/erp/emis/service/print/tanex/tmp/TanexTplPrintImpl.java new file mode 100644 index 000000000..87685e49b --- /dev/null +++ b/emis-biz/src/main/java/com/xdadan/erp/emis/service/print/tanex/tmp/TanexTplPrintImpl.java @@ -0,0 +1,254 @@ +package com.xdadan.erp.emis.service.print.tanex.tmp; + +import com.alibaba.fastjson.JSON; +import com.itextpdf.text.Element; +import com.xdadan.erp.common.core.domain.AjaxResult; +import com.xdadan.erp.emis.service.print.IXddTplPrint; +import com.xdadan.erp.emis.service.print.ItemCell; +import com.xdadan.erp.emis.service.print.XddBasePrint; +import com.xdadan.erp.emis.utils.sensitive.Convert; +import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.rendering.ImageType; +import org.apache.pdfbox.rendering.PDFRenderer; +import org.springframework.core.io.ClassPathResource; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.*; +import java.util.ArrayList; +import java.util.Base64; +import java.util.HashMap; +import java.util.Map; + +public class TanexTplPrintImpl extends XddBasePrint { + + /** + * 模版文件 + */ +// private static String tplName="tpl/tanex_sd_2.pdf"; + private static String tplPath="/Users/alan/work/git/tanex56/emis-service/emis-biz/src/main/resources/tpl/"; + + private static String tplName="/Users/alan/work/git/tanex56/emis-service/emis-biz/src/main/resources/tpl/tanex_zd_tpl.pdf"; +// private static String tplName="/Users/alan/work/git/doitinfo/ai/logistic/src/main/resources/print_tpl/tanex/tanex_sd_2.pdf"; + + /** + * 单元条目配置 + * @param orderMap + * @return + */ + @Override + protected ArrayList getItemCelconfig(Map orderMap,String tplCode) { + ArrayList printItems = new ArrayList<>(); + + String package3dm = ""; + String packageWJC = ""; + + String pdfInfo =MapUtils.getString(orderMap,"pdf_info",""); + if(!StringUtils.isEmpty(pdfInfo)) { + String[] pdfInfoArr = pdfInfo.split("]"); + if (pdfInfoArr.length > 0) { + package3dm = pdfInfoArr[0].replace("[", ""); + packageWJC = pdfInfoArr[1].replace("[", ""); + } + } + + // 设置打印参数信息 + String orderSn="订单号:"+MapUtils.getString(orderMap,"order_sn",""); + printItems.add(new ItemCell("package3dm",package3dm,10,true,Element.ALIGN_CENTER,"Text")); + printItems.add(new ItemCell("packageWJC",packageWJC,16,true,Element.ALIGN_CENTER,"Text")); + printItems.add(new ItemCell("orderSn", orderSn,6,true,"Text")); + + String logisticNo="运单编号:"+MapUtils.getString(orderMap,"yd_sn",""); + printItems.add(new ItemCell("logisticNo", logisticNo,7,true,Element.ALIGN_CENTER,"Text")); + + // 条码显示 + printItems.add(new ItemCell("barcodeImg1", MapUtils.getString(orderMap,"yd_sn",""),1,true,"BarCode")); +// printItems.add(new ItemCell("barcodeImg2", MapUtils.getString(orderMap,"yd_sn",""),22,true,"BarCode")); + + String mobile=MapUtils.getString(orderMap,"mobile",""); + mobile= Convert.mobile(mobile); +// address= Convert.address(address); + String consignee=MapUtils.getString(orderMap,"consignee",""); + consignee= Convert.chineseNameFirst(consignee); + + printItems.add(new ItemCell("company", MapUtils.getString(orderMap,"company",""),8,true,"Text")); + printItems.add(new ItemCell("consignee", consignee,9,true,"Text")); + printItems.add(new ItemCell("mobile", mobile,9,true,"Text")); + printItems.add(new ItemCell("address", MapUtils.getString(orderMap,"address",""),8,false,"Text")); + + + String sendPersonAndMobile= Convert.mobile(MapUtils.getString(orderMap,"send_person",""))+" " + Convert.mobile(MapUtils.getString(orderMap,"send_mobile","")); + printItems.add(new ItemCell("sendCompany", MapUtils.getString(orderMap,"send_company",""),8,true,"Text")); +// printItems.add(new ItemCell("sendMobile", MapUtils.getString(orderMap,"send_mobile",""),22,true,"Text")); +// printItems.add(new ItemCell("sendPerson", MapUtils.getString(orderMap,"send_person",""),22,true,"Text")); + printItems.add(new ItemCell("sendPersonAndMobile", MapUtils.getString(orderMap,"send_person",""),8,true,"Text")); +// printItems.add(new ItemCell("remark", MapUtils.getString(orderMap,"remark",""),22,true,"Text")); + printItems.add(new ItemCell("sendAddress", MapUtils.getString(orderMap,"send_address",""),8,false,"Text")); + + String goodsInfo="货物信息:"+MapUtils.getString(orderMap,"goods_info",""); + printItems.add(new ItemCell("goodsInfo", goodsInfo,6,false,"Text")); + + String parcelQty = "件数:"+MapUtils.getString(orderMap,"parcel_qty","")+"件"; + printItems.add(new ItemCell("parcelQty", parcelQty,6,true,"Text")); + printItems.add(new ItemCell("createDay", MapUtils.getString(orderMap,"create_day",""),6,true,"Text")); + + String extData =MapUtils.getString(orderMap,"ext_data",""); + if(!StringUtils.isEmpty(extData)){ + // 解析gson数据 + try + { + Map mapExtData = JSON.parseObject(extData,Map.class); +// printItems.add(new ItemCell("k1", MapUtils.getString(mapExtData,"k1",""),8,true,"Text")); +// printItems.add(new ItemCell("k2", MapUtils.getString(mapExtData,"k2",""),8,true,"Text")); + printItems.add(new ItemCell("k3", MapUtils.getString(mapExtData,"k3",""),15,true,Element.ALIGN_CENTER,"Text")); +// printItems.add(new ItemCell("k4", MapUtils.getString(mapExtData,"k4",""),8,true,"Text")); +// printItems.add(new ItemCell("k5", MapUtils.getString(mapExtData,"k5",""),8,true,"Text")); +// printItems.add(new ItemCell("k6", MapUtils.getString(mapExtData,"k6",""),8,true,"Text")); + } + catch (Exception ex) + { + } + } + return printItems; + } + + /** + * 主要控制模版的字体显示 + * @param orderMap + * @param os + * @return + * @throws Exception + */ + @Override + public AjaxResult genPDF(Map orderMap,String tplCode,String tplStrBase64, OutputStream os) throws Exception { + AjaxResult ar = new AjaxResult(); + + ClassPathResource classPathResource = new ClassPathResource(tplName); +// InputStream tplInStream = classPathResource.getInputStream(); + InputStream tplInStream = new FileInputStream(new File(tplName)); +// + // 生成pdf文件 + genPdfByTpl(getItemCelconfig(orderMap,tplCode),tplInStream,os); + + return ar; + } + + + @Override + public AjaxResult genPNG(Map order,String tplCode,String tplStrBase64, OutputStream os) throws Exception { + AjaxResult ar = new AjaxResult(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + genPDF(order,tplCode,tplStrBase64, bos); + PDDocument document = PDDocument.load(bos.toByteArray()); + PDFRenderer renderer = new PDFRenderer(document); + BufferedImage bufferedImage = renderer.renderImageWithDPI(0, 280, ImageType.RGB); + ImageIO.write(bufferedImage, "png", os); + return ar; + } + + @Override + public String genPDFBase64(Map order,String tplCode,String tplStrBase64) throws Exception { + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + // 生成pdfbase64 + genPDF(order,tplCode,tplStrBase64,bos); + byte[] pdfBytes = bos.toByteArray(); + String pdfBase64Str = Base64.getEncoder().encodeToString(pdfBytes); + return pdfBase64Str; + } + + @Override + public String genPNGBase64(Map order,String tplCode,String tplStrBase64) throws Exception { + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + // 生成pdf + genPDF(order,tplCode,tplStrBase64,bos); + PDDocument document = PDDocument.load(bos.toByteArray()); +// bos.close(); + PDFRenderer renderer = new PDFRenderer(document); + BufferedImage bufferedImage = renderer.renderImageWithDPI(0, 280, ImageType.RGB); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ImageIO.write(bufferedImage, "jpg", out); + + Base64.Encoder encoder = Base64.getEncoder(); + + String base64Str = "data:image/jpg;base64,"+encoder.encodeToString(out.toByteArray()); + System.out.println(base64Str); + + return base64Str; + } + + + public static void testPrint(int i) throws Exception { + +// String newPDFPath = "/Users/alan/Downloads/ynmb_blank_t21.pdf"; + String newPDFPath = "/Users/alan/Downloads/ynmb_blank_t_x"+i+".png"; + FileOutputStream out = new FileOutputStream(newPDFPath);//输出流 + + + Map order=new HashMap<>(); + order.put("order_sn", "DT20210901001"); + order.put("yd_sn", "SF1374689807723"); + order.put("kdgs", "TANEX"); + order.put("pdf_info", "[山东淄博-胡志明][越南DN]"); +// order.put("packageWJC", "."); +// order.put("address", Convert.address("上海市黄浦区河南南路1009弄3号902室上海市黄浦区河南南路1009弄3号902室上海市黄浦区河南南路1009弄3号902室")); + order.put("address", "上海市黄浦区河南南路1009弄3号902室上海市黄浦区河南南路1009弄3号902室上海市黄浦区河南南路1009弄3号902室"); + order.put("company", "SDSDS"); + order.put("mobile", Convert.mobile("13818228246")); + order.put("consignee", Convert.chineseNameFirst("张三")); + +// order.put("consigneeAndMobile", Convert.chineseNameFirst("张三"+" "+"13818228246")); + order.put("send_address", "Vancouver"); + order.put("send_company", "2324"); + order.put("send_mobile", "1604-683-1088"); + order.put("send_person", "Vancouver"); + order.put("ext_data", "{\"k1\":\"阿尔巴尼亚\",\"k2\":\"阿尔巴尼亚快递\",\"k3\":\"阿尔巴尼亚国际特快\",\"k4\":\"1-10工作日\",\"k5\":\"阿尔AL\",\"k6\":\"山东淄博-阿尔巴尼亚地拉那\"}"); +// order.put("sendPersonAndMobile", "Vancouver"+" "+"1604-683-1088"); + order.put("goods_info", "日常用品"); + order.put("parcel_qty", "1"); + order.put("create_day", "2023-12-30"); + + ByteArrayOutputStream os = new ByteArrayOutputStream(); + + String tplStrBase64=""; + String tplCode=""; + IXddTplPrint xtp = new TanexTplPrintImpl(); + xtp.genPDF(order,tplCode,tplStrBase64,os); + xtp.genPNG(order,tplCode,tplStrBase64,out); + + // 生成pdf base64 + byte[] pdfBytes = os.toByteArray(); + String pdfBase64Str = Base64.getEncoder().encodeToString(pdfBytes); + System.out.println(pdfBase64Str); + +// xtp.genPNGBase64(order); + } + + + public static void main(String[] args) { + try { + +// for(int i=0;i<1;i++){ +// testPrint(i); +// } + + +// xtp.testPdfPrint("/Users/alan/work/git/doitinfo/ai/logistic/src/main/resources/print_tpl/ynmb_blank.pdf","/Users/alan/Downloads/ynmb_blank_t21.pdf"); +// testPdfPrint("/Users/alan/work/git/doitinfo/ai/logistic/src/main/resources/print_tpl/ynmb_blank.pdf","/Users/alan/Downloads/ynmb_blank_t21.pdf"); +// testPdfPrint2Png("/Users/alan/work/git/doitinfo/ai/logistic/src/main/resources/print_tpl/lt_blank_bd.pdf","/Users/alan/Downloads/ynmb_blank_t10.png"); +// testPdfPrint2Png("ynmb_blank_t21.pdf","/Users/alan/Downloads/ynmb_blank_x1.png"); + + + } catch (Exception e) { + e.printStackTrace(); + } + } + + +} diff --git a/emis-biz/src/main/java/com/xdadan/erp/emis/service/print/tanex/tmp/ZPLConveter.java b/emis-biz/src/main/java/com/xdadan/erp/emis/service/print/tanex/tmp/ZPLConveter.java new file mode 100644 index 000000000..93054a9b1 --- /dev/null +++ b/emis-biz/src/main/java/com/xdadan/erp/emis/service/print/tanex/tmp/ZPLConveter.java @@ -0,0 +1,200 @@ +package com.xdadan.erp.emis.service.print.tanex.tmp; + +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import javax.imageio.ImageIO; +public class ZPLConveter { + private int blackLimit = 380; + private int total; + private int widthBytes; + private boolean compressHex = false; + private static Map mapCode = new HashMap(); + { + mapCode.put(1, "G"); + mapCode.put(2, "H"); + mapCode.put(3, "I"); + mapCode.put(4, "J"); + mapCode.put(5, "K"); + mapCode.put(6, "L"); + mapCode.put(7, "M"); + mapCode.put(8, "N"); + mapCode.put(9, "O"); + mapCode.put(10, "P"); + mapCode.put(11, "Q"); + mapCode.put(12, "R"); + mapCode.put(13, "S"); + mapCode.put(14, "T"); + mapCode.put(15, "U"); + mapCode.put(16, "V"); + mapCode.put(17, "W"); + mapCode.put(18, "X"); + mapCode.put(19, "Y"); + mapCode.put(20, "g"); + mapCode.put(40, "h"); + mapCode.put(60, "i"); + mapCode.put(80, "j"); + mapCode.put(100, "k"); + mapCode.put(120, "l"); + mapCode.put(140, "m"); + mapCode.put(160, "n"); + mapCode.put(180, "o"); + mapCode.put(200, "p"); + mapCode.put(220, "q"); + mapCode.put(240, "r"); + mapCode.put(260, "s"); + mapCode.put(280, "t"); + mapCode.put(300, "u"); + mapCode.put(320, "v"); + mapCode.put(340, "w"); + mapCode.put(360, "x"); + mapCode.put(380, "y"); + mapCode.put(400, "z"); + } + public String convertfromImg(BufferedImage image) throws IOException { + String body = createBody(image); + if(compressHex) + body = encodeHexAscii(body); + return headDoc() + body + footDoc(); + } + private String createBody(BufferedImage orginalImage) throws IOException { + StringBuffer sb = new StringBuffer(); + Graphics2D graphics = orginalImage.createGraphics(); + graphics.drawImage(orginalImage, 0, 0, null); + int height = orginalImage.getHeight(); + int width = orginalImage.getWidth(); + int rgb, red, green, blue, index=0; + char auxBinaryChar[] = {'0', '0', '0', '0', '0', '0', '0', '0'}; + widthBytes = width/8; + if(width%8>0){ + widthBytes= (((int)(width/8))+1); + } else { + widthBytes= width/8; + } + this.total = widthBytes*height; + for (int h = 0; h> 16 ) & 0x000000FF; + green = (rgb >> 8 ) & 0x000000FF; + blue = (rgb) & 0x000000FF; + char auxChar = '1'; + int totalColor = red + green + blue; + if(totalColor>blackLimit){ + auxChar = '0'; + } + auxBinaryChar[index] = auxChar; + index++; + if(index==8 || w==(width-1)){ + sb.append(fourByteBinary(new String(auxBinaryChar))); + auxBinaryChar = new char[]{'0', '0', '0', '0', '0', '0', '0', '0'}; + index=0; + } + } + sb.append("\n"); + } + return sb.toString(); + } + private String fourByteBinary(String binaryStr){ + int decimal = Integer.parseInt(binaryStr,2); + if (decimal>15){ + return Integer.toString(decimal,16).toUpperCase(); + } else { + return "0" + Integer.toString(decimal,16).toUpperCase(); + } + } + private String encodeHexAscii(String code){ + int maxlinea = widthBytes * 2; + StringBuffer sbCode = new StringBuffer(); + StringBuffer sbLinea = new StringBuffer(); + String previousLine = null; + int counter = 1; + char aux = code.charAt(0); + boolean firstChar = false; + for(int i = 1; i< code.length(); i++ ){ + if(firstChar){ + aux = code.charAt(i); + firstChar = false; + continue; + } + if(code.charAt(i)=='\n'){ + if(counter>=maxlinea && aux=='0'){ + sbLinea.append(","); + } else if(counter>=maxlinea && aux=='F'){ + sbLinea.append("!"); + } else if (counter>20){ + int multi20 = (counter/20)*20; + int resto20 = (counter%20); + sbLinea.append(mapCode.get(multi20)); + if(resto20!=0){ + sbLinea.append(mapCode.get(resto20) + aux); + } else { + sbLinea.append(aux); + } + } else { + sbLinea.append(mapCode.get(counter) + aux); + if(mapCode.get(counter)==null){ + } + } + counter = 1; + firstChar = true; + if(sbLinea.toString().equals(previousLine)){ + sbCode.append(":"); + } else { + sbCode.append(sbLinea.toString()); + } + previousLine = sbLinea.toString(); + sbLinea.setLength(0); + continue; + } + if(aux == code.charAt(i)){ + counter++; + } else { + if(counter>20){ + int multi20 = (counter/20)*20; + int mod20 = (counter%20); + sbLinea.append(mapCode.get(multi20)); + if(mod20!=0){ + sbLinea.append(mapCode.get(mod20) + aux); + } else { + sbLinea.append(aux); + } + } else { + sbLinea.append(mapCode.get(counter) + aux); + } + counter = 1; + aux = code.charAt(i); + } + } + return sbCode.toString(); + } + private String headDoc(){ + String str = "^XA " + + "^FO0,0^GFA,"+ total + ","+ total + "," + widthBytes +", "; + return str; + } + private String footDoc(){ + String str = "^FS"+ + "^XZ"; + return str; + } + public void setCompressHex(boolean compressHex) { + this.compressHex = compressHex; + } + public void setBlacknessLimitPercentage(int percentage){ + blackLimit = (percentage * 768 / 100); + } + public static void main(String[] args) throws Exception { + BufferedImage orginalImage = ImageIO.read(new File("/Users/alan/Downloads/u252.png")); + ZPLConveter zp = new ZPLConveter(); + zp.setCompressHex(true); + zp.setBlacknessLimitPercentage(50); + System.out.println(zp.convertfromImg(orginalImage)); + } +} \ No newline at end of file diff --git a/emis-biz/src/main/java/com/xdadan/erp/emis/service/print/tanex/tmp/ZPLConveterUtil.java b/emis-biz/src/main/java/com/xdadan/erp/emis/service/print/tanex/tmp/ZPLConveterUtil.java new file mode 100644 index 000000000..8bcc92cc8 --- /dev/null +++ b/emis-biz/src/main/java/com/xdadan/erp/emis/service/print/tanex/tmp/ZPLConveterUtil.java @@ -0,0 +1,381 @@ +package com.xdadan.erp.emis.service.print.tanex.tmp; + +import lombok.extern.slf4j.Slf4j; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.rendering.ImageType; +import org.apache.pdfbox.rendering.PDFRenderer; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +@Slf4j +public class ZPLConveterUtil { + private static int blackLimit = 380; + private static int total; + private static int widthBytes; + /** + * 是否压缩 true=压缩 false=不压缩 + **/ + private static boolean isCompressHex = true; + private static Map mapCode = new HashMap(); + { + mapCode.put(1, "G"); + mapCode.put(2, "H"); + mapCode.put(3, "I"); + mapCode.put(4, "J"); + mapCode.put(5, "K"); + mapCode.put(6, "L"); + mapCode.put(7, "M"); + mapCode.put(8, "N"); + mapCode.put(9, "O"); + mapCode.put(10, "P"); + mapCode.put(11, "Q"); + mapCode.put(12, "R"); + mapCode.put(13, "S"); + mapCode.put(14, "T"); + mapCode.put(15, "U"); + mapCode.put(16, "V"); + mapCode.put(17, "W"); + mapCode.put(18, "X"); + mapCode.put(19, "Y"); + mapCode.put(20, "g"); + mapCode.put(40, "h"); + mapCode.put(60, "i"); + mapCode.put(80, "j"); + mapCode.put(100, "k"); + mapCode.put(120, "l"); + mapCode.put(140, "m"); + mapCode.put(160, "n"); + mapCode.put(180, "o"); + mapCode.put(200, "p"); + mapCode.put(220, "q"); + mapCode.put(240, "r"); + mapCode.put(260, "s"); + mapCode.put(280, "t"); + mapCode.put(300, "u"); + mapCode.put(320, "v"); + mapCode.put(340, "w"); + mapCode.put(360, "x"); + mapCode.put(380, "y"); + mapCode.put(400, "z"); + } + + /** + * the BufferedImage to String + * + * @param orginalImage + * @return + * @throws IOException + */ + private static String createBody(BufferedImage orginalImage) throws IOException { + StringBuffer sb = new StringBuffer(); + Graphics2D graphics = orginalImage.createGraphics(); + graphics.drawImage(orginalImage, 0, 0, null); + int height = orginalImage.getHeight(); + int width = orginalImage.getWidth(); + int rgb, red, green, blue, index=0; + char auxBinaryChar[] = {'0', '0', '0', '0', '0', '0', '0', '0'}; + widthBytes = width/8; + if(width%8>0){ + widthBytes= (((int)(width/8))+1); + } else { + widthBytes= width/8; + } + total = widthBytes*height; + for (int h = 0; h> 16 ) & 0x000000FF; + green = (rgb >> 8 ) & 0x000000FF; + blue = (rgb) & 0x000000FF; + char auxChar = '1'; + int totalColor = red + green + blue; + if(totalColor>blackLimit){ + auxChar = '0'; + } + auxBinaryChar[index] = auxChar; + index++; + if(index==8 || w==(width-1)){ + sb.append(fourByteBinary(new String(auxBinaryChar))); + auxBinaryChar = new char[]{'0', '0', '0', '0', '0', '0', '0', '0'}; + index=0; + } + } + sb.append("\n"); + } + return sb.toString(); + } + + + private static String fourByteBinary(String binaryStr){ + int decimal = Integer.parseInt(binaryStr,2); + if (decimal>15){ + return Integer.toString(decimal,16).toUpperCase(); + } else { + return "0" + Integer.toString(decimal,16).toUpperCase(); + } + } + + /** + * Image to zpl string data. + * + * @param image + * @return + * @throws IOException + */ + public String convertFromImg(BufferedImage image) throws IOException { + String cuerpo = createBody(image); + if (isCompressHex) { + cuerpo = encodeHexAscii(cuerpo, 20); + } + return headDoc() + cuerpo + footDoc(); + + } + + private static String encodeHexAscii(String code){ + int maxlinea = widthBytes * 2; + StringBuffer sbCode = new StringBuffer(); + StringBuffer sbLinea = new StringBuffer(); + String previousLine = null; + int counter = 1; + char aux = code.charAt(0); + boolean firstChar = false; + for(int i = 1; i< code.length(); i++ ){ + if(firstChar){ + aux = code.charAt(i); + firstChar = false; + continue; + } + if(code.charAt(i)=='\n'){ + if(counter>=maxlinea && aux=='0'){ + sbLinea.append(","); + } else if(counter>=maxlinea && aux=='F'){ + sbLinea.append("!"); + } else if (counter>20){ + int multi20 = (counter/20)*20; + int resto20 = (counter%20); + sbLinea.append(mapCode.get(multi20)); + if(resto20!=0){ + sbLinea.append(mapCode.get(resto20) + aux); + } else { + sbLinea.append(aux); + } + } else { + sbLinea.append(mapCode.get(counter) + aux); + if(mapCode.get(counter)==null){ + } + } + counter = 1; + firstChar = true; + if(sbLinea.toString().equals(previousLine)){ + sbCode.append(":"); + } else { + sbCode.append(sbLinea.toString()); + } + previousLine = sbLinea.toString(); + sbLinea.setLength(0); + continue; + } + if(aux == code.charAt(i)){ + counter++; + } else { + if(counter>20){ + int multi20 = (counter/20)*20; + int resto20 = (counter%20); + sbLinea.append(mapCode.get(multi20)); + if(resto20!=0){ + sbLinea.append(mapCode.get(resto20) + aux); + } else { + sbLinea.append(aux); + } + } else { + sbLinea.append(mapCode.get(counter) + aux); + } + counter = 1; + aux = code.charAt(i); + } + } + return sbCode.toString(); + } + + private String encodeHexAscii(String code,int number) { + int maxlinea = widthBytes * 2; + StringBuffer sbCode = new StringBuffer(); + StringBuffer sbLinea = new StringBuffer(); + String previousLine = null; + int counter = 1; + char aux = code.charAt(0); + boolean firstChar = false; + for (int i = 1; i < code.length(); i++) { + if (firstChar) { + aux = code.charAt(i); + firstChar = false; + continue; + } + if (code.charAt(i) == '\n') { + if (counter >= maxlinea && aux == '0') { + sbLinea.append(","); + } else if (counter >= maxlinea && aux == 'F') { + sbLinea.append("!"); + } else if (counter > number) { + int multi20 = (counter / number) * number; + int resto20 = (counter % number); + sbLinea.append(mapCode.get(multi20)); + if (resto20 != 0) { + sbLinea.append(mapCode.get(resto20) + aux); + } else { + sbLinea.append(aux); + } + } else { + sbLinea.append(mapCode.get(counter) + aux); + if (mapCode.get(counter) == null) { + } + } + counter = 1; + firstChar = true; + if (sbLinea.toString().equals(previousLine)) { + sbCode.append(":"); + } else { + sbCode.append(sbLinea.toString()); + } + previousLine = sbLinea.toString(); + sbLinea.setLength(0); + continue; + } + if (aux == code.charAt(i)) { + counter++; + } else { + if (counter > number) { + int multi20 = (counter / number) * number; + int resto20 = (counter % number); + sbLinea.append(mapCode.get(multi20)); + if (resto20 != 0) { + sbLinea.append(mapCode.get(resto20) + aux); + } else { + sbLinea.append(aux); + } + } else { + sbLinea.append(mapCode.get(counter) + aux); + } + counter = 1; + aux = code.charAt(i); + } + } + return sbCode.toString(); + } + + private static String headDoc(){ + String str = "^XA " + + "^FO0,0^GFA,"+ total + ","+ total + "," + widthBytes +", "; + return str; + } + + private static String footDoc(){ + String str = "^FS"+ + "^XZ"; + return str; + } + + public void setCompressHex(boolean compressHex) { + this.isCompressHex = compressHex; + } + public void setBlacknessLimitPercentage(int percentage){ + blackLimit = (percentage * 768 / 100); + } + + + /** + * PDF转ZPL格式 + * @param pdf pdf文件字节数组 + * @return 返回ZPL格式 + * @throws IOException + */ + public static String convertPdfToZpl(byte[] pdf) throws IOException { + PDDocument doc = PDDocument.load(pdf); + PDFRenderer renderer = new PDFRenderer(doc); + BufferedImage orginalImage = renderer.renderImageWithDPI(0, 200); + return ZPLConveterUtil.convertImgToZpl(orginalImage); + } + + /** + * PDF转ZPL格式(压缩会返回用null表示英文字母的字符串) + * @param pdfPath pdf文件路径 + * @return 返回ZPL格式 + * @throws IOException + */ + public static String convertPdfToZpl(String pdfPath) throws IOException { + PDDocument doc = PDDocument.load(new File(pdfPath)); + PDFRenderer renderer = new PDFRenderer(doc); + BufferedImage orginalImage = renderer.renderImageWithDPI(0, 200); + return ZPLConveterUtil.convertImgToZpl(orginalImage); + } + + /** + * PDF转ZPL格式(压缩不会返回用null表示的字符串,压缩的更彻底一点) + * @param pdfPath pdf文件路径 + * @return 返回ZPL格式 + */ + public static String convertPDFToZPL(String pdfPath) { + File sourceFile = new File(pdfPath); + PDDocument document = null; + try { + if (sourceFile.exists()) { + document = PDDocument.load(sourceFile); + PDFRenderer pdfRenderer = new PDFRenderer(document); + int dpi = 203;// use less dpi for to save more space in harddisk. For professional usage you + BufferedImage bImage = pdfRenderer.renderImageWithDPI(0, dpi, ImageType.RGB); + ZPLConveterUtil zplConveterUtil = new ZPLConveterUtil(); + String zplStr = zplConveterUtil.convertFromImg(bImage); + zplStr = zplStr.replace("\n", ""); + return zplStr; + } else { + log.error("generate pdf failed"); + } + } catch (Exception e) { + log.error("error is {}", e); + } finally { + try { + if (document != null) { + document.close(); + } + } catch (IOException e) { + log.error("close stram failed,the exception is {}", e); + } + } + return ""; + } + + /** + * PDF转ZPL格式 + * @param imagePath 图片文件路径 + * @return 返回ZPL格式 + * @throws IOException + */ + public static String convertImgToZpl(String imagePath) throws IOException { + BufferedImage orginalImage = ImageIO.read(new File(imagePath)); + return ZPLConveterUtil.convertImgToZpl(orginalImage); + } + + public static String convertImgToZpl(BufferedImage image) throws IOException { + String cuerpo = createBody(image); + if(isCompressHex) + cuerpo = encodeHexAscii(cuerpo); + return headDoc() + cuerpo + footDoc(); + } + + public static void main(String[] args) throws Exception { + String pdf = ZPLConveterUtil.convertPdfToZpl("D:\\label.pdf"); + System.out.println(pdf); + +// String pdf2 = ZPLConveterUtil.convertPDFToZPL("D:\\label.pdf"); +// System.out.println(pdf2); + } +} +