admin
3 天以前 6e4a6789e948bc6a8dc9fc1aa3eebc2980be3072
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
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;
        }
    }
}