0

Here I have to work on pageIndex but not getting how to set it, so that I could print the long receipt whose size is more than A4 paper height in POS-Printer(paper roll)

    public int print(Graphics g, PageFormat pf, int pageIndex) {
    PrinterJob pj = PrinterJob.getPrinterJob();
    pf1 = pj.getPageFormat(attr_set);
    if (pageIndex != 0) {
        return NO_SUCH_PAGE;
    }
    attr_set.add(res);
    g2 = (Graphics2D) g;
    //initialising X & Y coordinates
    int x_header = 20;
    int y_header = 0;

    Easy.log("In Print method--------------------------------------");

    //call the method which will print header
    if (printData == 1) {  //this will print KOT
        pageWidth = (float) pf1.getImageableWidth();
        y_header=printKOTDetails(x_header,y_header);
    }
     return PAGE_EXISTS;
    }

Configuration method is:

    private void printerConfiguration() throws PrinterException {

    PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
    PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
    Media res = (Media) defaultPrintService.getDefaultAttributeValue(Media.class);
    PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
    attr_set.add(res);

    PrinterJob pj = PrinterJob.getPrinterJob();
    pf1 = pj.getPageFormat(attr_set);
    pj.setPrintService(printService);

    Paper paper = new Paper();
    double margin = 2;

    paper.setImageableArea(margin, 0, paper.getImageableWidth(), paper.getImageableHeight());
    pf1.setPaper(paper);

    pj.setPrintable(this, pf1);
    pj.print();
}
AshwinK
  • 1,039
  • 1
  • 12
  • 31
  • Rather the relying on the `pageIndex`, maintain information about how much you have printed and when you run out of stuff to print, send back `NO_SUCH_PAGE` ... but remember, you might be asked to print a page multiple times – MadProgrammer Jun 18 '15 at 05:45
  • Further, you should be using `pageIndex` to make decisions about where you are up to with what you are printing – MadProgrammer Jun 18 '15 at 05:49
  • @MadProgrammer But the pos printer driver took care of that. Like, if only 4 lines are printed it will print only 4lines & then tear the page. So any other way than this? Can't it be done using pageIndex ? – AshwinK Jun 18 '15 at 05:55
  • Not really, that I can think off. You could try setting the page height to what ever length you need. But remember, the print API tends to work at 72dpi – MadProgrammer Jun 18 '15 at 05:57
  • Okie. Will go by that way then. Good way to calculate how much have printed ? – AshwinK Jun 18 '15 at 05:59
  • That's kind of tricky, but essentially, you need to know the `FontMetrics` so you know the text height and how many lines in you might be printing (including spacing etc). I'd start with a `BufferedImage`, set it's size to `1x1` and use it's `Graphics` context, this "should" give you a starting point. – MadProgrammer Jun 18 '15 at 06:03
  • Sounds good. Will go with `fontMetrics` as I am already implementing it. – AshwinK Jun 18 '15 at 06:05

0 Answers0