Impresion de tickets

ticketlost
27 de Octubre del 2005
Hola, la duda es la siguiente, tengo que mandar a imprimir unos tickets con codigo nativo de la impresora (LTX 2003), me pasaron ya el codigo que de java para hacerlo, lo que no se es como y donde voy a agregar dicho codigo, al presionar un boton de aceptar en una pagina debe de automaticamente mandarse a imprimir el ticket. Saludos y gracias

Milena
27 de Octubre del 2005
Podrias mostrarnos el codigo??

ticketlost
27 de Octubre del 2005
/*
* Created on 18/08/2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.neitek.wt.print;

import java.io.IOException;
import java.util.Properties;


/**
* @author jfdominguez
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class LTXSuiteTicketTemplateSinCode implements DocumentTemplate {

//-- Constants
private static final String CR = "rn";
private static final String PRINTER_PORT = "LPT1";
//-- LM stands for Landscape Mode
private static int LM_MAX_ROWS = 389;
private static int LM_MAX_COLUMNS = 750; // Equiv. de 3.5", donde está la línea de corte del boleto

//-- Columns for portrait mode, used to center text
private static int PM_MAX_COLUMNS = 389;

private static String NULL_DATA_SYMBOL = "--";

private static String[] TICKET_PARAMETERS =
{
"print.isDebug",
"template.type",
"venue.name",
"event.code",
"event.name",
"presentation.date",
"presentation.time",
"seat.section",
"seat.row",
"seat.number",
"seat.access.door",
"ticket.code",
"ticket.type",
"ticket.price",
};

//--- Properties
private String venueName = "venue default";
private String eventCode = "event code default";
private String eventName = "event name default";
private String presentationDate;
private String presentationTime;
private String seatSection;
private String seatRow;
private String seatNumber;
private String seatAccessDoor;
private String ticketCode;
private String ticketType;
private String ticketPrice;
private String packageCode;
private String packageName;
private String productCode;
private String productName;
private String templateType;
private String seatLabel;
private String parsedSeatZone;
private String parsedSeatNumber;
private String parsedSeatLabel;
private String parsedSeatSection;

/* (non-Javadoc)
* @see com.neitek.wt.print.DocumentTemplate#getParameterNames()
*/
public String[] getParameterNames() {
return TICKET_PARAMETERS;
}

/* (non-Javadoc)
* @see com.neitek.wt.print.DocumentTemplate#initialize(byte[], java.util.Properties)
*/
public void initialize(byte[] tempalte, Properties templateProperties)
throws IOException {


}

protected double transformTicketPrice (String ticketPrice)
{
double value = 0;
if (ticketPrice == null) return value;

ticketPrice = ticketPrice.trim();
if (ticketPrice.length() == 0) return value;
ticketPrice = ticketPrice.substring(1);


int pos = 0;
// quitamos las comas
while ( (pos = ticketPrice.indexOf(",")) > 0)
{
if (pos < ticketPrice.length() )
ticketPrice = ticketPrice.substring(0, pos)+ ticketPrice.substring(pos+1);
else
ticketPrice = ticketPrice.substring(0, ticketPrice.length()-1);
}

try
{
value = Double.parseDouble(ticketPrice);
}
catch (NumberFormatException e)
{ e.printStackTrace(); }


return value;
}


/* (non-Javadoc)
* @see com.neitek.wt.print.DocumentTemplate#printDocument(java.util.Properties)
*/
public void printDocument(Properties parameters)
throws IOException {

this.venueName = parameters.getProperty("venue.name");
this.eventCode = parameters.getProperty("event.code");
this.eventName = parameters.getProperty("event.name");
this.presentationDate = parameters.getProperty("presentation.date");
this.presentationTime = parameters.getProperty("presentation.time");
this.seatSection = parameters.getProperty("seat.section");
this.seatRow = parameters.getProperty("seat.row");
this.seatNumber = parameters.getProperty("seat.number");
this.seatAccessDoor = parameters.getProperty("seat.access.door");
this.ticketCode = parameters.getProperty("ticket.code");
this.ticketType = parameters.getProperty("ticket.type");
this.ticketPrice = parameters.getProperty("ticket.price");
this.packageCode = parameters.getProperty("package.code");
this.packageName = parameters.getProperty("package.name");
this.productCode = parameters.getProperty("product.code");
this.productName = parameters.getProperty("product.name");
this.templateType = parameters.getProperty("template.type");



if (presentationDate == null || "".equals(presentationDate))
{
presentationDate = NULL_DATA_SYMBOL;
}
if (presentationTime == null || "".equals(presentationTime))
{
presentationTime = NULL_DATA_SYMBOL;
}
if (seatSection == null || "".equals(seatSection))
{
seatSection = NULL_DATA_SYMBOL;
}
if (seatNumber == null || "".equals(seatNumber))
{
seatNumber = NULL_DATA_SYMBOL;
}
if (seatAccessDoor == null || "".equals(seatAccessDoor))
{
seatAccessDoor = NULL_DATA_SYMBOL;
}


this.parseSeatNumber();
this.parseSeatSection();

System.out.println(this.toString());
System.out.println("SeatNumber ----> " + seatNumber);

// Reemplazar caracteres invalidos para la impresora
this.venueName = this.replaceInvalidChars(this.venueName);
this.eventName = this.replaceInvalidChars(this.eventName);
this.parsedSeatSection = this.replaceInvalidChars(this.parsedSeatSection);
this.parsedSeatZone = this.replaceInvalidChars(this.parsedSeatZone);
this.seatAccessDoor = this.replaceInvalidChars(this.seatAccessDoor);

String text = " <LM><LM><LM>";
text += "<F2><HW2,2><RC120,30>";
text += this.venueName.toUpperCase();

text += "<F3><HW1,1><RC165," + this.centerText(this.eventName, 20) + ">";
text += this.eventName.toUpperCase();
text += "<F9><RC195," + this.centerText(this.presentationDate + " " + this.presentationTime + " hrs.", 13) + ">";
text += this.presentationDate + " " + this.presentationTime + " hrs.";

text += "<F9><RC240,30>Area:<RC240,32>Area:";
text += this.parsedSeatZone;
text += "<RC240,540>Boleto:<RC240,542>Boleto:";
text += this.ticketType;
text += "<RC265,30>Zona:<RC265,32>Zona:";
text += this.parsedSeatSection;
text += "<RC265,540>Precio:<RC265,542>Precio:";
text += this.ticketPrice;
text += "<RC290,30>Fila:<RC290,32>Fila:";
text += this.parsedSeatLabel;
text += "<RC290,140>Asiento:<RC290,142>Asiento:";
text += this.parsedSeatNumber;
text += "<RC290,310>Acceso:<RC290,312>Acceso:";
text += this.seatAccessDoor;

// Poner código de barras
// text += "<RC325,235><BI><NP5><X2>*" + this.ticketCode + "*";
// text += "<RC315,235><NP5><X2>*" + this.ticketCode + "*";
text += "<RC3,780><LT2><BX370,215>";

text += "<RC10,1050><OL5><X2>^" + this.ticketCode + "^";
text += "<RC10,1070><OL5><X2>^" + this.ticketCode + "^";
text += "<RC260,1080><RL><F2>" + this.ticketCode;

text += "<RC360,790><RL><F9>" + this.venueName;
text += "<RC362,790><RL><F9>" + this.venueName;

text += "<F2><HW1,1>";
text += "<RC" + centerTextVertical(this.eventName, 10) + ",820>";
text += this.eventName;
text += "<RC" + centerTextVertical(this.presentationDate + " " + this.presentationTime + " hrs.", 10) + ",840>";
text += this.presentationDate + " " + this.presentationTime + " hrs.";

text += "<RC3,860><LT2><VX370>";

text += "<RC360,875>Area:<RC361,875>Area:";
text += this.parsedSeatZone;

text += "<RC360,902>Zona:<RC361,902>Zona:";
text += this.parsedSeatSection;

text += "<RC360,929>Fila:<RC361,929>Fila:";
text += this.parsedSeatLabel;

text += "<RC280,929>Asiento:<RC281,929>Asiento:";
text += this.parsedSeatNumber;

text += "<RC160,929>Acceso:<RC161,929>Acceso:";
text += this.seatAccessDoor;

text += "<RC360,956>Boleto:<RC361,956>Boleto:";
text += this.ticketType;

text += "<RC200,956>Precio:<RC201,956>Precio:";
text += this.ticketPrice;

text += "<p>";
TextPrinterUtil.Print(text, PRINTER_PORT);

}

private void parseSeatNumber() {
this.parsedSeatLabel = "";
System.out.println("SeatNumber en parsing: " + seatNumber);
if (NULL_DATA_SYMBOL.equals(seatNumber))
{
parsedSeatLabel = NULL_DATA_SYMBOL;
parsedSeatNumber = NULL_DATA_SYMBOL;
return;
}
try {
if (this.seatNumber != null) {

for (int i = 0; i < this.seatNumber.length(); i++) {
if (Character.isDigit(this.seatNumber.charAt(i))) {
this.parsedSeatNumber = this.seatNumber.substring(i);
break;
}
this.parsedSeatLabel += this.seatNumber.charAt(i);
}

}
} catch (Exception e) {
e.printStackTrace();
}
}

private void parseSeatSection() {
parsedSeatZone = "";
try {
if (this.seatSection.indexOf("/") > -1) {
parsedSeatZone =
this.seatSection.substring(
0,
this.seatSection.indexOf("/"));
this.parsedSeatSection =
this.seatSection.substring(
this.seatSection.indexOf("/") + 1);
if (parsedSeatSection == null || "".equals(parsedSeatSection.trim()))
{
parsedSeatSection = NULL_DATA_SYMBOL;
}
} else {
parsedSeatZone = seatSection;
}
} catch (Exception e) {
e.printStackTrace();
}
}

private String replaceInvalidChars(String data)
{
data = data.replace('á', 'a');
data = data.replace('é', 'e');
data = data.replace('í', 'i');
data = data.replace('ó', 'o');
data = data.replace('ú', 'u');
data = data.replace('ñ', 'n');
data = data.replace('ü', 'u');
data = data.replace('Á', 'A');
data = data.replace('É', 'E');
data = data.replace('Í', 'I');
data = data.replace('Ó', 'O');
data = data.replace('Ú', 'U');
data = data.replace('Ñ', 'N');
data = data.replace('Ü', 'U');
return data;
}


private String centerText(String textToCenter, int charWidth)
{
int col = 0;
int textWidth = textToCenter.length() * charWidth;
col = (LM_MAX_COLUMNS - textWidth) / 2;
System.out.println("Valor de centrado para " + textToCenter + ": " + col);
col += 30;
return col + "";

}

private String centerTextVertical(String textToCenter, int charWidth)
{
int col = 0;
int textWidth = textToCenter.length() * charWidth;
col = (PM_MAX_COLUMNS - textWidth) / 2;
System.out.println("Valor de centrado vertical para " + textToCenter + ": " + col);
//col += 30;
return (PM_MAX_COLUMNS - col) + "";

}


/* (non-Javadoc)
* @see com.neitek.wt.print.DocumentTemplate#finish()
*/
public void finish() {
// TODO Auto-generated method stub

}
public void waithForReady()
{
try
{ Thread.sleep (100); }
catch (InterruptedException e)
{ e.printStackTrace(); }

}
}

samile
27 de Octubre del 2005
Pues parece mas comlicado de lo q pense, pero mas o menos la estructura es la siguiente:
Debes crear un PrinterJob (me imagino que lo sabes) donde iria toda la estructura de tus tikcets, y todo esto debe ir en el actionperformed de ese boton aceptar desde ese metodo mandar la impresion.
La verdad no estoy segura de q esto sea lo q necesites. pero bueno...

ticketlost
27 de Octubre del 2005
Muchas gracias, pero si ando en la luna, aparte los programas estan hechos en progress, no se si se puede utilizar java.

jamecaes
27 de Octubre del 2005
No se si sea de ayuda...
yo se como hacerlo si aun no lo has podido hacer, cuentame.