change - 原材料条码信息添加

master
yinq 8 months ago
parent 3b66195f8c
commit c6c02813c9

@ -59,6 +59,10 @@ public class MesBaseBarcodeInfo extends BaseEntity
@Excel(name = "物料ID")
private Long materialId;
/** 物料名称 */
@Excel(name = "物料ID")
private String materialName;
/** 供应商ID */
@Excel(name = "供应商ID")
private Long manufacturerId;
@ -131,6 +135,14 @@ public class MesBaseBarcodeInfo extends BaseEntity
@Excel(name = "绑定托盘时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
private Date bindTime;
public String getMaterialName() {
return materialName;
}
public void setMaterialName(String materialName) {
this.materialName = materialName;
}
public String getPrintFlag() {
return printFlag;
}

@ -20,7 +20,7 @@
<!-- RuoYi Common Core-->
<dependency>
<groupId>com.hw</groupId>
<artifactId>hw-common-core</artifactId>
<artifactId>hw-api-mes</artifactId>
</dependency>
</dependencies>

@ -0,0 +1,29 @@
package com.hw.printer.api;
import com.hw.common.core.constant.SecurityConstants;
import com.hw.common.core.constant.ServiceNameConstants;
import com.hw.common.core.domain.R;
import com.hw.mes.api.domain.MesBaseBarcodeInfo;
import com.hw.printer.api.factory.RemotePrinterFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
@FeignClient(contextId = "remotePrinterService", value = ServiceNameConstants.PRINTER_SERVICE, fallbackFactory = RemotePrinterFallbackFactory.class)
public interface RemotePrinterService {
/**
*
*
* @param barcodeInfo
* @param source
* @return
*/
@PostMapping("/printService/printBarCode")
public MesBaseBarcodeInfo printBarCode(@RequestBody MesBaseBarcodeInfo barcodeInfo, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
}

@ -0,0 +1,32 @@
package com.hw.printer.api.factory;
import com.hw.common.core.domain.R;
import com.hw.common.core.exception.ServiceException;
import com.hw.printer.api.RemotePrinterService;
import com.hw.mes.api.domain.MesBaseBarcodeInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
/**
*
*
* @author ruoyi
*/
@Component
public class RemotePrinterFallbackFactory implements FallbackFactory<RemotePrinterService> {
private static final Logger log = LoggerFactory.getLogger(RemotePrinterFallbackFactory.class);
@Override
public RemotePrinterService create(final Throwable throwable) {
log.error("打印条码服务调用失败:{}", throwable.getMessage());
return new RemotePrinterService() {
@Override
public MesBaseBarcodeInfo printBarCode(MesBaseBarcodeInfo barcodeInfo, String source) {
throw new ServiceException("打印条码服务调用异常:" + throwable.getMessage());
}
};
}
}

@ -47,4 +47,9 @@ public class ServiceNameConstants
* Systemcommonserviceid
* */
public static final String SYS_COMMON_SERVICE = "hw-system";
/**
* serviceid
* */
public static final String PRINTER_SERVICE = "hw-printer";
}

@ -81,6 +81,12 @@
<groupId>com.hw</groupId>
<artifactId>hw-api-mes</artifactId>
</dependency>
<dependency>
<groupId>com.hw</groupId>
<artifactId>hw-api-printer</artifactId>
<version>3.6.3</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>

@ -92,7 +92,6 @@ public class MesBaseBarcodeInfoController extends BaseController
@PutMapping
public AjaxResult edit(@RequestBody MesBaseBarcodeInfo mesBaseBarcodeInfo)
{
mesBaseBarcodeInfo.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
return toAjax(mesBaseBarcodeInfoService.updateMesBaseBarcodeInfo(mesBaseBarcodeInfo));
}
@ -108,6 +107,16 @@ public class MesBaseBarcodeInfoController extends BaseController
}
/**
*
* @param barcodeIds
* @return
*/
@GetMapping(value = "/printBarCode/{barcodeIds}")
public AjaxResult printBarCode(@PathVariable Long[] barcodeIds)
{
return success(mesBaseBarcodeInfoService.printBarCode(barcodeIds));
}
/**

@ -68,4 +68,12 @@ public interface IMesBaseBarcodeInfoService
* @return
*/
public int deleteMesBaseBarcodeInfoByBarcodeId(Long barcodeId);
/**
*
* @param barcodeIds
* @return
*/
public int printBarCode(Long[] barcodeIds);
}

