2

Hey there im making an Invoice software Everything is fine just the problem is when my report is printing this is printed according to the standard printer but i want to print it through billing receipt printer

 @Override
   public int print(Graphics g, PageFormat format, int page_index) 
    throws PrinterException {
if (page_index > 0) {
    return Printable.NO_SUCH_PAGE;
}

// get the bounds of the component
Dimension dim = comp.getSize();
double cHeight = dim.getHeight();
double cWidth = dim.getWidth();

// get the bounds of the printable area
double pHeight = format.getImageableHeight();
double pWidth = format.getImageableWidth();

double pXStart = format.getImageableX();
double pYStart = format.getImageableY();

double xRatio = pWidth / cWidth;
double yRatio = pHeight / cHeight;


Graphics2D g2 = (Graphics2D) g;
g2.translate(pXStart, pYStart);
g2.scale(xRatio, yRatio);
comp.paint(g2);

return Printable.PAGE_EXISTS;
}




 public static void printing(){









  PrinterJob pjob = PrinterJob.getPrinterJob();
  PageFormat preformat = pjob.defaultPage();
  preformat.setOrientation(PageFormat.PORTRAIT);
  PageFormat postformat = pjob.defaultPage();
 //If user does not hit cancel then print.
 if (preformat != postformat) {
  //Set print component
   pjob.setPrintable(new reporting(frame), postformat);
  //  if (pjob.printDialog()) {
   try {
    pjob.print();
    frame.dispose();
    } catch (PrinterException ex) {
    Logger.getLogger(reporting.class.getName()).log(Level.SEVERE, null, ex);
   }
 //}
//}
}

} 

This code is printing my reciept but this is printing according to the standard printer and as i want to print it through billing printer and im not using jesper report to print it im jsut directly printing my jframe help? thankx in advance

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Numan Pathan
  • 149
  • 7
  • There are a number of ways you can do this. If you know the size of the receipt printer paper, you can use something like [this example](https://stackoverflow.com/questions/11803741/printing-in-java-to-label-printer/11805237#11805237) to change the paper size – MadProgrammer Sep 16 '17 at 23:41
  • [This is another example](https://stackoverflow.com/questions/28427566/save-jpanel-as-image-hd-quality/28492187#28492187) on the same idea and [so is this example](https://stackoverflow.com/questions/27029166/java-printerjob-not-printing-to-fit-paper/27029220#27029220) - The problem I "think" you might have is, is knowing the "height" of page, if it's a thermal style printer which just has a roll – MadProgrammer Sep 16 '17 at 23:41
  • If you're using a Thermal Printer, you might want to [have a look at these other questions](https://stackoverflow.com/search?q=%5Bjava%5D+thermal+printer) as some (that I'm aware of), have their own printer APIs – MadProgrammer Sep 16 '17 at 23:43

1 Answers1

0

If you are looking for the user to specify the printer via an input, I'd try using the System print dialogs, more details here:

https://docs.oracle.com/javase/tutorial/2d/printing/dialog.html

But essentially adding around your try block:

if (pj.printDialog()) 

If what you're looking for is a more programmatic way of sending the job to a specified printer and you know the name of the printer ahead of time you'd look to use a print service. Perhaps the following SO question may be of use.

  • I read all answers but most usefull i found is i think pj.printdialog() . But one thing is still un cleared that how to say my printer to print adjust at paper roll bcz im printing at paper roll and paper size is letter there is no any paper roll or such kind of drop down such as paper roll so i need to knw this how to print adjust according to the paper roll – Numan Pathan Sep 17 '17 at 07:05