package com.billion.main.bs.controller;
|
|
import java.awt.print.PageFormat;
|
import java.awt.print.Paper;
|
import java.awt.print.PrinterException;
|
import java.awt.print.PrinterJob;
|
import java.util.List;
|
import javax.print.PrintService;
|
import javax.print.PrintServiceLookup;
|
import javax.servlet.http.HttpServletResponse;
|
|
import com.google.zxing.common.BitMatrix;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PutMapping;
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
import com.billion.common.annotation.Log;
|
import com.billion.common.core.controller.BaseController;
|
import com.billion.common.core.domain.AjaxResult;
|
import com.billion.common.enums.BusinessType;
|
import com.billion.main.bs.domain.BsRouteInfo;
|
import com.billion.main.bs.service.IBsRouteInfoService;
|
import com.billion.common.utils.poi.ExcelUtil;
|
import com.billion.common.core.page.TableDataInfo;
|
import java.awt.image.BufferedImage;
|
import java.awt.Graphics;
|
import java.awt.Graphics2D;
|
import java.awt.RenderingHints;
|
import java.awt.Font;
|
import com.google.zxing.BarcodeFormat;
|
import com.google.zxing.qrcode.QRCodeWriter;
|
import java.io.ByteArrayOutputStream;
|
import javax.swing.JFrame;
|
import javax.swing.JPanel;
|
import javax.swing.JScrollPane;
|
import javax.swing.JButton;
|
import java.awt.Dimension;
|
import java.awt.BorderLayout;
|
import java.awt.Color;
|
|
/**
|
* 工艺路线Controller
|
*
|
* @author Billion-Yi
|
* @date 2024-11-23
|
*/
|
@RestController
|
@RequestMapping("/bs/routeInfo")
|
public class BsRouteInfoController extends BaseController
|
{
|
@Autowired
|
private IBsRouteInfoService bsRouteInfoService;
|
|
/**
|
* 查询工艺路线列表
|
*/
|
@PreAuthorize("@ss.hasPermi('bs:routeInfo:list')")
|
@GetMapping("/list")
|
public TableDataInfo list(BsRouteInfo bsRouteInfo)
|
{
|
startPage();
|
List<BsRouteInfo> list = bsRouteInfoService.selectBsRouteInfoList(bsRouteInfo);
|
return getDataTable(list);
|
}
|
|
/**
|
* 导出工艺路线列表
|
*/
|
@PreAuthorize("@ss.hasPermi('bs:routeInfo:export')")
|
@Log(title = "工艺路线", businessType = BusinessType.EXPORT)
|
@PostMapping("/export")
|
public void export(HttpServletResponse response, BsRouteInfo bsRouteInfo)
|
{
|
List<BsRouteInfo> list = bsRouteInfoService.selectBsRouteInfoList(bsRouteInfo);
|
ExcelUtil<BsRouteInfo> util = new ExcelUtil<BsRouteInfo>(BsRouteInfo.class);
|
util.exportExcel(response, list, "工艺路线数据");
|
}
|
|
/**
|
* 获取工艺路线详细信息
|
*/
|
@PreAuthorize("@ss.hasPermi('bs:routeInfo:query')")
|
@GetMapping(value = "/{id}")
|
public AjaxResult getInfo(@PathVariable("id") Long id)
|
{
|
return success(bsRouteInfoService.selectBsRouteInfoById(id));
|
}
|
|
/**
|
* 新增工艺路线
|
*/
|
@PreAuthorize("@ss.hasPermi('bs:routeInfo:add')")
|
@Log(title = "工艺路线", businessType = BusinessType.INSERT)
|
@PostMapping
|
public AjaxResult add(@RequestBody BsRouteInfo bsRouteInfo)
|
{
|
return toAjax(bsRouteInfoService.insertBsRouteInfo(bsRouteInfo));
|
}
|
|
/**
|
* 修改工艺路线
|
*/
|
@PreAuthorize("@ss.hasPermi('bs:routeInfo:edit')")
|
@Log(title = "工艺路线", businessType = BusinessType.UPDATE)
|
@PutMapping
|
public AjaxResult edit(@RequestBody BsRouteInfo bsRouteInfo)
|
{
|
return toAjax(bsRouteInfoService.updateBsRouteInfo(bsRouteInfo));
|
}
|
|
/**
|
* 删除工艺路线
|
*/
|
@PreAuthorize("@ss.hasPermi('bs:routeInfo:remove')")
|
@Log(title = "工艺路线", businessType = BusinessType.DELETE)
|
@DeleteMapping("/{ids}")
|
public AjaxResult remove(@PathVariable Long[] ids)
|
{
|
return toAjax(bsRouteInfoService.deleteBsRouteInfoByIds(ids));
|
}
|
|
|
/**
|
* 预览打印内容
|
* @param content 需要打印的内容
|
* @param paperSize 纸张大小
|
* @param orientation 纸张方向
|
*/
|
public static void previewPrint(String content, String paperSize, boolean orientation) {
|
JFrame frame = new JFrame("打印预览");
|
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
// 创建预览面板
|
JPanel previewPanel = new JPanel() {
|
@Override
|
protected void paintComponent(Graphics g) {
|
super.paintComponent(g);
|
Graphics2D g2d = (Graphics2D) g;
|
|
// 设置打印质量
|
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
|
// 绘制纸张边框
|
g2d.setColor(Color.WHITE);
|
g2d.fillRect(0, 0, getWidth(), getHeight());
|
g2d.setColor(Color.BLACK);
|
g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
|
|
// 使用CustomPrintable绘制内容
|
CustomPrintable printable = new CustomPrintable(content);
|
try {
|
PageFormat pf = new PageFormat();
|
Paper paper = new Paper();
|
configurePaper(paper, paperSize);
|
pf.setPaper(paper);
|
if (orientation) {
|
pf.setOrientation(PageFormat.LANDSCAPE);
|
}
|
|
// 缩放到预览窗口大小
|
double scale = Math.min(
|
(double) getWidth() / paper.getWidth(),
|
(double) getHeight() / paper.getHeight()
|
);
|
g2d.scale(scale, scale);
|
|
printable.print(g2d, pf, 0);
|
} catch (PrinterException e) {
|
e.printStackTrace();
|
}
|
}
|
};
|
|
// 设置预览面板大小
|
Dimension preferredSize = new Dimension(595, 842); // A4纸默认大小
|
previewPanel.setPreferredSize(preferredSize);
|
|
// 创建滚动面板
|
JScrollPane scrollPane = new JScrollPane(previewPanel);
|
scrollPane.setPreferredSize(new Dimension(650, 900));
|
|
// 添加打印按钮
|
JButton printButton = new JButton("打印");
|
printButton.addActionListener(e -> {
|
try {
|
frame.dispose();
|
printWithQRCode(content, paperSize, orientation);
|
} catch (PrinterException ex) {
|
ex.printStackTrace();
|
}
|
});
|
|
// 布局
|
JPanel mainPanel = new JPanel(new BorderLayout());
|
mainPanel.add(scrollPane, BorderLayout.CENTER);
|
mainPanel.add(printButton, BorderLayout.SOUTH);
|
|
frame.add(mainPanel);
|
frame.pack();
|
frame.setLocationRelativeTo(null);
|
frame.setVisible(true);
|
}
|
|
/**
|
* 配置纸张大小
|
*/
|
private static void configurePaper(Paper paper, String paperSize) {
|
switch (paperSize.toUpperCase()) {
|
case "A4":
|
paper.setSize(595, 842);
|
paper.setImageableArea(36, 36, 523, 770);
|
break;
|
case "A5":
|
paper.setSize(420, 595);
|
paper.setImageableArea(36, 36, 348, 523);
|
break;
|
case "B5":
|
paper.setSize(516, 729);
|
paper.setImageableArea(36, 36, 444, 657);
|
break;
|
default:
|
paper.setSize(595, 842);
|
paper.setImageableArea(36, 36, 523, 770);
|
}
|
}
|
|
// 修改main方法用于测试
|
public static void main(String[] args) {
|
// 先预览
|
previewPrint("这是测试内容\r\n第二行内容\n第三行内容", "A4", false);
|
}
|
|
|
/**
|
* 打印文档并生成二维码
|
* @param content 需要生成二维码的内容
|
* @param paperSize 纸张大小 例如:A4, A5, B5等
|
* @param orientation 纸张方向 true为横向,false为纵向
|
*/
|
public static void printWithQRCode(String content, String paperSize, boolean orientation) throws PrinterException {
|
PrinterJob job = PrinterJob.getPrinterJob();
|
|
// 查找打印机
|
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
|
PrintService printService = null;
|
for (PrintService service : services) {
|
if (service.getName().equals("Kyocera ECOSYS M4125idn KX")) {
|
printService = service;
|
break;
|
}
|
}
|
|
if (printService == null) {
|
System.out.println("未找到指定打印机");
|
return;
|
}
|
|
job.setPrintService(printService);
|
|
// 设置纸张大小和方向
|
PageFormat pf = job.defaultPage();
|
Paper paper = new Paper();
|
|
// 根据纸张类型设置尺寸(单位:1/72英寸)
|
switch (paperSize.toUpperCase()) {
|
case "A4":
|
paper.setSize(595, 842); // A4: 210mm x 297mm
|
paper.setImageableArea(36, 36, 523, 770);
|
break;
|
case "A5":
|
paper.setSize(420, 595); // A5: 148mm x 210mm
|
paper.setImageableArea(36, 36, 348, 523);
|
break;
|
case "B5":
|
paper.setSize(516, 729); // B5: 182mm x 257mm
|
paper.setImageableArea(36, 36, 444, 657);
|
break;
|
default:
|
System.out.println("不支持的纸张类型,使用默认A4");
|
paper.setSize(595, 842);
|
paper.setImageableArea(36, 36, 523, 770);
|
}
|
|
// 设置纸张方向
|
if (orientation) {
|
pf.setOrientation(PageFormat.LANDSCAPE);
|
} else {
|
pf.setOrientation(PageFormat.PORTRAIT);
|
}
|
|
pf.setPaper(paper);
|
job.setPrintable(new CustomPrintable(content), pf);
|
|
if (job.printDialog()) {
|
job.print();
|
}
|
}
|
|
/**
|
* 自定义打印内容类
|
*/
|
static class CustomPrintable implements java.awt.print.Printable {
|
private String content;
|
private BufferedImage qrCodeImage;
|
|
public CustomPrintable(String content) {
|
this.content = content;
|
this.qrCodeImage = generateQRCode(content);
|
}
|
|
/**
|
* 生成二维码
|
* @param content 二维码内容
|
* @return 二维码图片
|
*/
|
private BufferedImage generateQRCode(String content) {
|
try {
|
// 使用ZXing库生成二维码
|
QRCodeWriter qrCodeWriter = new QRCodeWriter();
|
BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, 200, 200);
|
|
BufferedImage qrImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
|
for (int x = 0; x < 200; x++) {
|
for (int y = 0; y < 200; y++) {
|
qrImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
|
}
|
}
|
return qrImage;
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
@Override
|
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
|
if (pageIndex > 0) {
|
return NO_SUCH_PAGE;
|
}
|
|
Graphics2D g2d = (Graphics2D) g;
|
g2d.translate(pf.getImageableX(), pf.getImageableY());
|
|
// 设置打印质量
|
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
|
// 页面布局参数(可根据需要调整)
|
int currentY = 50; // 起始Y坐标
|
int leftMargin = 50; // 左边距
|
|
// 绘制标题
|
g2d.setFont(new Font("宋体", Font.BOLD, 20));
|
g2d.drawString("打印测试文档", leftMargin, currentY);
|
|
// 绘制内容
|
currentY += 50;
|
g2d.setFont(new Font("宋体", Font.PLAIN, 12));
|
g2d.drawString("yyyyyyyyy", leftMargin, currentY);
|
currentY += 100;
|
g2d.drawString("XXXXXXXXX", leftMargin, currentY);
|
|
// 绘制二维码
|
currentY += 200;
|
if (qrCodeImage != null) {
|
currentY += 30;
|
g2d.drawImage(qrCodeImage, leftMargin, currentY, null);
|
}
|
|
return PAGE_EXISTS;
|
}
|
}
|
}
|