@ -4,9 +4,15 @@ import java.math.BigDecimal;
import java.util.List;
import com.hw.common.core.constant.MesConstants;
import com.hw.common.core.constant.SecurityConstants;
import com.hw.common.core.domain.R;
import com.hw.common.core.exception.ServiceException;
import com.hw.common.core.utils.DateUtils;
import com.hw.common.core.utils.StringUtils;
import com.hw.common.core.utils.uuid.Seq;
import com.hw.common.security.utils.SecurityUtils;
import com.hw.mes.api.domain.MesBaseBarcodeInfo;
import com.hw.printer.api.RemotePrinterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.mes.mapper.MesBaseBarcodeInfoMapper;
@ -23,6 +29,9 @@ public class MesBaseBarcodeInfoServiceImpl implements IMesBaseBarcodeInfoService
@Autowired
private MesBaseBarcodeInfoMapper mesBaseBarcodeInfoMapper;
@Autowired
private RemotePrinterService remotePrinterService;
/**
*
*
@ -94,6 +103,7 @@ public class MesBaseBarcodeInfoServiceImpl implements IMesBaseBarcodeInfoService
*/
@Override
public int updateMesBaseBarcodeInfo(MesBaseBarcodeInfo mesBaseBarcodeInfo) {
mesBaseBarcodeInfo.setUpdateBy(SecurityUtils.getUsername());
mesBaseBarcodeInfo.setUpdateTime(DateUtils.getNowDate());
return mesBaseBarcodeInfoMapper.updateMesBaseBarcodeInfo(mesBaseBarcodeInfo);
}
@ -119,4 +129,39 @@ public class MesBaseBarcodeInfoServiceImpl implements IMesBaseBarcodeInfoService
public int deleteMesBaseBarcodeInfoByBarcodeId(Long barcodeId) {
return mesBaseBarcodeInfoMapper.deleteMesBaseBarcodeInfoByBarcodeId(barcodeId);
}
/**
*
*
* @param barcodeIds
* @return
*/
@Override
public int printBarCode(Long[] barcodeIds) {
StringBuilder buffer = new StringBuilder();
//获取条码List
for (Long barcodeId : barcodeIds) {
MesBaseBarcodeInfo mesBaseBarcodeInfo = mesBaseBarcodeInfoMapper.selectMesBaseBarcodeInfoByBarcodeId(barcodeId);
try {
//调用的打印接口
MesBaseBarcodeInfo barCodeInfo = remotePrinterService.printBarCode(mesBaseBarcodeInfo, SecurityConstants.INNER);
MesBaseBarcodeInfo baseBarcodeInfo = new MesBaseBarcodeInfo();
baseBarcodeInfo.setBarcodeId(barcodeId);
baseBarcodeInfo.setMachineName(barCodeInfo.getMachineName());
baseBarcodeInfo.setPrintTime(DateUtils.getNowDate());
baseBarcodeInfo.setPrintPerson(SecurityUtils.getUsername());
baseBarcodeInfo.setPrintFlag("1");
baseBarcodeInfo.setPrintNumber(StringUtils.isNull(baseBarcodeInfo.getPrintNumber()) ? 1 : baseBarcodeInfo.getPrintNumber() + 1);
baseBarcodeInfo.setAcceptedDate(DateUtils.getNowDate());
this.updateMesBaseBarcodeInfo(baseBarcodeInfo);
} catch (Exception e) {
buffer.append("打印条码报错——条码内容:").append(mesBaseBarcodeInfo.getBarcodeInfo()).append(e.getMessage());
}
}
if (buffer.length() == 0) {
return 1;
} else {
throw new ServiceException(buffer.toString());
}
}
}

