背景:
在智物项目中,为了物品入库、借用、归还方便,减少用户的操作,使用扫描二维码的方式进行操作,那么如何java 后端生成二维码传入到前端就成了一个问题
业务分析:
1.寻找一个二维码的pfd文件类型
2.将二维码信息存入pdf文件中
3.将pdf文件如何传入前端
E-JCEBLUE官网
https://www.e-iceblue.cn/pdf_java_barcode/add-qrcode-in-pdf-using-java.html
实践操作:
1 Java 添加二维码到 PDF 文档 (e-iceblue.cn)
准备工作:
本项目的pom文件中添加两个依赖
<dependency> <groupId>e-iceblue</groupId> <artifactId>spire.office.free</artifactId> <version>3.1.1</version> </dependency>
<repository> <id>com.e-iceblue</id> <url>http://repo.e-iceblue.cn/repository/maven-public/</url> </repository>
需要在maven的setting文件中添加:
<mirror> <id>com.e-iceblue</id> <url>http://repo.e-iceblue.cn/repository/maven-public/</url> <mirrorOf>com.e-iceblue</mirrorOf> <name>com.e-iceblue</name> </mirror>
业务实践:
controller层:
@ApiOperation(value=“后端生成所有未打印二维码信息”)
@GetMapping("/download") public FrontResult findAllUnexportedQrCode( String isExportQRCode, HttpServletResponse response){ itemQRCodeService.findAllUnexportedQrCode( isExportQRCode); //如果拿到的数据不为空 response.reset(); response.setContentType("application/pdf"); response.setHeader("Content-disposition", "attachment;filename=qrcode-" + System.currentTimeMillis() + ".pdf"); File file = new File("ias-provider-itemqrcode/src/main/resources/qrcode.pdf"); // 改这里 try (FileInputStream inputStream = new FileInputStream(file);) { // try-with-resources byte[] b = new byte[CommonConstants.ONEBYTE]; int len; while ((len = inputStream.read(b)) > CommonConstants.ZERO_INT) { response.getOutputStream().write(b, CommonConstants.ZERO_INT, len); }; } catch (IOException e) { e.printStackTrace(); } return super.failure(); }
service层:
void findAllUnexportedQrCode(String isExportQRCode);
将二维码信息生成pdf文件
public class GenerateCode { public List<PdfImage> getPdfList(List<QrcodePojo> qrCodeModelList){ List<PdfImage> pdfImages =new ArrayList<>(); for (QrcodePojo qrcodePojo: qrCodeModelList ) { BarcodeSettings settings = new BarcodeSettings(); settings.setType(BarCodeType.QR_Code); settings.setData(qrcodePojo.getItemId()); settings.setData2D(qrcodePojo.getItemName()); settings.setX(CommonConstants.QRCODELengh); settings.setLeftMargin(CommonConstants.ZERO_INT); settings.setShowTextOnBottom(true); settings.setQRCodeECL(QRCodeECL.Q); settings.setQRCodeDataMode(QRCodeDataMode.Numeric); BarCodeGenerator generator = new BarCodeGenerator(settings); Image image = generator.generateImage(); PdfImage pdfImage =PdfImage.fromImage((BufferedImage)image); pdfImages.add(pdfImage); } return pdfImages; }
/** * @Author:RenYaBing * @Description:查询所有未导出二维码 * @CreateTime: 2023/1/30 21:13 * @param: [isExportQRCode * @return: java.util.List<com.tfjybj.itemqrcode.entity.ItemQRCodeEntity> **/ @Override public void findAllUnexportedQrCode(String isExportQRCode ) { try { //查出二维码集合列表 List<QrcodePojo> itemQRCodeEntityList =itemQRCodeMapper.findQRCode(isExportQRCode); //判断查出来的数据是否为空 if(itemQRCodeEntityList!=CommonConstants.Null_Object) { //按照一页40条数据首先获取生成pdf的页 int pages= itemQRCodeEntityList.size()/CommonConstants.FORTY+CommonConstants.ONE_INT; //获取总页数 GenerateCode generateCode = new GenerateCode(); List<PdfImage> pdfImages = generateCode.getPdfList(itemQRCodeEntityList); int startpage=CommonConstants.ZERO_INT; int i=CommonConstants.ZERO_INT; int j=CommonConstants.ZERO_INT; int count=CommonConstants.ZERO_INT; PdfDocument pdf = new PdfDocument(); while (startpage<pages&&count%CommonConstants.FORTY==CommonConstants.ZERO_INT){ PdfPageBase page = pdf.getPages().add(); while (j<CommonConstants.EIGHTHUNDRED){ while (i<=CommonConstants.FOUR_HUNDRED_AND_FROUR&&count<pdfImages.size()){ page.getCanvas().drawImage(pdfImages.get(count), i, j); i+=CommonConstants.HUNDRED_INT; count++; } j+=CommonConstants.HUNDRED_INT; i=CommonConstants.ZERO_INT; } j=CommonConstants.ZERO_INT; i=CommonConstants.ZERO_INT; startpage++; } pdf.saveToFile("ias-provider-itemqrcode/src/main/resources/qrcode.pdf"); pdf.dispose(); } }catch (Exception e){ log.error("打印导出二维码异常"); } } }