0

I've a problem printing using java classes. Everytime my file is found(the path is right), it is succesfully sent to my printer queue, but it disappears after few moments without errors and without printing. This is my code, thanks in advance.

    private void print(String path){

   FileInputStream psStream = null;
    try {
        psStream = new FileInputStream(path);
        } catch (FileNotFoundException ffne) {
          ffne.printStackTrace();
        }
        if (psStream == null) {
            return;
        }


    DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
    Doc myDoc = new SimpleDoc(psStream, psInFormat, null);  
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();    
    PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);

    PrintService myPrinter = null;
    for (int i = 0; i < services.length; i++){
        String svcName = services[i].toString();    
        System.out.println("service found: "+svcName);

        if (svcName.contains(PrintServiceLookup.lookupDefaultPrintService().getName())){
            myPrinter = services[i];
            System.out.println("my printer found: "+svcName);
            break;
        }
    }

    if (myPrinter != null) {            
        DocPrintJob job = myPrinter.createPrintJob();
        try {
            job.print(myDoc, aset);
        } catch (Exception pe) {
            pe.getMessage();}
    } else {
        System.out.println("no printer services found");
    }
}
fabrom
  • 61
  • 4
  • Have you tried a different printer than the default one? Preferably one that will create a file (e.g. PDF) instead of creating a hard copy? – hotzst Oct 07 '15 at 13:36
  • I only have a physical Canon MP230 series, so I've set code on "default printer" trying to print with it. I've also tried printing with both pdf and txt using Microsoft XPS Writer and it writes a ~14kb file not readable with XPS Viewer. – fabrom Oct 07 '15 at 13:46

0 Answers0