@ -35,81 +35,84 @@
<result property="bindTime" column="bind_time"/>
<result property="updateBy" column="update_by"/>
<result property="updateTime" column="update_time"/>
<result property="materialName" column="material_name"/>
</resultMap>
<sql id="selectMesBaseBarcodeInfoVo">
select barcode_id,
print_time,
print_person,
batch_flag,
barcode_type,
barcode_info,
batch_code,
pallet_info_code,
material_id,
manufacturer_id,
amount,
machine_name,
print_number,
po_no,
production_date,
accepted_date,
last_outstock_date,
plan_code,
plan_detail_code,
sale_order_id,
saleorder_code,
project_no,
print_flag,
serial_number,
remark,
bind_status,
bind_by,
bind_time,
update_by,
update_time
from mes_base_barcode_info
select bbi.barcode_id,
bbi.print_time,
bbi.print_person,
bbi.batch_flag,
bbi.barcode_type,
bbi.barcode_info,
bbi.batch_code,
bbi.pallet_info_code,
bbi.material_id,
bmi.material_name,
bbi.manufacturer_id,
bbi.amount,
bbi.machine_name,
bbi.print_number,
bbi.po_no,
bbi.production_date,
bbi.accepted_date,
bbi.last_outstock_date,
bbi.plan_code,
bbi.plan_detail_code,
bbi.sale_order_id,
bbi.saleorder_code,
bbi.project_no,
bbi.print_flag,
bbi.serial_number,
bbi.remark,
bbi.bind_status,
bbi.bind_by,
bbi.bind_time,
bbi.update_by,
bbi.update_time
from mes_base_barcode_info bbi
left join mes_base_material_info bmi on bmi.material_id = bbi.material_id
</sql>
<select id="selectMesBaseBarcodeInfoList" parameterType="MesBaseBarcodeInfo" resultMap="MesBaseBarcodeInfoResult">
<include refid="selectMesBaseBarcodeInfoVo"/>
<where>
<if test="printTime != null ">and print_time = #{printTime}</if>
<if test="printPerson != null and printPerson != ''">and print_person = #{printPerson}</if>
<if test="printFlag != null and printFlag != ''">and print_flag = #{printFlag}</if>
<if test="batchFlag != null and batchFlag != ''">and batch_flag = #{batchFlag}</if>
<if test="barcodeType != null and barcodeType != ''">and barcode_type = #{barcodeType}</if>
<if test="barcodeInfo != null and barcodeInfo != ''">and barcode_info = #{barcodeInfo}</if>
<if test="batchCode != null and batchCode != ''">and batch_code = #{batchCode}</if>
<if test="palletInfoCode != null and palletInfoCode != ''">and pallet_info_code = #{palletInfoCode}</if>
<if test="materialId != null ">and material_id = #{materialId}</if>
<if test="manufacturerId != null ">and manufacturer_id = #{manufacturerId}</if>
<if test="amount != null ">and amount = #{amount}</if>
<if test="machineName != null and machineName != ''">and machine_name like concat('%', #{machineName},
<if test="printTime != null ">and bbi.print_time = #{printTime}</if>
<if test="printPerson != null and printPerson != ''">and bbi.print_person = #{printPerson}</if>
<if test="printFlag != null and printFlag != ''">and bbi.print_flag = #{printFlag}</if>
<if test="batchFlag != null and batchFlag != ''">and bbi.batch_flag = #{batchFlag}</if>
<if test="barcodeType != null and barcodeType != ''">and bbi.barcode_type = #{barcodeType}</if>
<if test="barcodeInfo != null and barcodeInfo != ''">and bbi.barcode_info = #{barcodeInfo}</if>
<if test="batchCode != null and batchCode != ''">and bbi.batch_code = #{batchCode}</if>
<if test="palletInfoCode != null and palletInfoCode != ''">and bbi.pallet_info_code = #{palletInfoCode}</if>
<if test="materialId != null ">and bbi.material_id = #{materialId}</if>
<if test="manufacturerId != null ">and bbi.manufacturer_id = #{manufacturerId}</if>
<if test="amount != null ">and bbi.amount = #{amount}</if>
<if test="machineName != null and machineName != ''">and bbi.machine_name like concat('%', #{machineName},
'%')
</if>
<if test="printNumber != null ">and print_number = #{printNumber}</if>
<if test="poNo != null and poNo != ''">and po_no = #{poNo}</if>
<if test="productionDate != null ">and production_date = #{productionDate}</if>
<if test="acceptedDate != null ">and accepted_date = #{acceptedDate}</if>
<if test="lastOutstockDate != null ">and last_outstock_date = #{lastOutstockDate}</if>
<if test="planCode != null and planCode != ''">and plan_code = #{planCode}</if>
<if test="planDetailCode != null and planDetailCode != ''">and plan_detail_code = #{planDetailCode}</if>
<if test="saleOrderId != null ">and sale_order_id = #{saleOrderId}</if>
<if test="saleorderCode != null and saleorderCode != ''">and saleorder_code = #{saleorderCode}</if>
<if test="projectNo != null and projectNo != ''">and project_no = #{projectNo}</if>
<if test="serialNumber != null and serialNumber != ''">and serial_number = #{serialNumber}</if>
<if test="bindStatus != null and bindStatus != ''">and bind_status = #{bindStatus}</if>
<if test="bindBy != null and bindBy != ''">and bind_by = #{bindBy}</if>
<if test="bindTime != null ">and bind_time = #{bindTime}</if>
<if test="updateBy != null and updateBy != ''">and update_by = #{updateBy}</if>
<if test="updateTime != null ">and update_time = #{updateTime}</if>
<if test="printNumber != null ">and bbi.print_number = #{printNumber}</if>
<if test="poNo != null and poNo != ''">and bbi.po_no = #{poNo}</if>
<if test="productionDate != null ">and bbi.production_date = #{productionDate}</if>
<if test="acceptedDate != null ">and bbi.accepted_date = #{acceptedDate}</if>
<if test="lastOutstockDate != null ">and bbi.last_outstock_date = #{lastOutstockDate}</if>
<if test="planCode != null and planCode != ''">and bbi.plan_code = #{planCode}</if>
<if test="planDetailCode != null and planDetailCode != ''">and bbi.plan_detail_code = #{planDetailCode}</if>
<if test="saleOrderId != null ">and bbi.sale_order_id = #{saleOrderId}</if>
<if test="saleorderCode != null and saleorderCode != ''">and bbi.saleorder_code = #{saleorderCode}</if>
<if test="projectNo != null and projectNo != ''">and bbi.project_no = #{projectNo}</if>
<if test="serialNumber != null and serialNumber != ''">and bbi.serial_number = #{serialNumber}</if>
<if test="bindStatus != null and bindStatus != ''">and bbi.bind_status = #{bindStatus}</if>
<if test="bindBy != null and bindBy != ''">and bbi.bind_by = #{bindBy}</if>
<if test="bindTime != null ">and bbi.bind_time = #{bindTime}</if>
<if test="updateBy != null and updateBy != ''">and bbi.update_by = #{updateBy}</if>
<if test="updateTime != null ">and bbi.update_time = #{updateTime}</if>
</where>
</select>
<select id="selectMesBaseBarcodeInfoByBarcodeId" parameterType="Long" resultMap="MesBaseBarcodeInfoResult">
<include refid="selectMesBaseBarcodeInfoVo"/>
where barcode_id = #{barcodeId}
where bbi.barcode_id = #{barcodeId}
</select>
<insert id="insertMesBaseBarcodeInfo" parameterType="MesBaseBarcodeInfo" useGeneratedKeys="true"
@ -230,6 +233,6 @@
<select id="selectMesBaseBarcodeInfoByBarcodeInfo" parameterType="String" resultMap="MesBaseBarcodeInfoResult">
<include refid="selectMesBaseBarcodeInfoVo"/>
where barcode_info = #{barcodeInfo} limit 1
where bbi.barcode_info = #{barcodeInfo} limit 1
</select>
</mapper>

@ -41,8 +41,76 @@
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<!-- Hw api mes-->
<dependency>
<groupId>com.hw</groupId>
<artifactId>hw-api-mes</artifactId>
</dependency>
<!--向下全是打印PDF-->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>jempbox</artifactId>
<version>1.8.11</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>xmpbox</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>preflight</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-tools</artifactId>
<version>2.0.0</version>
</dependency>
<!--编辑PDF-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.5</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.hw</groupId>
<artifactId>hw-common-swagger</artifactId>
</dependency>
<!-- ZXing library for barcode/QR code generation -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.5.1</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>

@ -1,19 +0,0 @@
package com.hw;
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
public static void main(String[] args) {
// Press Alt+Enter with your caret at the highlighted text to see how
// IntelliJ IDEA suggests fixing it.
System.out.printf("Hello and welcome!");
// Press Shift+F10 or click the green arrow button in the gutter to run the code.
for (int i = 1; i <= 5; i++) {
// Press Shift+F9 to start debugging your code. We have set one breakpoint
// for you, but you can always add more by pressing Ctrl+F8.
System.out.println("i = " + i);
}
}
}

