本文主要内容

  • 二维码生成
  • 二维码标签预览及打印

二维码生成

笔者此次的二维码是通过调用第三方接口生成的,具体流程如下:

  1. 根据规范要求调用第三方接口,返回二维码下载地址及二维码图片的属性值(图片大小等)
  2. 根据返回的值获取二维码下载地址,根据地址下载二维码图片,保存至本地磁盘及记录数据信息
  3. 扫描二维码跳转相应地址,显示资产的基本信息
  4. 最终生成的二维码如下图所示:

预览并批量打印二维码标签

先给大家看最终预览效果:

事先配置好标签打印机(标签亚银纸、树脂碳带等),点击右上方打印按钮,即可打印。打印标签效果如图:

前端

目前前端用的是 easyui,直接上代码:

{id: 'print',text: '打印',iconCls: 'icon-print',handler: function () {var rows = $('#dg').datagrid('getSelections');if (rows.length > 0) {var ids = "";for (var i = 0; i < rows.length; i++) {ids += rows[i].ID + ","}var id = ids.substring(0, ids.length - 1);window.open("${hostPrefix}swjEqu/qrCodeManageAction!printQr.action?id=" + id);} else {alert("请选择一条记录!");}}}

后端

主要用 itextPdf 处理


package com.whread.common.web.action;import com.opensymphony.xwork2.ActionSupport;import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Map;import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;@SuppressWarnings("serial")
public class BaseAction extends ActionSupport implements ServletRequestAware,ServletContextAware, ServletResponseAware, SessionAware,ApplicationAware, ParameterAware {protected HttpServletRequest request;protected HttpServletResponse response;protected HttpSession httpSession;protected Map session;protected Map parameters;protected Map application;private ServletContext servletContext;protected String msg;protected Logger log = Logger.getLogger(this.getClass());public BaseAction() {super();}public HttpServletRequest getServletRequest() {return this.request;}public void setServletRequest(HttpServletRequest req) {this.request = req;if (request != null) {this.httpSession = request.getSession();}}public HttpSession getHttpSession() {return this.httpSession;}public HttpServletResponse getServletResponse() {return this.response;}public void setServletResponse(HttpServletResponse rep) {this.response = rep;}public Map getSession() {return this.session;}public void setSession(Map session) {this.session = session;}public ServletContext getServletContext() {return this.servletContext;}public void setServletContext(ServletContext servletContext) {this.servletContext = servletContext;}public Map getApplication() {return this.application;}public void setApplication(Map application) {this.application = application;}public Map getParameters() {return this.parameters;}public void setParameters(Map parameters) {this.parameters = parameters;}public HttpServletRequest getRequest() {return request;}public void setRequest(HttpServletRequest request) {this.request = request;}public HttpServletResponse getResponse() {return response;}public void setResponse(HttpServletResponse response) {this.response = response;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public Logger getLog() {return log;}public void setLog(Logger log) {this.log = log;}public void setHttpSession(HttpSession httpSession) {this.httpSession = httpSession;}public void writeResponseTxt(String responseTxt) {Writer writer;try {this.response.setContentType("text/html;charset=UTF-8");writer = this.response.getWriter();writer.write(responseTxt);writer.flush();writer.close();} catch (IOException e) {e.printStackTrace();}}protected void responseOutWithJson(String content) {try (PrintWriter out = response.getWriter()) {response.setContentType("application/json;charset=UTF-8");out.println(content);out.flush();} catch (Exception e) {e.printStackTrace();}}
}//需导入的核心jar包
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;public class QrCodeManageAction extends BaseAction {
/*** 预览二维码信息,并可批量打印* @return*/public void printQr() {String path = request.getServletContext().getRealPath("/");response.setContentType("application/pdf");try {response.setHeader("content-disposition", "filename=资产信息.pdf");OutputStream out = response.getOutputStream();float[] rectSize = {4, 1, 4, 2};printQrInfo(out, rectSize, path);} catch (Exception e) {e.printStackTrace();}}
}/*** 预览、打印二维码pdf* @param out* @param reportMap* @param rectSize* @param path*/public static void printQrInfo(OutputStream out, float[] rectSize, String path) throws IOException, DocumentException {// SIMHEI.TTF 文件字体文件,可下载至本地 ,此处更改为相应本地地址BaseFont bfChinese = BaseFont.createFont( path+"/fonts/SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);Rectangle rect = new Rectangle(170F, 85.0F);// A4纸张rect.setBackgroundColor(BaseColor.WHITE);//方正小标送简 常规 -标签标题BaseFont bfChinese1 = BaseFont.createFont( path+"/fonts/FZXBSJW.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);Font song22 = new Font(bfChinese1, 8, Font.BOLD);BaseFont bfChinese2 = BaseFont.createFont( path+"/fonts/SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);Font fang16 = new Font(bfChinese2, 6, Font.BOLD);BaseFont bfChinese3 = BaseFont.createFont( path+"/fonts/simkai.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);Font kai16 = new Font(bfChinese3, 7, Font.BOLD);//西文 16号 Times New RomanBaseFont bfChinese4 = BaseFont.createFont( path+"/fonts/TIMES.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);Font roman16 = new Font(bfChinese4, 7, Font.BOLD);//人名 方正行楷 FZXKJW.TTFBaseFont bfChinese5 = BaseFont.createFont( path+"/fonts/FZXKJW.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);Font fzxk16 = new Font(bfChinese5, 9);Document doc = new Document(rect, rectSize[0], rectSize[1], rectSize[2], rectSize[3]);PdfWriter writer = PdfWriter.getInstance(doc, out);writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);//文档属性doc.addTitle("资产信息");doc.open();//加入内容PdfPTable tab = new PdfPTable(1);PdfPCell cell = new PdfPCell();for (int i=0;i<infos.size();i++) {createPrintQrInfo(doc, tab, cell, song22, fang16, kai16, roman16, fzxk16);}doc.newPage();doc.close();}private static void createPrintQrInfo(Document doc, PdfPTable tab, PdfPCell cell, Font titleFont1, Font  valueFont,  Font kai16, Font roman16, Font fzxk16) throws DocumentException {//这里打印的是  60mm * 30mm 大小的标签,故这里参数很重要int [] cellWidth = {35,70,65};tab = new PdfPTable(3);tab.setLockedWidth(true);tab.setTotalWidth(165);tab.getDefaultCell().setMinimumHeight(15f);tab.setHorizontalAlignment(Element.ALIGN_CENTER);tab.setWidths(cellWidth);cell = PdfCellUtill.createCell(ObjUtils.defaultIfNull("", titleFont1,PdfPCell.ALIGN_CENTER,PdfPCell.ALIGN_MIDDLE,3,0);cell.setFixedHeight(15f);cell.setBorderWidthTop(0.5f);cell.setBorderWidthLeft(0.5f);cell.setBorderWidthRight(0.5f);cell.setUseAscender(true);cell.setVerticalAlignment(cell.ALIGN_MIDDLE);cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中tab.addCell(cell);cell = PdfCellUtill.createCell("资产名称", kai16,PdfPCell.ALIGN_CENTER,PdfPCell.ALIGN_MIDDLE,1,0);cell.setFixedHeight(15f);cell.setBorderWidthTop(0.5f);cell.setBorderWidthLeft(0.5f);tab.addCell(cell);cell = PdfCellUtill.createCell("资产名称1", valueFont,PdfPCell.ALIGN_LEFT,PdfPCell.ALIGN_MIDDLE,1,0);cell.setBorderWidthTop(0.5f);cell.setBorderWidthLeft(0.5f);tab.addCell(cell);Image imageQjPhoto = null;// 二维码图片路径try{imageQjPhoto = Image.getInstance("g://qrcode.png");} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}imageQjPhoto.scaleAbsoluteWidth(59);imageQjPhoto.scaleAbsoluteHeight(59);cell = PdfCellUtill.createCell("", valueFont,PdfPCell.ALIGN_CENTER,PdfPCell.ALIGN_MIDDLE,1,4);cell.setBorderWidthTop(0.5f);cell.setBorderWidthLeft(0.5f);cell.setBorderWidthRight(0.5f);cell.setBorderWidthBottom(0.5f);cell.addElement(imageQjPhoto);tab.addCell(cell);cell = PdfCellUtill.createCell("资产编号", kai16,PdfPCell.ALIGN_CENTER,PdfPCell.ALIGN_MIDDLE,1,0);cell.setFixedHeight(15f);cell.setBorderWidthTop(0.5f);cell.setBorderWidthLeft(0.5f);tab.addCell(cell);cell = PdfCellUtill.createCell("资产编号1", roman16,PdfPCell.ALIGN_LEFT,PdfPCell.ALIGN_MIDDLE,1,0);cell.setBorderWidthTop(0.5f);cell.setBorderWidthLeft(0.5f);tab.addCell(cell);cell = PdfCellUtill.createCell("购置日期", kai16,PdfPCell.ALIGN_CENTER,PdfPCell.ALIGN_MIDDLE,1,0);cell.setFixedHeight(15f);cell.setBorderWidthTop(0.5f);cell.setBorderWidthLeft(0.5f);tab.addCell(cell);cell = PdfCellUtill.createCell("2021-01-01", roman16,PdfPCell.ALIGN_LEFT,PdfPCell.ALIGN_MIDDLE,1,0);cell.setBorderWidthTop(0.5f);cell.setBorderWidthLeft(0.5f);tab.addCell(cell);cell = PdfCellUtill.createCell("使 用 人", kai16,PdfPCell.ALIGN_CENTER,PdfPCell.ALIGN_MIDDLE,1,0);cell.setFixedHeight(15f);cell.setBorderWidthTop(0.5f);cell.setBorderWidthLeft(0.5f);cell.setUseAscender(true);cell.setVerticalAlignment(cell.ALIGN_MIDDLE);cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中cell.setBorderWidthBottom(0.5f);tab.addCell(cell);cell = PdfCellUtill.createCell("张三", fzxk16,PdfPCell.ALIGN_LEFT,PdfPCell.ALIGN_MIDDLE,1,0);cell.setBorderWidthTop(0.5f);cell.setBorderWidthLeft(0.5f);cell.setBorderWidthBottom(0.5f);tab.addCell(cell);doc.add(tab);}
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;public class PdfCellUtill {private static PdfPCell pdfCell;/*** 创建PdfCell* @param content 内容文本(短语)* @param font  字体* @return 返回PdfCell*/public static PdfPCell createCell(String content,Font font){pdfCell = new PdfPCell(new Phrase(content, font));   pdfCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);pdfCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);pdfCell.setBorder(0);return pdfCell;}/*** * 创建PdfCell* @param content 内容文本(短语)* @param font  字体* @param horizontalAlignment 水平位置对齐* @param verticalAlignment 垂直位置对齐* @return 返回PdfCell*/public static PdfPCell createCell(String content,Font font,int horizontalAlignment,int verticalAlignment){pdfCell = new PdfPCell(new Phrase(content, font));pdfCell.setHorizontalAlignment(horizontalAlignment);pdfCell.setVerticalAlignment(verticalAlignment);pdfCell.setBorder(0);return pdfCell;}/*** * 创建PdfCell* @param content 内容文本(短语)* @param font  字体* @param horizontalAlignment 水平位置对齐* @param verticalAlignment 垂直位置对齐* @param colspan 合并列数* @param rowspan 合并行数* @return 返回PdfCell*/public static PdfPCell createCell(String content,Font font,int horizontalAlignment,int verticalAlignment ,int colspan,int rowspan){pdfCell = new PdfPCell(new Phrase(content, font));pdfCell.setHorizontalAlignment(horizontalAlignment);pdfCell.setVerticalAlignment(verticalAlignment);if(colspan>0){pdfCell.setColspan(colspan);}if(rowspan>0){pdfCell.setRowspan(rowspan);}pdfCell.setBorder(0);return pdfCell;}/*** * @param content 内容文本(段落)* @param font 字体* @return 返回PdfCell*/public static PdfPCell createCellParagraph(String content,Font font){pdfCell = new PdfPCell(new Paragraph(content, font));pdfCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);pdfCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);pdfCell.setBorder(0);return pdfCell;}/*** * @param content 内容文本(段落)* @param font 字体* @param horizontalAlignment 水平对齐* @param verticalAlignment 垂直位置对齐* @return 返回PdfCell*/public static PdfPCell createCellParagraph(String content,Font font,int horizontalAlignment,int verticalAlignment){pdfCell = new PdfPCell(new Paragraph(content, font));pdfCell.setHorizontalAlignment(horizontalAlignment);pdfCell.setVerticalAlignment(verticalAlignment);pdfCell.setBorder(0);return pdfCell;}/*** * @param content 内容文本(段落)* @param font 字体* @param horizontalAlignment 水平对齐* @param verticalAlignment 垂直位置对齐* @param colspan 合并列数* @param rowspan 合并行数* @return 返回PdfCell*/public static PdfPCell createCellParagraph(String content,Font font,int horizontalAlignment,int verticalAlignment,int colspan,int rowspan){pdfCell = new PdfPCell(new Paragraph(content, font));pdfCell.setHorizontalAlignment(horizontalAlignment);pdfCell.setVerticalAlignment(verticalAlignment);if(colspan>0){pdfCell.setColspan(colspan);}if(rowspan>0){pdfCell.setRowspan(rowspan);}pdfCell.setBorder(0);return pdfCell;}}

java 二维码生成及其标签打印相关推荐

  1. java二维码生成 使用SSM框架 搭建属于自己的APP二维码合成、解析、下载

    java二维码生成 使用SSM框架 搭建属于自己的APP二维码合成.解析.下载 自己用java搭建一个属于自己APP二维码合成网站.我的思路是这样的: 1.用户在前台表单提交APP的IOS和Andro ...

  2. 【笔记11】uniapp点击复制;mysql数据库存储emoji表情;Java 二维码生成;uniapp引入自定义图标

    目录 前言 一.uniapp 实现点击复制某段文本 二.MySQL 数据库存储 emoji 表情 三.Layui 的富文本编辑器 四.谷歌 Java 二维码生成 (1) 引入 MAVEN 依赖 五.微 ...

  3. java二维码生成-谷歌(Google.zxing)开源二维码生成学习及实例

    java二维码生成-谷歌(Google.zxing)开源二维码生成的实例及介绍  这里我们使用比特矩阵(位矩阵)的QR码编码在缓冲图片上画出二维码 实例有以下一个传入参数 OutputStream o ...

  4. java 二维码生成和解析

    2019独角兽企业重金招聘Python工程师标准>>> <!-- 二维码 --><dependency><groupId>com.google.z ...

  5. java二维码生成并可以转换

    二维码很常见,简单的二维码生成 pom中导入两个包 <!--二维码--><dependency><groupId>com.google.zxing</grou ...

  6. Java—二维码生成与识别(一)

    一.二维码生成 思路:将字符串中的每个字符转为二进制码字符串,保存在二进制码字符串数组中.对二进制码字符串数组中的每个二进制码字符串进行字符遍历,若是'0',则设置画笔颜色为白色,若是'1',则设置画 ...

  7. java 二维码生成和加密base64压码

    因为项目中要实现扫描二维码并实现登录,但本人开发的模块是服务器,跟前台传输用到的主要是json对象.所以不能直接传输图片,必须把图片加密成base64压码的形式. 首先介绍二维码生成的代码,二维码生成 ...

  8. java二维码生成导出成压缩包

    效果: 首先引入zxing依赖: <lombok.version>1.18.8</lombok.version> <zxing.version>3.3.3</ ...

  9. java 二维码生成/解码器

    /*** 二维码生成/解码器*/ public class ZxingQRCode {/*** 生成二维码* @param contents 内容* @param width 宽度* @param h ...

最新文章

  1. angular2.0学习日记1
  2. 在web开发中,为什么前端比后端更得到转行程序员的青睐?必看
  3. pytorch学习笔记(十三):Dropout
  4. html怎么把字转换为行内元素,什么是行内元素?
  5. 在Flex4中嵌入字体
  6. JSP购物车案例精简版-适合小白学习
  7. 联想m7400更换墨粉盒怎么清零_联想M7400、7600打印机换粉盒或加碳粉后,仍提示缺粉?联想7400、7600硒鼓加粉清零图解...
  8. win10 U盘无法识别修复方法
  9. CS5211,CS5212,CS5256,CS5811,CS5288资料
  10. 争对让望对思野葛对山栀注解_《笠翁对韵》四 支(4)
  11. 作为技术人员,写博客对我们到底有什么好处?为什么要写博客?
  12. 八大古都大排名(权威版)
  13. Python批量获取手机号码归属地(图文展示)
  14. 【附源码/完整教程】如何使用C语言打造人机对战版五子棋?
  15. 深度解析CentOS通过日志反查入侵
  16. static定义静态方法
  17. JS 生成uuid(v4)
  18. 机器人控制算法八之 工作空间仿真
  19. 小程序WebView远程连接报错(1202)网络出错,轻触屏幕重新加载,如何解决这个问题?
  20. Java代码简单模仿银行ATM功能

热门文章

  1. 微信公众号开发 - 开发环境搭建
  2. 高等工程数学--求解非齐次线性微分方程组
  3. GPLT PAT 编程练习
  4. css:box-shadow实现单边,多边阴影
  5. color constancy dataset(白平衡仿真数据集)
  6. 2023年美赛C题Wordle预测问题一建模及Python代码详细讲解
  7. 从ACL 2022 Onsite经历看NLP热点
  8. 脚本语言、编程语言、中间件
  9. 解决windows10 时间轴灰色的活动历史记录无法删除的问题
  10. 一步一步学网络爬虫(从python到scrapy)