Java code not able to print long receipt in thermal printer

2.1k views Asked by At

My java thermal printer code not able to print long receipt(more than A4 sheet size). Its work fine normally, but in case where there is too much items in cart then it generate half print. My code is under mentioned-

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;
}

Please let me know how can i generate long receipt print by correcting my code or if you have any better solution to do this.

2

There are 2 answers

2
CraigR8806 On

Right here:

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

You are creating a new Paper object and not setting its height.

Here is a link to the documentation of this class.

When creating a Paper object, it is the application's responsibility to ensure that the paper size and the imageable area are compatible

You need to set the height of the paper by calling paper.setSize(width, height) or it will use its default size property.

The dimensions are supplied in 1/72nds of an inch.

So both width and height will need to be provided in this format as doubles

2
Alritz Escorido On
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
                PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();

                PrintService printService[] = PrintServiceLookup.lookupPrintServices(
                        flavor, pras);
                PrintService service = findPrintService(printerName, printService);
PDDocument document = PDDocument.load(bytes);

                PrinterJob job = PrinterJob.getPrinterJob();

                job.setPrintService(service);

                job.setPageable(new PDFPageable(document));
 job.print();


                if (document != null) {
                    document.close();
                }