@ -0,0 +1,31 @@
package com.hw.printer;
import com.hw.common.swagger.annotation.EnableCustomSwagger2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/**
*
*
* @author ruoyi
*/
@EnableCustomSwagger2
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
public class RuoYiPrinterApplication
{
public static void main(String[] args)
{
SpringApplication.run(RuoYiPrinterApplication.class, args);
System.out.println("(♥◠‿◠)ノ゙ 文件服务模块启动成功 ლ(´ڡ`ლ)゙ \n" +
" .-------. ____ __ \n" +
" | _ _ \\ \\ \\ / / \n" +
" | ( ' ) | \\ _. / ' \n" +
" |(_ o _) / _( )_ .' \n" +
" | (_,_).' __ ___(_ o _)' \n" +
" | |\\ \\ | || |(_,_)' \n" +
" | | \\ `' /| `-' / \n" +
" | | \\ / \\ / \n" +
" ''-' `'-' `-..-' ");
}
}

@ -0,0 +1,42 @@
package com.hw.printer.controller;
import com.hw.common.core.domain.R;
import com.hw.mes.api.domain.MesBaseBarcodeInfo;
import com.hw.printer.service.IPrinterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
*
* @author Yinq
*/
@RestController
@RequestMapping("/printService")
public class PrinterController {
private static final Logger log = LoggerFactory.getLogger(PrinterController.class);
@Autowired
private IPrinterService printerService;
/**
*
* @param barcodeInfo
* @return
*/
@PostMapping("/printBarCode")
public R<MesBaseBarcodeInfo> printBarCode(@RequestBody MesBaseBarcodeInfo barcodeInfo) {
try {
return R.ok(printerService.printBarCode(barcodeInfo));
} catch (Exception e) {
log.error("打印条码失败", e);
return R.fail(e.getMessage());
}
}
}

@ -0,0 +1,19 @@
package com.hw.printer.service;
import com.hw.mes.api.domain.MesBaseBarcodeInfo;
/**
*
*
* @author Yinq
*/
public interface IPrinterService {
/**
*
* @param barcodeInfo
* @return
* @throws Exception
*/
MesBaseBarcodeInfo printBarCode(MesBaseBarcodeInfo barcodeInfo) throws Exception;
}

@ -0,0 +1,61 @@
package com.hw.printer.service;
import com.hw.common.core.utils.DateUtils;
import com.hw.common.core.utils.StringUtils;
import com.hw.mes.api.domain.MesBaseBarcodeInfo;
import com.hw.printer.utils.HwPrintUtil;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Value;
import java.util.HashMap;
/**
* Service
*
* @author Yinq
*/
@Service
public class PrinterServiceImpl implements IPrinterService {
/**
*
*/
@Value("${print.path}")
public String localPrintPath;
/**
* PDF
*/
@Value("${print.pdfTemplatePath}")
public String pdfTemplatePath;
/**
* PDF
*/
@Value("${print.generatePath}")
public String generatePath;
/**
*
*
* @param barcodeInfo
* @return
* @throws Exception
*/
@Override
public MesBaseBarcodeInfo printBarCode(MesBaseBarcodeInfo barcodeInfo) throws Exception {
HashMap<String, String> params = new HashMap<>();
params.put("localPrintPath", localPrintPath);
params.put("pdfTemplatePath", pdfTemplatePath);
params.put("generatePath", generatePath);
params.put("barcodeInfo", barcodeInfo.getBarcodeInfo());
params.put("batchCode", barcodeInfo.getBatchCode());
params.put("materialName", barcodeInfo.getMaterialName());
params.put("date", DateUtils.getDate());
String machineName = HwPrintUtil.printBarCode(params);
barcodeInfo.setMaterialName(machineName);
return barcodeInfo;
}
}

