-1

ill try make this as simple as possible

im trying to print a bill that contains this:

String text="Home\n";
       text+="Sweet\n"+"Home";
String cut_page="((char)12)m"; //this string is just a reference

i want to print to a printer the string "text" and immediately after print "cut_page".

i want to do it in this order because concatenating havent worked for me, because for some reason it cuts in the middle of the phrase.


bellow if the real method im using, if whats above is enough for you then ignore this part

String param = request.getParameter("empresa");
    System.out.println("empresa");
    System.out.println(param);
    Gson gson = new Gson();
    String corte="m";
    DetalleImpresion detalle = gson.fromJson(param, DetalleImpresion.class);
    System.out.println(detalle);
    ArrayList<HashMap<String, Object>> detalles_factura = detalle.getDetalle_factura();

    String total = detalle.total;
    String direccion = detalle.direccion;
    String fecha = detalle.fecha;
    String facturacli = detalle.factura;
    String cliente = detalle.cliente;
    String empresa = detalle.empresa;
    String clienteNombre = detalle.cliente_nombre;
    String guia = detalle.guia_remision;

    String factura = empresa+"\t"+facturacli+"\n"+"\t"+fecha+"\n";
            factura+="\t"+clienteNombre+"\tRUC\n"+"\t"+direccion+"\n"+guia+"\n";//+"=======================================\n";

    for(int i=0; i< detalles_factura.size();i++){
        HashMap<String, Object> row = detalles_factura.get(i);      
        factura+=row.get("cantidad").toString() +"\t" + row.get("producto_nombre").toString()+
                "\t"+row.get("precio_unitario").toString()+"\t"+row.get("a_pagar").toString()+"\n";                 
    }   

    StringBuilder facfinal = new StringBuilder();
    facfinal.append(factura);
    facfinal.append(corte);
    String ff=facfinal.toString();
    //factura+=corte;

    String printerName = request.getParameter("dispositivo");
    System.out.println(printerName);
    AttributeSet attributeSet = new HashAttributeSet();
    attributeSet.add(new PrinterName(printerName, null));
    attributeSet = new HashAttributeSet();
    attributeSet.add(ColorSupported.NOT_SUPPORTED);
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, attributeSet);
    int indexOfEmpsonPrinter = -1;

    for( int i = 0 , count = services.length ;  i < count ; ++i ){
        System.out.println(services[i].getName());
        if( services[i].getName().equals(printerName) ){
            indexOfEmpsonPrinter = i;

        }

    }

    if(indexOfEmpsonPrinter!=-1){

        InputStream is = new ByteArrayInputStream(ff.getBytes("UTF8"));
        InputStream is2 = new ByteArrayInputStream(factura.getBytes("UTF8"));
        PrintRequestAttributeSet  pras = new HashPrintRequestAttributeSet();
        pras.add(new Copies(2));


        Doc doc = new SimpleDoc(is, flavor, null);
        Doc doc2 = new SimpleDoc(is2, flavor, null);
        DocPrintJob job = services[indexOfEmpsonPrinter].createPrintJob();

        try {
            job.print(doc, pras);
        } catch (PrintException e) {
            e.printStackTrace();
        }

        is.close();
        is2.close();
    }
    else{
        System.out.println("Impresora no encontrada");
    }
  • 1
    Please show only the relevant part. The whole business logic you posted makes it harder to isolate the problem, and help you. – Balázs Édes May 20 '14 at 17:32

1 Answers1

0

From the DocFlavor API:

A Java Print Service instance is not required to support the following print data formats and print data representation classes. In fact, a developer using this class should never assume that a particular print service supports the document types corresponding to these pre-defined doc flavors.

Java is probably autosensing the "text/plain" doc flavor from your print job. Most printers that I have encountered have only rudimentary support for this.

In your case, I suspect the problem is that the printer wants a "\r\n" endline rather than Java's "\n". But more generally, you will have much better control over the results if you actually implement your own Printable object.

Russell Zahniser
  • 16,188
  • 39
  • 30