java 調收銀機打印小票


最近要做一個swing的小程序監聽收銀機的數據並打印在這記錄一下!

public class MyPrint implements Printable{
    
    static Logger log = Logger.getLogger(MyPrint.class); 
    
    //業務類
    UserService userService = new UserServiceImpl();
    PayInfoService payInfoService = new PayInfoServiceImpl();
 
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        if (pageIndex > 0) {
            return NO_SUCH_PAGE;
        }

        LoginUser loginUser = userService.checkLoginUser();
        
        Graphics2D graphics2d = (Graphics2D) graphics;
        //設置字體
        graphics2d.setFont(new Font("宋體", Font.PLAIN,8));
        graphics2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        //打印格式
        graphics2d.drawString("登入時間:"+loginUser.getLoginDate(), 15, 10); 
        graphics2d.drawString("登出時間:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), 15, 25); 
        graphics2d.drawString("收銀員:"+loginUser.getUserName(), 15, 45);
        graphics2d.drawString("收銀員編號:"+loginUser.getLoginName(), 15, 60);
        graphics2d.drawString("------------------------------------------", 15, 75); 
        graphics2d.drawString("總單數:"+payInfoService.countPay(0)+"", 15, 90); 
        graphics2d.drawString("當班總收入(元):"+payInfoService.sumMoeny(0)+"", 15, 105);
        graphics2d.drawString("聚合支付單數:"+payInfoService.countPay(3)+"" , 15, 125); 
        graphics2d.drawString("聚合支付收款(元):"+payInfoService.sumMoeny(3)+"", 15, 140);
        graphics2d.drawString("微信單數:"+payInfoService.countPay(1)+"" , 15, 160); 
        graphics2d.drawString("微信收款(元):"+payInfoService.sumMoeny(1)+"", 15, 175);
        graphics2d.drawString("支付寶單數:"+payInfoService.countPay(2)+"", 15, 195); 
        graphics2d.drawString("支付寶收款(元):"+payInfoService.sumMoeny(2)+"", 15, 210);
        graphics2d.drawString("POS機單數:"+payInfoService.countPay(5)+"", 15, 230); 
        graphics2d.drawString("POS機收款(元):"+payInfoService.sumMoeny(5)+"" , 15, 245);
        graphics2d.drawString("加油卡單數:"+payInfoService.countPay(4)+"" , 15, 265); 
        graphics2d.drawString("加油卡收款(元):"+payInfoService.sumMoeny(4)+"", 15, 280); 
        graphics2d.drawString("------------------------------------------", 15, 295); 
        return PAGE_EXISTS;    
    }
    
    public static void printWordDayInfo() {
        try {
            
            Book book = new Book(); 
            PageFormat pf = new PageFormat();    
            
            pf.setOrientation(PageFormat.PORTRAIT);     
            Paper p = new Paper(); 
            //設置打印紙的大小一般是158,10000隨便設的因為這是一卷紙不像A4紙是規定的
            p.setSize(158,10000); 
            //打印區域
            p.setImageableArea(0,0, 158,10000); 
            pf.setPaper(p);   
            book.append(new MyPrint(), pf);  
           
            PrinterJob job = PrinterJob.getPrinterJob(); 
            job.setPageable(book); 
            job.print(); 
        } catch (Exception e) {
            e.printStackTrace();
            log.error("打印異常:"+e.getMessage());
        }
    }
 

}

打印效果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM