Search code examples
javaprintinggifthermal-printer

Print gif using java on a 4x6" paper


What is the best way in Java to print a gif given as byte[] or ByteArrayInputStream on a paper with a size of 4x6 inches?

This:

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new MediaSize(4, 6, Size2DSyntax.INCH));
aset.add(new Copies(1));
PrintService[] pservices =
PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, aset);

DocPrintJob printJob = pservices[0].createPrintJob();
Doc doc = new SimpleDoc(sap.getGraphicImageBytes(), DocFlavor.INPUT_STREAM.GIF, null);
printJob.print(doc, aset);

does not work because the MediaSize is not a PrintRequestAttribute. This should be almost the same as in Package javax.print Description


Solution

  • I found a way to solve my problem

    PageFormat format = new PageFormat();
    format.setOrientation(PageFormat.REVERSE_LANDSCAPE);
    
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(OrientationRequested.REVERSE_LANDSCAPE);
    aset.add(MediaSizeName.JAPANESE_POSTCARD);
    
    PrinterJob printerJob = PrinterJob.getPrinterJob();
    printerJob.setPrintable(new ImagePrintable(sap.getGraphicImage()));
    printerJob.defaultPage(format);
    printerJob.print(aset);
    

    The trick was using japanese postcard as media size.