1

what I want to do is to print a bill with a logo on the top left

I can print the image like this:

public class PrintLogo {

    public static void main(String args[]) throws Exception {

        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        pras.add(new Copies(0));


        PrintService pss = PrintServiceLookup.lookupDefaultPrintService();

        System.out.println("Printing to " + pss);

        DocPrintJob job = pss.createPrintJob();

        FileInputStream fin = new FileInputStream("D:\\logo.gif");
        Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);

        job.print(doc, pras);

        fin.close();
    }
}


and to print the bill I'm using the "PrinterJob"

I couldn't find a way to print the bill with the logo

so any help please :)

Wojciech Wirzbicki
  • 3,887
  • 6
  • 36
  • 59
user3448506
  • 41
  • 1
  • 7
  • 1
    You need to scale the image to fit within the page, for [example](http://stackoverflow.com/questions/27273402/printing-a-1800-x-1200-image-on-4-x-6-paper-using-java/27356904#27356904) and [example](http://stackoverflow.com/questions/17904518/fit-scale-jcomponent-to-page-being-printed/17961911#17961911), – MadProgrammer Apr 02 '15 at 01:51

1 Answers1

1

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?

Community
  • 1
  • 1
  • I guess I'll follow your method, and if you can, please show me the code who generate the bill info with the logo as an image :) – user3448506 Apr 02 '15 at 02:27
  • ok. will share my code. just include it in your class and pass it billing info (mutliple lines) as a single string param. FYI, I am also using codehause MVEL template engine, to generate actual text from objects. If you want you can use or ignore it no problem. sharing the code by improving as the new-answer. please refer it. –  Apr 02 '15 at 03:15
  • FYI, you could maintain your receipt format as a text template and convert that into actual billing info by passing java objects using codehaus MVEL and then finally pass the resultant string to the method I shared. PNG receipt will be created. –  Apr 02 '15 at 03:23
  • Your code, creat the text as an image, but, i want to add an image as logo to a text and make all that a new image (image+text) to print it with the printer :) – user3448506 Apr 02 '15 at 15:08
  • Lets say that you have a logo image and billing template as a string. Steps, you pass your string template consisting of multiple lines as a param to above function. Why dont you try yourself adding logo image, before printing/adding all lines using drawString?. Use drawImage(Image, x,y) to print logo at top-left or wherever you want. Please see this ref: https://docs.oracle.com/javase/tutorial/2d/images/drawimage.html –  Apr 03 '15 at 07:46
  • FYI, I am also using Html2Image to display beautiful html receipt on webpage. When print button is clicked, i am converting that html into image and finally printing image as the same you did printing image file. Ref: https://code.google.com/p/java-html2image/ –  Apr 03 '15 at 07:50