package tarjetasServlet;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import m05bClasses.m05bCamposInformes;
import m05bClasses.m05bComun;
import m05bClasses.m05bConstantesInformes;
import m05bClasses.m05bFileLog;

import org.apache.fop.apps.Driver;
import org.xml.sax.InputSource;


public class m05bImpTarjDupliServlet extends javax.servlet.http.HttpServlet
{
   public static final String FO_REQUEST_PARAM = "fo";
//    Logger log = null;
   m05bFileLog miLog = new m05bFileLog();
   public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
   {
 	   try{
		      doGet(request, response);
		    }catch (Exception e){
		      miLog.addTraza(e);
		    }
   }

   public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException
   {
      performTask(request, response);
   }
//        if(log == null)
//        {
//            Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
//            log = hierarchy.getLoggerFor("fop");
//            log.setPriority(Priority.WARN);
//        }

   public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws ServletException
   {
      try
      {  HttpSession session = request.getSession(true);

      	 // Obtención de información de BD
      	 	 	// Vehículo
      	 String matricula=request.getParameter("Matricula");
         String sIncidencia=request.getParameter("Incidencia");
         String sPlastico=request.getParameter("Plastic");
         String sNcta=request.getParameter("NCta");
         String sLimite=request.getParameter("Limite");
         String sTarjeta=request.getParameter("Codigo");
         String sHisTarjeta=request.getParameter("CodInterno");
         String sCentro=request.getParameter("CentroT");
         String sTipo=request.getParameter("Tipo");
         String sTipoT=request.getParameter("TipoT");
      	 m05bCamposInformes miVehiculo = new m05bCamposInformes();
      	 ////System.out.println(sHisTarjeta);
      	 String banco[] = miVehiculo.getDatosTarjetaBanco(sHisTarjeta);
      	 
	 	 m05bComun miComun = new m05bComun();
      	 
      	 		
      	 String nombre = (String) session.getAttribute("PDFNombre");  // Se pone en la sesion en JSP Asignaciones
      	 String firma = miVehiculo.getFirma(m05bConstantesInformes.DET7_C_ACTA);

         //String foParam = request.getParameter(FO_REQUEST_PARAM);
         String sFoFileDir = "/datos/m05b/tmp/";
         File foFileDir = new File(sFoFileDir);
         File tmpFoFile = File.createTempFile("m05b", "Fo", foFileDir);


         FileOutputStream tmpFoFileStream = new FileOutputStream(tmpFoFile);

         String sLinea;

         sLinea = "<?xml version=\"1.0\" encoding=\"windows-1252\"?>\n";
         tmpFoFileStream.write(sLinea.getBytes());
         
         sLinea = "<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">\n";
         tmpFoFileStream.write(sLinea.getBytes());
		 // LAYOUT DEL LISTADO 
	         sLinea = "<fo:layout-master-set>\n";
	         tmpFoFileStream.write(sLinea.getBytes());
		         sLinea = "	<fo:simple-page-master master-name=\"general\" page-height=\"33cm\" page-width=\"21cm\" margin-top=\"2cm\" margin-bottom=\"2cm\" margin-left=\"2cm\" margin-right=\"2.5cm\">\n";
			     tmpFoFileStream.write(sLinea.getBytes());
			         sLinea = "		<fo:region-before extent=\"0cm\"/>\n";
			         tmpFoFileStream.write(sLinea.getBytes());
			         sLinea = "		<fo:region-body margin-bottom=\"0cm\" margin-top=\"0cm\"/>\n";
			         tmpFoFileStream.write(sLinea.getBytes());
			         sLinea = "		<fo:region-after extent=\"0cm\"/>\n";
			         tmpFoFileStream.write(sLinea.getBytes());
			         sLinea = "		<fo:region-start extent=\"0cm\"/>\n";
			         tmpFoFileStream.write(sLinea.getBytes());
			         sLinea = "		<fo:region-end extent=\"0cm\"/>\n";
			         tmpFoFileStream.write(sLinea.getBytes());
		         sLinea = "	</fo:simple-page-master>\n";
		         tmpFoFileStream.write(sLinea.getBytes());
	         sLinea = "</fo:layout-master-set>\n";
	         tmpFoFileStream.write(sLinea.getBytes());
		 // PAGE-SEQUENCE DEL LISTADO 
	         sLinea = "<fo:page-sequence master-reference=\"general\">\n";
	         tmpFoFileStream.write(sLinea.getBytes());
		         sLinea = "	<fo:flow flow-name=\"xsl-region-body\">\n";
		         tmpFoFileStream.write(sLinea.getBytes());
		         Context ctx =  new InitialContext(System.getProperties());

		           // --------------    CABECERA DEL LISTADO    --------------
                        //Cabecera 1
		            sLinea = "		<fo:block-container position=\"absolute\" top=\"0cm\" left=\"3.4cm\" width=\"10.5cm\" height=\"1.7cm\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	//  Escudo
			            sLinea = "			<fo:external-graphic src=\"" + "/datos/m05b/file/images/fop/cabeceraHacFin.jpg\" height=\"1.7cm\" width=\"10.5cm\"/>\n";
			            tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "      </fo:block-container>\n";
		            tmpFoFileStream.write(sLinea.getBytes());

		            sLinea = "      <fo:block-container  position=\"absolute\" top=\"2.6cm\"  width=\"15cm\" height=\"2.8cm\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	// Detalle-1
		            	sLinea= "       <fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"55mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"50mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"66mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"10pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			//  Blanco1
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\">\n " + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			//  Blanco2
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" >\n " + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			//  Cuadro vacío
			            			sLinea = "	    <fo:table-cell border-style=\"solid\"  border-width=\"0.1pt\" height=\"2.7cm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" font-size=\"8pt\" padding-top=\"7pt\">\n <![CDATA[" + banco[0] + "]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  font-size=\"8pt\" padding-top=\"7pt\">\n <![CDATA[" + banco[1] + "]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  font-size=\"8pt\" padding-top=\"7pt\">\n <![CDATA[" + banco[2] + "]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" font-size=\"8pt\" padding-top=\"7pt\">\n <![CDATA[" + banco[3] + "]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
				            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    </fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    </fo:table-body>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());			            								            		
			            sLinea= "       </fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "      </fo:block-container>\n";
		            tmpFoFileStream.write(sLinea.getBytes());



      	         // --------------    DETALLE DEL LISTADO    --------------

		            sLinea = "      <fo:block-container position=\"absolute\" top=\"5.5cm\"  width=\"15cm\" height=\"5.5cm\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	// Detalle-1
		            	sLinea= "       <fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"75mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"10mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"75mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"10pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// DET1 - Jaun horiek
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"8pt\">\n " + m05bConstantesInformes.DET1_E_TARJETA_DUPLI + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// DET1 - Intermedia
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"8pt\">\n " +  "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());

			            			// DET1 - Muy señores Nuestros
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  padding-top=\"8pt\">\n " + m05bConstantesInformes.DET1_C_TARJETA_DUPLI +  "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    </fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// Ondoren zehazten - Euskera
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			////System.out.println(sTipo);
			            			if (sTipo.equals("0")){
			            				if (sTipoT.equals("G")){
							             sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"8pt\">\n " + m05bConstantesInformes.DET2_E_TARJETA_MODIF + "</fo:block>\n";
			            				}else{
			            					sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"8pt\">\n " + m05bConstantesInformes.DET2_E_TARJETA_MODIF_BIS + "</fo:block>\n";			            					
			            				}
							        }else {
			            				if (sTipoT.equals("G")){
                                         sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"8pt\">\n " + m05bConstantesInformes.DET2_E_TARJETA_DUPLI + "</fo:block>\n";	
			            				}else{
			            					sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"8pt\">\n " + m05bConstantesInformes.DET2_E_TARJETA_DUPLI_BIS + "</fo:block>\n";			            					
			            				}
                                    }                                         
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// DET1 - Intermedia
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"8pt\">\n " +  "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());

			            			// De conformidad - Castellano
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			if (sTipo.equals("0")){
			            				if (sTipoT.equals("G")){			            				
							            sLinea = "      <fo:block  padding-top=\"8pt\">\n " + m05bConstantesInformes.DET2_C_TARJETA_MODIF +  "</fo:block>\n";
			            				}else{
			            					sLinea = "      <fo:block  padding-top=\"8pt\">\n " + m05bConstantesInformes.DET2_C_TARJETA_MODIF_BIS +  "</fo:block>\n";
			            				}
							        }else{
			            				if (sTipoT.equals("G")){							        	
							             sLinea = "      <fo:block  padding-top=\"8pt\">\n " + m05bConstantesInformes.DET2_C_TARJETA_DUPLI +  "</fo:block>\n";
			            				}else{
								             sLinea = "      <fo:block  padding-top=\"8pt\">\n " + m05bConstantesInformes.DET2_C_TARJETA_DUPLI_BIS +  "</fo:block>\n";			            					
			            				}
							        }	
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    </fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            		
		            		sLinea = "	    </fo:table-body>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());			            								            		
			            sLinea= "       </fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "      </fo:block-container>\n";
		            tmpFoFileStream.write(sLinea.getBytes());


						// Cuerpo
		            sLinea = "      <fo:block-container position=\"absolute\" top=\"9cm\"   width=\"15cm\" height=\"5.5cm\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		           		sLinea= "       <fo:table table-layout=\"fixed\">\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"15mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"15mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"20mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"20mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"30mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"20mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"20mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"30mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            	
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"8pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// Unitatea 
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" text-align=\"center\">\n Unitatea</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Zentrua
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n Zentrua</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
		            			   // MEGE Kodea
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n MEGE Kodea</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
     			                   // Plastiko Mota
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n Plastiko Mota</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
                         			// Txartela ZK.
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n Txartela ZK.</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
     			                   // Matrikula
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n Matrikula</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
			            		   // DIRU-MUGA
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n Diru-Muga</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
     			                   // ZERGATIA
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n Zergatia</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());				            			
			            		sLinea = "	    </fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// Unidad 
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n Unidad</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Centro
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n Centro</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Cod.Mege
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n Cod.MEGE</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Tipo Plástico
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n Tipo Plástico</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Nº tarjeta
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n NºTarjeta</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Mátricula
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n Matricula</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Límite
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n Limite</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Motivo
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n Motivo</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			
			            		sLinea = "	    </fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            		///DETALLE
   		                        sLinea = "	    <fo:table-row >\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// Unidad 
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block line-height=\"15pt\" text-align=\"center\">\n 01</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Centro
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block line-height=\"15pt\" text-align=\"center\">\n <![CDATA["+ sCentro +"]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Cod.Mege
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block line-height=\"15pt\" text-align=\"center\">\n 888888</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Tipo Plástico
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block line-height=\"15pt\" text-align=\"center\">\n 01</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Nº tarjeta
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block line-height=\"15pt\" text-align=\"center\">\n<![CDATA[" + sTarjeta +"]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Mátricula
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block line-height=\"15pt\" text-align=\"center\">\n<![CDATA["+  matricula +"]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Límite
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block line-height=\"15pt\" text-align=\"center\">\n<![CDATA["+  sLimite +"]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Motivo
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block line-height=\"15pt\" text-align=\"center\">\n<![CDATA[" +sIncidencia +"]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			
			            		sLinea = "	    </fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());			            		
			            	sLinea = "	    </fo:table-body>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());			            								            		
			            sLinea= "       </fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());   
		            sLinea = "      </fo:block-container>\n";
		            tmpFoFileStream.write(sLinea.getBytes());


		            sLinea = "      <fo:block-container position=\"absolute\" top=\"20cm\"  width=\"15cm\" height=\"5.5cm\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	// Detalle-1
		            	sLinea= "       <fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"75mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"10mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"75mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"10pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// DET1 - Adeitasunez
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"10pt\">\n " + m05bConstantesInformes.DET3_E_TARJETA_DUPLI + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// DET1 - Intermedia
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"10pt\">\n " +  "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());

			            			// DET1 - Atentamente
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  padding-top=\"10pt\">\n " + m05bConstantesInformes.DET3_C_TARJETA_DUPLI +  "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    </fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            		
		            		sLinea = "	    </fo:table-body>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());			            								            		
			            sLinea= "       </fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "      </fo:block-container>\n";
		            tmpFoFileStream.write(sLinea.getBytes());



		            
                        //PIE DE PAGINA FIRMA
		         // --------------   EN   --------------
					sLinea = "      <fo:block-container position=\"absolute\" top=\"23cm\"  width=\"15cm\" height=\"2cm\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	sLinea= "       <fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"55mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"40mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"55mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"10pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// En Castellano 
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"left\"  font-weight=\"bold\">\n" +  m05bConstantesInformes.EN_E_ASIGNACION + "</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Intermedio
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\">\n </fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
		            			// Puntos
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block>\n  </fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
			            		sLinea = "	    </fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// En Euskera 
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"left\">\n" + m05bConstantesInformes.EN_C_ASIGNACION + "</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Intermedio
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"left\">\n "+ miComun.getDetFechaActual(m05bConstantesInformes.IDIOMA_C)+ " </fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
		            			// Puntos
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block>\n </fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    </fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());

		            		sLinea = "	    </fo:table-body>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());			            								            		
			            sLinea= "       </fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "      </fo:block-container>\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            
//FIRMADO:
					sLinea = "      <fo:block-container position=\"absolute\" top=\"26.5cm\" left=\"2.90cm\" width=\"15cm\" height=\"2cm\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	sLinea= "       <fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"10mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"15mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"60mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			                sLinea = "	    <fo:table-column column-width=\"5mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"10pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		                              // FIRMA CASTELLANO
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
                                          //Fiema en Castellano
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\">\n" + m05bConstantesInformes.FIR_E_ASIGNACION+ "</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
		            			// Firma en euskera
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"left\">\n" + " / " + m05bConstantesInformes.FIR_C_ASIGNACION+ "</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
		            			// Responsable
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"left\">\n<![CDATA[" + firma + "]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());		
			                       // jna.
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            //sLinea = "      <fo:block  text-align=\"left\" font-weight=\"bold\">\n" + "jna." + "</fo:block>\n";
			            				sLinea = "      <fo:block  text-align=\"left\" font-weight=\"bold\">\n" + "" + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());		
			            		sLinea = "	    </fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    </fo:table-body>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());			            								            		
			            sLinea= "       </fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "      </fo:block-container>\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            
		            sLinea = "      <fo:block-container position=\"absolute\" top=\"27cm\" left=\"0.60cm\" width=\"15cm\" height=\"2.8cm\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	// Detalle-1
		            	sLinea= "       <fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"80mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"80mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            	
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"10pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			//  Responsable parque movil
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" text-align=\"right\" >\n " + m05bConstantesInformes.DET7_E_ACTA+ "  " + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"left\">\n " +" "+ m05bConstantesInformes.DET7_C_ACTA +"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    </fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    </fo:table-body>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());			            								            		
			            sLinea= "       </fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "      </fo:block-container>\n";
		            tmpFoFileStream.write(sLinea.getBytes());



                        //PIE DE PAGINA Pie
					sLinea = "      <fo:block-container position=\"absolute\" top=\"28.5cm\"  width=\"15cm\" height=\"2cm\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	sLinea= "       <fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"160mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"6pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		                              // Donostia SS
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
                                          //Donostias SS
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"center\">" + m05bConstantesInformes.PIE_ASIGNACION+ "</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
			            		sLinea = "	    </fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    </fo:table-body>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());			            								            		
			            sLinea= "       </fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "      </fo:block-container>\n";
		            tmpFoFileStream.write(sLinea.getBytes());



		         sLinea = "      </fo:flow>\n";
                 tmpFoFileStream.write(sLinea.getBytes());
                 
         	 sLinea = "   </fo:page-sequence>\n";
             tmpFoFileStream.write(sLinea.getBytes());
             
         sLinea = "</fo:root>\n";
         tmpFoFileStream.write(sLinea.getBytes());
         

         tmpFoFileStream.close();

         FileInputStream file = new FileInputStream(sFoFileDir + tmpFoFile.getName());

         renderFO(new InputSource(file), response);

         tmpFoFile.delete();

         response.flushBuffer();


      }
      catch (Exception ex)
      {
      	try {
			response.sendRedirect("m05bError.jsp");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			miLog.addTraza(e);
		}
      }
    }

    /**
     * renders an FO inputsource into a PDF file which is rendered
     * directly to the response object's OutputStream
     */
    public void renderFO(InputSource foFile, HttpServletResponse response) throws ServletException
    {
        try
        {
            ByteArrayOutputStream out = new ByteArrayOutputStream();

	    	response.reset();
            OutputStream oOut=response.getOutputStream();
            BufferedOutputStream bOut=new BufferedOutputStream(oOut);
            Driver driver = new Driver(foFile, out);
            driver.setRenderer(Driver.RENDER_PDF);
            driver.run();

            response.setContentLength(out.size());
	    response.setContentType("application/pdf");


            bOut.write(out.toByteArray());
	    out.close();
	    bOut.flush();
            bOut.close();
//	    response.flushBuffer();
        }
        catch (Exception ex)
        {
            throw new ServletException(ex);
        }
    }
}