just for your information,
I also faced similar problems with printing in java.
And I found out following ways
1) You can create a html file, and render it on the printer
as it renders on the browser using
DocFlavor flavor = DocFlavor.STRING.TEXT_HTML;
See this Printing in java
2) You can create Image file which consists of LOGO and billing info
as a one single image and send it to the printer. I am using this
method.
Here is how
public static void createImageFromText(String text, String filename) {
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
int width = 0;
int height = 0;
String[] lines = StringUtils.split(text, System.getProperty("line.separator"));
Graphics2D g2d = img.createGraphics();
Font font = new Font("Arial", Font.PLAIN, 48);
g2d.setFont(font);
FontMetrics fm = g2d.getFontMetrics();
for (String line : lines) {
width = Math.max(fm.stringWidth(line), width);
height += fm.getHeight();
}
g2d.dispose();
width += 100;
height += 100;
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
g2d = img.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setFont(font);
fm = g2d.getFontMetrics();
g2d.setColor(Color.BLACK);
int y = 100;
for (String line : lines) {
g2d.drawString(line, 0, y += fm.getAscent());
}
g2d.dispose();
try {
ImageIO.write(img, "png", new File(filename));
} catch (IOException ex) {
ex.printStackTrace();
}
}
3) Search your printer maker provides any APIs to directly communicate with
printer in java. In case Star Printer, they publish Java,C,C++ APIs
4) this might be odd. you can also send contents on any swing component
to a printer using its graphics method.see this
How can I print a single JPanel's contents?