当前位置: 首页 > news >正文

【Java】批量生成条形码-itextpdf

批量生成条形码
Controller

@ApiOperation("商品一览批量生成商品条形码")@PostMapping("/batchGenerateProdBarCode")public void batchGenerateProdBarCode(@RequestBody ProductListCondition productListCondition,HttpServletResponse response){importExportService.batchGenerateProdBarCode(response, productListCondition);}

Service

 /*** 商品一览批量生成商品条形码* @param productListCondition* @return*/public void batchGenerateProdBarCode(HttpServletResponse response,ProductListCondition productListCondition){List<MProductEx> productExList = mProductListDao.selectmproduct(productListCondition.getProdCdDis(),productListCondition.getBrand());try {int dataCount = productExList.stream().filter(f -> StringUtils.isNotEmpty(f.getProdLabel())).collect(Collectors.toList()).size();if(dataCount == 0){response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "没有要生成的条码请调整查询条件后重新生成");}if(CollectionUtil.isNotEmpty(productExList)){String exportFileName = URLEncoder.encode("商品条码", "UTF-8") + DateUtil.format(new Date(), "yyyyMMddHHmmss");response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + exportFileName + ".pdf");response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");response.setHeader("content-Type", "application/pdf");generateProdBarcodePDF(productExList, response.getOutputStream(),response);}else {response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "没有要生成的条码请调整查询条件后重新生成");}}catch (Exception e){e.printStackTrace();}}
/*** 批量生成商品条形码pdf文件导出适配条码打印机** @param productExList 条码数据x信息* @param os    输出流* @throws IOException*/public static void generateProdBarcodePDF(List<MProductEx> productExList, OutputStream os,HttpServletResponse response) throws IOException {Document document = null;try {document = new Document(new Rectangle(120F, 85F), 10, 2, 10, 2);PdfWriter writer = PdfWriter.getInstance(document, os);document.open();PdfContentByte cb = writer.getDirectContent();BaseFont bfChinese = BaseFont.createFont("C:\\Windows\\Fonts\\simsun.ttc,1",  BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);Font fontChinese = new Font(bfChinese, 2, Font.NORMAL);Pattern pat = Pattern.compile("[\u4e00-\u9fa5]");Matcher m = null;for (MProductEx p : productExList) {m = pat.matcher(p.getProdLabel());if(StringUtils.isNullOrEmpty(p.getProdLabel()) || m.find()){response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "商品编号不能为中文且不能为空");}document.newPage();//创建一个一列的表格PdfPTable headerTable = new PdfPTable(1);headerTable.setWidthPercentage(70.0F);PdfPCell rightCell = new PdfPCell();Image codeImage = BarcodeUtil.generateBarcodePrefixSuffix(cb, p.getProdLabel(), 22f, 6f);Phrase imageP = new Phrase("", fontChinese);//自己调整偏移值 主要是负值往下imageP.add(new Chunk(codeImage, 5, -1));String textNmShow ="商品名称:" + (StringUtils.isNotEmpty(p.getProdNm()) ? p.getProdNm() : "");String textUnitShow ="计量单位:" + (StringUtils.isNotEmpty(p.getUnitNm()) ? p.getUnitNm() : "") ;String textSpecShow ="规格: " + (StringUtils.isNotEmpty(p.getSpec()) ? p.getSpec() : "");String textModelAndUnitShow ="型号:" + (StringUtils.isNotEmpty(p.getModel()) ? p.getModel() : "") +"  " + textUnitShow;//Chunk chunkCd = new Chunk(textCdShow,fontChinese);Chunk chunkNm = new Chunk(textNmShow,fontChinese);//Chunk chunkUnit = new Chunk(textUnitShow,fontChinese);Chunk chunkSpec = new Chunk(textSpecShow,fontChinese);Chunk chunkModel = new Chunk(textModelAndUnitShow,fontChinese);rightCell = new PdfPCell();//imageP.setLeading(2f,1.5f);rightCell.addElement(imageP);//rightCell.addElement(chunkCd);rightCell.addElement(new Chunk("  ",fontChinese));rightCell.addElement(chunkNm);//rightCell.addElement(chunkUnit);rightCell.addElement(chunkSpec);rightCell.addElement(chunkModel);//false自动换行rightCell.setNoWrap(false);//行间距//rightCell.setLeading(40f,10.0f);rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);rightCell.setVerticalAlignment(Element.ALIGN_MIDDLE);rightCell.setFixedHeight(26.0f);//rightCell.setPadding(4.0f);//填充headerTable.addCell(rightCell);
//                headerTable.setSplitLate(false);
//                headerTable.setSplitRows(true);document.add(headerTable);}//document.add(headerTable);os.flush();} catch (DocumentException e) {e.printStackTrace();} finally {if(Objects.nonNull(document)){document.close();}if (Objects.nonNull(os)) {os.close();}}}/*** 批量生成商品条形码pdf文件导出适配A4纸** @param productExList 条码数据x信息* @param os    输出流* @throws IOException*/public static void generateProdBarcodeA4PDF(List<MProductEx> productExList, OutputStream os,HttpServletResponse response) throws IOException {Document document = null;try {document = new Document(new Rectangle(283F, 425F), 10, 10, 10, 10);PdfWriter writer = PdfWriter.getInstance(document, os);document.open();PdfContentByte cb = writer.getDirectContent();//判断列,一条数据只允许单列int dataCount = productExList.stream().filter(f -> StringUtils.isNotEmpty(f.getProdLabel())).collect(Collectors.toList()).size();int numColumns = 1;if (dataCount > 1) {numColumns = 2;}BaseFont bfChinese = BaseFont.createFont("C:\\Windows\\Fonts\\simsun.ttc,1",  BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);Font fontChinese = new Font(bfChinese, 5, Font.NORMAL);//创建一个两列的表格PdfPTable headerTable = new PdfPTable(numColumns);if(numColumns == 1){headerTable.setWidthPercentage(40.0f);}else {headerTable.setWidthPercentage(80.0f);}PdfPCell rightCell = null;Pattern pat = Pattern.compile("[\u4e00-\u9fa5]");Matcher m = null;for (MProductEx p : productExList) {m = pat.matcher(p.getProdLabel());if(StringUtils.isNullOrEmpty(p.getProdLabel()) || m.find()){response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "商品编号不能为中文且不能为空");}rightCell = new PdfPCell();Image codeImage = BarcodeUtil.generateBarcodePrefixSuffix(cb, p.getProdLabel(), 72, 24);Phrase imageP = new Phrase("", fontChinese);//自己调整偏移值 主要是负值往下imageP.add(new Chunk(codeImage, 15, -4));String textNmShow ="商品名称:" + (StringUtils.isNotEmpty(p.getProdNm()) ? p.getProdNm() : "");String textUnitShow ="计量单位:" + (StringUtils.isNotEmpty(p.getUnitNm()) ? p.getUnitNm() : "") ;String textSpecShow ="规格: " + (StringUtils.isNotEmpty(p.getSpec()) ? p.getSpec() : "");String textModelAndUnitShow ="型号:" + (StringUtils.isNotEmpty(p.getModel()) ? p.getModel() : "") +"  " + textUnitShow;//Chunk chunkCd = new Chunk(textCdShow,fontChinese);Chunk chunkNm = new Chunk(textNmShow,fontChinese);//Chunk chunkUnit = new Chunk(textUnitShow,fontChinese);Chunk chunkSpec = new Chunk(textSpecShow,fontChinese);Chunk chunkModel = new Chunk(textModelAndUnitShow,fontChinese);rightCell = new PdfPCell();//imageP.setLeading(2f,1.5f);rightCell.addElement(imageP);//rightCell.addElement(chunkCd);rightCell.addElement(new Chunk("  ",fontChinese));rightCell.addElement(chunkNm);//rightCell.addElement(chunkUnit);rightCell.addElement(chunkSpec);rightCell.addElement(chunkModel);//false自动换行rightCell.setNoWrap(false);//行间距//rightCell.setLeading(40f,10.0f);rightCell.setHorizontalAlignment(Element.ALIGN_LEFT);rightCell.setVerticalAlignment(Element.ALIGN_MIDDLE);rightCell.setFixedHeight(70.0f);//rightCell.setPadding(4.0f);//填充headerTable.addCell(rightCell);}if(productExList.size()%2 == 1){rightCell = new PdfPCell();new Chunk("END",fontChinese);headerTable.addCell(rightCell);}document.add(headerTable);os.flush();} catch (DocumentException e) {e.printStackTrace();} finally {if(Objects.nonNull(document)){document.close();}if (Objects.nonNull(os)) {os.close();}}}

相关文章:

【Java】批量生成条形码-itextpdf

批量生成条形码 Controller ApiOperation("商品一览批量生成商品条形码")PostMapping("/batchGenerateProdBarCode")public void batchGenerateProdBarCode(RequestBody ProductListCondition productListCondition,HttpServletResponse response){import…...

SpringBoot登录、退出、获取用户信息的session处理

1、登录方法&#xff1a;login PostMapping("/user/login")public ResponseVo<User> login(Valid RequestBody UserLoginForm userLoginForm,HttpSession session) {ResponseVo<User> userResponseVo userService.login(userLoginForm.getUsername(), …...

【软件测试】随笔系统测试报告

博主简介&#xff1a;想进大厂的打工人博主主页&#xff1a;xyk:所属专栏: 软件测试 随笔系统采用 SSM 框架前后端分离的方法实现&#xff0c;本文主要针对功能&#xff1a;登录&#xff0c;注册&#xff0c;注销&#xff0c;写随笔&#xff0c;删除随笔&#xff0c;随笔详情页…...

vue中使用html2canvas+jsPDF实现pdf的导出

导入依赖 html2canvas依赖 npm install html2canvasjspdf依赖 npm install jspdfpdf导出 以导出横向&#xff0c;A4大小的pdf为例 规律&#xff1a;1. html2canvas 中&#xff0c;在保持jsPDF中的宽高不变的情况下&#xff0c;设置html2canvas中的 width 和 height 值越小&a…...

Linux学习之firewallD

systemctl status firewalld.service查看一下firewalld服务的状态&#xff0c;发现状态是inactive (dead)。 systemctl start firewalld.service启动firewalld&#xff0c;systemctl status firewalld.service查看一下firewalld服务的状态&#xff0c;发现状态是active (runni…...

【JS学习】Object.assign 用法介绍

Object.assign 是ES6中的一个方法。该方法能够实现对象的浅复制以及对象合并。Object.assign 并不会修改目标对象本身&#xff0c;而是返回一个新的对象&#xff0c;其中包含了所有源对象的属性。 例1 2个对象合并 const target { a: 1, b: 2 }; const source { b: 3, c: 4…...

【uni-app报错】获取用户收货地址uni.chooseAddress()报错问题

chooseAddress:fail the api need to be declared in …e requiredPrivateInf 原因&#xff1a; 小程序配置 / 全局配置 (qq.com) 解决&#xff1a; 登录小程序后台申请接口 按照流程申请即可 在项目根目录中找到 manifest.json 文件&#xff0c;在左侧导航栏选择源码视图&a…...

机器学习、cv、nlp的一些前置知识

为节省篇幅&#xff0c;不标注文章来源和文章的问题场景。大部分是我的通俗理解。 文章目录 向量关于向量的偏导数&#xff1a;雅可比矩阵二阶导数矩阵&#xff1a;海森矩阵随机变量随机场伽马函数beta分布数学术语坐标上升法协方差训练集&#xff0c;验证集&#xff0c;测试集…...

Steam 灵感的游戏卡悬停效果

先看效果&#xff1a; 再看代码&#xff08;查看更多&#xff09;&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Steam 灵感的游戏卡悬停效果</title><style>* {margin: …...

[Openwrt]一步一步搭建MT7981A uboot、atf、openwrt-21.02开发环境操作说明

安装ubuntu-18.04 软件安装包 ubuntu-18.04-desktop-amd64.iso 修改ubuntu管理员密码 sudo passwd [sudo] password for w1804: Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully 更新ubuntu源 备份源 sudo cp /etc/apt/so…...

Unity C# 之 Azure 微软SSML语音合成TTS流式获取音频数据以及表情嘴型 Animation 的简单整理

Unity C# 之 Azure 微软SSML语音合成TTS流式获取音频数据以及表情嘴型 Animation 的简单整理 目录 Unity C# 之 Azure 微软SSML语音合成TTS流式获取音频数据以及表情嘴型 Animation 的简单整理 一、简单介绍 二、实现原理 三、注意事项 四、实现步骤 五、关键代码 一、简…...

安全学习DAY16_信息打点-CDN绕过

信息打点-CDN绕过 文章目录 信息打点-CDN绕过本节思维导图相关链接&工具站&项目工具前置知识&#xff1a;CDN配置&#xff1a;配置1&#xff1a;加速域名-需要启用加速的域名配置2&#xff1a;加速区域-需要启用加速的地区配置3&#xff1a;加速类型-需要启用加速的资源…...

genism word2vec方法

文章目录 概述使用示例模型的保存与使用训练参数详解&#xff08;[原链接](https://blog.csdn.net/weixin_44852067/article/details/130221655)&#xff09;语料库训练 概述 word2vec是按句子来处理的Sentences(句子们) 使用示例 from gensim.models import Word2Vec #sent…...

vue3自定义样式-路由-axios拦截器

基于vue,vite和elementPlus 基于elementPlus自定义样式 history模式的路由 在根目录配置jsconfig.json&#xff0c;添加json的配置项。输入自动联想到src目录&#xff0c;是根路径的别名拦截器 如果存在多个接口地址&#xff0c;可以配置多个axios实例 数据持久化之后&#x…...

【mysql】事务的四种特性的理解

&#x1f307;个人主页&#xff1a;平凡的小苏 &#x1f4da;学习格言&#xff1a;命运给你一个低的起点&#xff0c;是想看你精彩的翻盘&#xff0c;而不是让你自甘堕落&#xff0c;脚下的路虽然难走&#xff0c;但我还能走&#xff0c;比起向阳而生&#xff0c;我更想尝试逆风…...

C++中List的实现

前言 数据结构中&#xff0c;我们了解到了链表&#xff0c;但是我们使用时需要自己去实现链表才能用&#xff0c;但是C出现了list将这一切皆变为现。list可以看作是一个带头双向循环的链表结构&#xff0c;并且可以在任意的正确范围内进行增删查改数据的容器。list容器一样也是…...

ElementUI 树形表格的使用以及表单嵌套树形表格的校验问题等汇总

目录 一、树形表格如何添加序号体现层级关系 二、树形表格展开收缩图标位置放置&#xff0c;设置指定列 三、表单嵌套树形表格的校验问题以及如何给校验rules传参 普通表格绑定如下&#xff1a;这种方法只能校验表格的第一层&#xff0c;树形需要递归设置子级节点prop。 树…...

解决“Unable to start embedded Tomcat“错误的完整指南

系列文章目录 文章目录 系列文章目录前言一、查看错误信息二、确认端口是否被占用三、检查依赖版本兼容性四、清理临时文件夹五、检查应用程序配置六、检查依赖冲突七、查看异常堆栈信息八、升级或降级Spring Boot版本总结前言 在使用Spring Boot开发应用程序时,有时可能会遇…...

JVS开源基础框架:平台基本信息介绍

JVS是面向软件开发团队可以快速实现应用的基础开发脚手架&#xff0c;主要定位于企业信息化通用底座&#xff0c;采用微服务分布式框架&#xff0c;提供丰富的基础功能&#xff0c;集成众多业务引擎&#xff0c;它灵活性强&#xff0c;界面化配置对开发者友好&#xff0c;底层容…...

C++ - max_element

在C中&#xff0c;要找到一个数组中的最大元素&#xff0c;可以使用 std::max_element 函数。以下是使用步骤&#xff1a; 包含 <algorithm> 头文件&#xff0c;这里定义了 std::max_element 函数。声明一个数组&#xff0c;并初始化它。使用 std::max_element 函数来查找…...

如何彻底解决文献格式混乱?Zotero格式规范化处理工具的创新方案

如何彻底解决文献格式混乱&#xff1f;Zotero格式规范化处理工具的创新方案 【免费下载链接】zotero-format-metadata Linter for Zotero. A plugin for Zotero to format item metadata. Shortcut to set title rich text; set journal abbreviations, university places, and…...

Phi-3-mini-4k-instruct-gguf一文详解:从网页问答到摘要改写的全流程应用

Phi-3-mini-4k-instruct-gguf一文详解&#xff1a;从网页问答到摘要改写的全流程应用 1. 认识Phi-3-mini-4k-instruct-gguf Phi-3-mini-4k-instruct-gguf是微软Phi-3系列中的轻量级文本生成模型GGUF版本。这个模型特别适合处理问答、文本改写、摘要整理和简短创作等任务。想象…...

机械革命无界14X实战:用VMware 17.5给AMD 8845HS装macOS 15(附8核/16核OC引导)

机械革命无界14X实战&#xff1a;AMD 8845HS笔记本在VMware 17.5上运行macOS 15全攻略 最近不少技术爱好者都在尝试将macOS系统运行在AMD平台的笔记本上&#xff0c;尤其是搭载锐龙8845HS处理器的设备。作为一款性能强劲的移动处理器&#xff0c;8845HS配合780M核显确实具备运…...

AutoRaise:macOS窗口悬停管理的技术实现与配置指南

AutoRaise&#xff1a;macOS窗口悬停管理的技术实现与配置指南 【免费下载链接】AutoRaise AutoRaise (and focus) a window when hovering over it with the mouse 项目地址: https://gitcode.com/gh_mirrors/au/AutoRaise AutoRaise是一款基于Objective-C开发的macOS窗…...

uniapp集成腾讯地图:从marker点聚合到轨迹回放的跨端实战与性能调优

1. uniapp集成腾讯地图SDK的核心步骤 第一次在uniapp里用腾讯地图SDK时&#xff0c;我踩了个大坑——直接在H5端跑代码发现地图出不来。后来才明白&#xff0c;腾讯地图在H5端需要单独配置安全域名。具体操作是在腾讯地图开放平台申请key时&#xff0c;必须把H5的域名加入白名单…...

5大核心功能解密:douyin-downloader抖音下载器实战指南

5大核心功能解密&#xff1a;douyin-downloader抖音下载器实战指南 【免费下载链接】douyin-downloader A practical Douyin downloader for both single-item and profile batch downloads, with progress display, retries, SQLite deduplication, and browser fallback supp…...

内容营销对 SEO 有什么影响

<h3 id"seo">内容营销对 SEO 有什么影响</h3> <h4 id"">引言</h4> <p>在当今数字化时代&#xff0c;搜索引擎优化&#xff08;SEO&#xff09;和内容营销被广泛认为是网站流量和业务增长的关键驱动因素。许多企业在网站建设…...

RePKG终极指南:Wallpaper Engine资源提取与转换的完整解决方案

RePKG终极指南&#xff1a;Wallpaper Engine资源提取与转换的完整解决方案 【免费下载链接】repkg Wallpaper engine PKG extractor/TEX to image converter 项目地址: https://gitcode.com/gh_mirrors/re/repkg 你是否曾经遇到过这样的问题&#xff1f;在Wallpaper Eng…...

Lingbot-Depth-Pretrain-ViTL-14 Anaconda环境搭建:创建隔离的Python开发与推理环境

Lingbot-Depth-Pretrain-ViTL-14 Anaconda环境搭建&#xff1a;创建隔离的Python开发与推理环境 你是不是也遇到过这种情况&#xff1a;好不容易跟着教程跑通了一个AI项目&#xff0c;结果过两天想跑另一个项目时&#xff0c;发现各种库版本冲突&#xff0c;报错满天飞&#x…...

解锁微信多设备协同新体验:WeChatPad技术全解析

解锁微信多设备协同新体验&#xff1a;WeChatPad技术全解析 【免费下载链接】WeChatPad 强制使用微信平板模式 项目地址: https://gitcode.com/gh_mirrors/we/WeChatPad WeChatPad通过创新的设备伪装技术&#xff0c;突破微信单设备登录限制&#xff0c;实现手机与平板的…...