I have problem with setting up the margins or printable area width, I'm not sure, for my Epson printers. I tried everything, setting up drivers, and everything.
I'm using two printers EPSON TM-T20II Receipt, and EPSON TM-H6000II Receipt and have problem with both of them. Test pages are printed correctly, but my data just won't spread throught page.
This is the code I'm using.
public class Printing implements Printable {
private String stringToPrint;
public Printing(String stringToPrint) {
this.stringToPrint = stringToPrint;
}
@Override
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
if (pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
}
Paper paper = new Paper();
double margin = 0;
paper.setImageableArea(margin, margin, 612.0, 792.0);
pf.setPaper(paper);
g.setColor(Color.black);
g.setFont(new Font(Font.MONOSPACED, Font.BOLD, 13));
g.translate(0, 0);
String printers = paper.getImageableHeight() + " " + paper.getImageableWidth() + "---" + pf.getHeight() + " "
+ pf.getWidth();
Writer fw = null;
try {
fw = new OutputStreamWriter(new FileOutputStream("printingData.txt"), StandardCharsets.UTF_8);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter writer = new PrintWriter(bw);
writer.print(printers);
writer.close();
int x = 0;
int y = 100;
for (String line : stringToPrint.split("\n")) {
g.drawString(line, x, y += g.getFontMetrics().getHeight());
}
return Printable.PAGE_EXISTS;
}
public static void printer(String printerData, Boolean bill, String designatedPrinter)
throws IOException, PrinterException {
try {
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
PrintService designatedService = null;
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
AttributeSet aset = new HashAttributeSet();
aset = new HashAttributeSet();
aset.add(ColorSupported.NOT_SUPPORTED);
String printers = "";
for (int i = 0; i < printServices.length; i++) {
printers += " service found " + printServices[i].getName() + "\n";
}
for (int i = 0; i < printServices.length; i++) {
System.out.println(" service found " + printServices[i].getName());
if (printServices[i].getName().equalsIgnoreCase(designatedPrinter)) {
System.out.println("I want this one: " + printServices[i].getName());
designatedService = printServices[i];
break;
}
}
Writer fw = new OutputStreamWriter(new FileOutputStream("printing.txt"), StandardCharsets.UTF_8);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter writer = new PrintWriter(bw);
writer.print(printers);
writer.close();
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintService(designatedService);
Printable painter;
// Specify the painter
painter = new Printing(printerData);
pj.setPrintable(painter);
pj.print();
} catch (PrinterException e) {
Writer fw = new OutputStreamWriter(new FileOutputStream("log.txt", true), StandardCharsets.UTF_8);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter writer = new PrintWriter(bw);
e.printStackTrace(writer);
writer.close();
}
}
}
Problem is that you are creating new page but not adding it to the PrinterJob
You add this bellow comment.
// Specify the painter
painter = new Printing(printerData);
pj.setPrintable(painter);
PageFormat pf0 = pj.defaultPage();
Paper p = pf0.getPaper();
p.setImageableArea(0, 0, pf0.getWidth(), pf0.getHeight());
pf0.setPaper(p);
pj.validatePage(pf0);
Book book = new Book();
book.append(painter, pj.validatePage(pf0));
pj.setPageable(book);
pj.print();
You don't need it in print
function.