“无边落木萧萧下,不尽长江滚滚来”
前言
最近在帮别人做个打印的功能,就是登记完学生信息后,用小票打印出学生信息。打印预览图如下:
实现
首先我用Java代码实现:
import com.xxx.students.domain.StudentInfo;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.print.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class PrintUtils implements Printable {
private StudentInfo studentInfo;
private HttpServletRequest request;
public PrintUtils(HttpServletRequest request,StudentInfo studentInfo) {
this.studentInfo = studentInfo;
this.request = request;
}
public PrintUtils() {
}
@Override
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (page > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) g;
g2d.setFont(new Font("Default", Font.PLAIN, 20));
g2d.drawString("测试打印", 60, 20);
g2d.drawString("-------------------------------------", 7, 30);
g2d.setFont(new Font("Default", Font.PLAIN, 12));
g2d.drawString("学生姓名:" + studentInfo.getName(), 5, 45);
g2d.drawString("手机号码:" + studentInfo.getParentPhone(), 5, 75);
g2d.drawString("课程名称:" + studentInfo.getCourseName(), 5, 105);
g2d.drawString("缴费金额:" + studentInfo.getPaymentAmount() + " RMB", 5, 135);
g2d.drawString("支付方式:" + studentInfo.getPaymentModeName(), 5, 165);
g2d.drawString("打印时间:" + sdf.format(new Date()), 5, 195);
g2d.drawImage(writeQrCodeContent(),50, 225,100,100, null);
return PAGE_EXISTS;
}
/**
*@author ruanhui
*@date 2018/12/10
*@description 生成二维码
*/
public Image writeQrCodeContent() {
java.awt.Image im = null ;
try {
//获取图片路径
String filePath = request.getSession().getServletContext().getRealPath("/") + "resource/images/";
System.out.println("filePath:" + filePath) ;
File file=new File(filePath + "xxx.jpg");
System.out.println(file.getName());
InputStream is = new FileInputStream(file);
BufferedImage bi;
bi = ImageIO.read(is);
im = (java.awt.Image)bi;
}catch (Exception e) {
e.printStackTrace();
}
return im;
}
//打印方法
public static void print(HttpServletRequest request,StudentInfo studentInfo) {
int height = 250 + 3 * 15 + 20;
// 通俗理解就是书、文档
Book book = new Book();
// 打印格式
PageFormat pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT);
// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
Paper p = new Paper();
p.setSize(230, height);
p.setImageableArea(5, -20, 230, height + 20);
pf.setPaper(p);
// 把 PageFormat 和 Printable 添加到书中,组成一个页面
book.append(new PrintUtils(request,studentInfo), pf);
// 获取打印服务对象
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(book);
try {
job.print();
} catch (PrinterException e) {
System.out.println("================打印出现异常");
}
}
}
这段java代码能实现在本地运行中调用打印机并且打印。但是把项目放到服务器上就不能调本地的打印机了。
于是,有了第二种打印方法,用JS实现:
//测试打印
function doTestPrint() {
var nowDate = new Date();
//初始化打印机
var LODOP=getLodop();
LODOP.PRINT_INIT("测试打印"); //首先一个初始化语句
LODOP.SET_PRINT_STYLE("FontSize", 25); //字体大小
LODOP.ADD_PRINT_TEXT(20,50,150,20,"xxxx");//然后多个ADD语句及SET语句
LODOP.SET_PRINT_STYLE("FontSize", 10); //字体大小
LODOP.ADD_PRINT_TEXT(50,5,250,20,"--------------------------------");//然后多个ADD语句及SET语句
LODOP.ADD_PRINT_TEXT(70,5,180,20,"学生姓名:诺贝尔爱情奖");//然后多个ADD语句及SET语句
LODOP.ADD_PRINT_TEXT(95,5,180,20,"手机号码:123456");//然后多个ADD语句及SET语句
LODOP.ADD_PRINT_TEXT(120,5,180,20,"课程名称:年卡");//然后多个ADD语句及SET语句
LODOP.ADD_PRINT_TEXT(145,5,180,20,"缴费金额:1000RMB");//然后多个ADD语句及SET语句
LODOP.ADD_PRINT_TEXT(170,5,180,20,"支付方式:支付宝支付");//然后多个ADD语句及SET语句
LODOP.ADD_PRINT_TEXT(195,5,220,20,"打印时间:" + nowDate.getFullYear() + "-" + (nowDate.getMonth() + 1) + "-" + nowDate.getDate() + " " + nowDate.getHours() + ":" + nowDate.getMinutes() + ":" + nowDate.getSeconds());//然后多个ADD语句及SET语句
// ADD_PRINT_TEXT(intTop,intLeft,intWidth,intHeight,strContent)增加纯文本项
LODOP.ADD_PRINT_IMAGE(220,50,150,150,"<img border='0' src='../../resource/images/xxx.jpg' width='150' height='150'/>"); //打印二维码
LODOP.SET_PRINT_STYLEA(0,"Stretch",2);//按原图比例(不变形)缩放模式
LODOP.PREVIEW(); //打印预览
// LODOP.PRINT(); //最后一个打印(或预览、维护、设计)语句
}
在JS文件写上面的代码即可调用打印机。这里用到的插件就是Lodop。需要从官网下载类似驱动(我是这样的认为的)软件安装之后,然后需要引入它的JS文件,<script src="js/LodopFuncs.js?v=<%=new Date()%>"></script>
就这么简单,在哪都可以打印了。具体请见官网参考实例:官网样例