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");
}