Java代码无法在热敏打印机

时间:2016-12-14 15:36:46

标签: java printing thermal-printer

我的java热敏打印机代码无法打印长收据(超过A4纸张尺寸)。它的工作正常,但如果购物车中的物品太多,它会产生一半的印刷品。我的代码未提及 -

public PrintReceipt(Map<String,String> hm){

         /* 
           product details code..


         */


        try{


            input = new FileInputStream("C:/printer.properties");
            prop.load(input);
            printerName=prop.getProperty("receiptPrinter");
            System.out.println("Printer Name "+printerName);

                }catch(Exception exception){
                    System.out.println("Properties file not found");
                }

            PrintService[] pservices = PrintServiceLookup.lookupPrintServices(null,null);

           for (int i = 0; i < pservices.length; i++) {
            if (pservices[i].getName().equalsIgnoreCase(printerName)) {
           job = PrinterJob.getPrinterJob();
           PageFormat pf = job.defaultPage();

           double margin = 1.0D;
           Paper paper = new Paper();
           paper.setSize(216D, paper.getHeight());
           paper.setImageableArea(margin, margin, paper.getWidth() - margin * 1.5D, paper.getHeight() - margin * 1.5D);
           pf.setPaper(paper);        
           job.setCopies(1);
           pf.setOrientation(1);
           job.setPrintable(this, pf);

           try
           {
               job.print();
           }
           catch(PrinterException ex)
           {
            System.out.println("Printing failed");

        }
        }
    }
}



public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
        throws PrinterException {
    if(pageIndex > 0)
        return 1;


            Graphics2D g2d = (Graphics2D)graphics;


            double width = pageFormat.getImageableWidth();
            double height = pageFormat.getImageableHeight();    
            g2d.translate((int) pageFormat.getImageableX(),(int) pageFormat.getImageableY()); 
            Font font = new Font("Monospaced",Font.BOLD,8);       
            g2d.setFont(font);

            try {
                /*
                         * Draw Image*


                         */
                                    int x=50 ;                                     
                                    int y=10;                                      
                                    int imagewidth=100;
                                    int imageheight=50;
                          BufferedImage read = ImageIO.read(new File("C:/hotel.png"));
                          g2d.drawImage(read,x,y,imagewidth,imageheight,null);         //draw image
                          g2d.drawString("-- * Resturant  * --", 20,y+60); 
                          g2d.drawLine(10, y+70, 180, y+70);                          //draw line
                                 } catch (IOException e) {
                        e.printStackTrace();
                    }
            try{

                 /*Draw Header*/


         /* 
           product details code..


         */


           /*Footer*/


          //end of the receipt



          }
            catch(Exception r){
              r.printStackTrace();
            }


    return 0;
}

请告诉我如何通过更正我的代码或者如果您有更好的解决方案来生成长收据打印件。

1 个答案:

答案 0 :(得分:1)

就在这里:

Paper paper = new Paper();
paper.setSize(216D, paper.getHeight());

您正在创建新的Paper对象,而不是设置其高度。

以下是此课程文档的link

  

创建Paper对象时,应用程序有责任确保纸张尺​​寸和可成像区域兼容

您需要通过调用paper.setSize(width, height)来设置纸张的高度,否则它将使用其默认大小属性。

  

尺寸以1/72英寸提供。

因此,需要以double s

格式提供宽度和高度