@ -0,0 +1,215 @@
package com.hw.printer.utils;
import com.hw.common.core.exception.ServiceException;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPrintable;
import org.apache.pdfbox.printing.Scaling;
import javax.annotation.PostConstruct;
import javax.print.*;
import java.awt.print.*;
import java.io.*;
import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* @author wanghao
* @date 2022/4/24 14:52
*/
@Component
public class HwPrintUtil {
private static final Logger log = LoggerFactory.getLogger(HwPrintUtil.class);
/**
*
*/
public static String localPrintPath;
/**
* PDF
*/
public static String pdfTemplatePath;
/**
* PDF
*/
public static String generatePath;
/**
* List
*/
private static PrintService[] printServices;
/**
*
*/
private static PrintService printService = null;
/**
*
*/
@PostConstruct
public static void findPrinters() {
printServices = PrintServiceLookup.lookupPrintServices(null, null);
System.out.println("Bean已初始化执行@PostConstruct注解的方法");
}
/**
*
*
* @param params
* @return
*/
public static String printBarCode(HashMap<String, String> params) {
//解析参数生成打印图片
localPrintPath = params.get("localPrintPath");
pdfTemplatePath = params.get("pdfTemplatePath");
generatePath = params.get("generatePath");
String barcodeInfo = params.get("barcodeInfo");
String batchCode = params.get("batchCode");
String materialName = params.get("materialName");
String date = params.get("date");
initPrintService();
File outputFile = printBarCode(barcodeInfo, batchCode, materialName, date);
try {
printPdf(outputFile);
log.info("打印条码printBarCode方法,条码内容:" + barcodeInfo);
return localPrintPath;
} catch (Exception e) {
log.error("打印条码printPdf异常" + e);
throw new ServiceException("打印条码printPdf异常" + e);
}
}
public static File printBarCode(String barcodeInfo, String batchCode, String materialName, String date) {
try {
// 生成PDF文件
File generateFile = new File(generatePath);
FileOutputStream out = new FileOutputStream(generateFile);
// 打印模板PDF
PdfReader reader = new PdfReader(pdfTemplatePath);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// 修改PDF文档的PdfStamper实例将修改后的内容输出到内存中的字节数组
PdfStamper stamper = new PdfStamper(reader, bos);
// 动态地将变量的值填充到PDF表单相应字段
AcroFields form = stamper.getAcroFields();
form.setField("code", barcodeInfo);
form.setField("name", materialName);
form.setField("spe", batchCode);
form.setField("data", date);
//获取位置(左上右下)
AcroFields.FieldPosition fieldPosition = form.getFieldPositions("qrcode").get(0);
// 绘制二维码
float width = fieldPosition.position.getRight() - fieldPosition.position.getLeft();
BarcodeQRCode pdf417 = new BarcodeQRCode(barcodeInfo, (int) width, (int) width, null);
// 生成二维码图像
Image image128 = pdf417.getImage();
// 条码位置
image128.setAbsolutePosition(fieldPosition.position.getLeft(), fieldPosition.position.getBottom());
// 获取PDF的第一页
PdfContentByte cb = stamper.getOverContent(1);
cb.addImage(image128);
// 设置表单不可编辑即将表单字段内容固定到PDF中
stamper.setFormFlattening(true);
// 关闭PdfStamper
stamper.close();
// 创建一个新的文档对象
Document doc = new Document();
// 创建 PdfCopy 对象,关联到输出流
PdfCopy copy = new PdfCopy(doc, out);
// 打开文档
doc.open();
// 从内存中的字节数组创建一个 PdfImportedPage 对象
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
// 将导入的页面添加到新文档中
copy.addPage(importPage);
// 关闭文档
doc.close();
return generateFile;
} catch (Exception e) {
log.error("打印条码生成PDF异常" + e);
}
return null;
}
/**
* PDF
*
* @param pdfFile PDF
* @throws IOException PDF
*/
private static void printPdf(File pdfFile) throws IOException {
PDDocument document = null;
try {
document = PDDocument.load(pdfFile);
//打印的页面的大小和方向
PageFormat pageFormat = new PageFormat();
//设置打印方向 PORTRAIT 竖向 REVERSE_LANDSCAPE 横向
pageFormat.setOrientation(PageFormat.PORTRAIT);
Paper paper = new Paper();
// 纸的大小
paper.setSize(221, 200);
// Print 区域
paper.setImageableArea(2, -15, 221, 200);
pageFormat.setPaper(paper);
Book book = new Book();
PDFPrintable printable = new PDFPrintable(document, Scaling.SCALE_TO_FIT);
book.append(printable, pageFormat, document.getNumberOfPages());
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(printService);
job.setPageable(book);
job.defaultPage();
try {
job.print();
} catch (PrinterException e) {
throw new IOException("PDF打印失败:", e);
}
} catch (PrinterException e) {
throw new ServiceException("打印机服务错误:" + e);
} finally {
if (document != null) {
try {
document.close();
} catch (IOException e) {
// 处理关闭文档时可能出现的异常
e.printStackTrace();
}
}
}
}
/**
* PDF
* WPS PDF
* OneNote 2013
* Microsoft XPS Document Writer
* Microsoft Print to PDF
* Fax
* \\10.11.43.159\HP LaserJet MFP M129-M134
* \\10.11.43.159\HP Laser NS 1020
*/
public static void initPrintService() {
for (PrintService service : printServices) {
System.out.println(service.getName());
if (service.getName().contains(localPrintPath)) {
printService = service;
break;
}
}
if (printService == null) {
throw new ServiceException("打印机配置错误!");
}
}
}

@ -1,10 +1,6 @@
Spring Boot Version: ${spring-boot.version}
Spring Application Name: ${spring.application.name}
_ _
(_) | |
_ __ _ _ ___ _ _ _ ______ ___ _ _ ___ | |_ ___ _ __ ___
| '__|| | | | / _ \ | | | || ||______|/ __|| | | |/ __|| __| / _ \| '_ ` _ \
| | | |_| || (_) || |_| || | \__ \| |_| |\__ \| |_ | __/| | | | | |
|_| \__,_| \___/ \__, ||_| |___/ \__, ||___/ \__| \___||_| |_| |_|
__/ | __/ |
|___/ |___/
/ __ ___ __ ( ) __ __ ___ ___ __
// ) ) // / / / / ____ // ) ) // ) ) / / // ) ) / / //___) ) // ) )
// / / // / / / / //___/ / // / / // / / / / // //
// / / ((__( (__/ / // // / / // / / / / ((____ //

@ -103,7 +103,7 @@
<dict-tag :options="dict.type.active_flag" :value="scope.row.batchFlag"/>
</template>
</el-table-column>
<el-table-column label="物料名称" align="center" prop="materialId" v-if="columns[8].visible" width="100"/>
<el-table-column label="物料名称" align="center" prop="materialName" v-if="columns[8].visible" width="100"/>
<el-table-column label="供应商ID" align="center" prop="manufacturerId" v-if="columns[9].visible"/>
<el-table-column label="数量" align="center" prop="amount" v-if="columns[10].visible"/>
<el-table-column label="生产日期" align="center" prop="productionDate" width="180" v-if="columns[14].visible">
@ -542,9 +542,8 @@ export default {
/** 打印条码按钮操作 */
handlePrintBarCode(row) {
const barcodeId = row.barcodeId || this.ids
printBarCode(barcodeId).then(response => {
const barcodeIds = row.barcodeId || this.ids;
printBarCode(barcodeIds).then(response => {
this.$modal.msgSuccess("打印条码成功");
}).catch(() => {

Loading…
Cancel
Save