Foros del Web » Programación para mayores de 30 ;) » Java »

JSF exportar flash a pdf

Estas en el tema de JSF exportar flash a pdf en el foro de Java en Foros del Web. Estimados, estoy trabajando con amchart, unos graficos en flash y ademas estoy importando ese flash a pdf, si lo hago con un FileOutputStream funciona bien, ...
  #1 (permalink)  
Antiguo 19/10/2011, 07:48
 
Fecha de Ingreso: mayo-2011
Ubicación: Santiago
Mensajes: 20
Antigüedad: 13 años
Puntos: 2
Pregunta JSF exportar flash a pdf

Estimados, estoy trabajando con amchart, unos graficos en flash y ademas estoy importando ese flash a pdf, si lo hago con un FileOutputStream funciona bien, pero si lo quiero ver en linea, no me lo genera, voy a publicar el metodo completo para que ustedes puedan ayudarme, de antemano muchas gracias.

Código java:
Ver original
  1. public void doPost() throws ServletException, IOException, DocumentException {
  2.  
  3.         System.out.println("Entre al metodo doPost");
  4.         HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
  5.  
  6.         String heightStr = request.getParameter("height") == null ? "0" : request.getParameter("height");
  7.         String widthStr = request.getParameter("width") == null ? "0" : request.getParameter("width");
  8.         int height = 520;
  9.         int width = 300;
  10.         //int height = Integer.parseInt(heightStr.indexOf(".") == -1 ? heightStr : heightStr.substring(0, heightStr.indexOf(".")));
  11.         // int width = Integer.parseInt(widthStr.indexOf(".") == -1 ? widthStr : widthStr.substring(0, widthStr.indexOf(".")));
  12.         BufferedImage image = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);
  13.         Graphics g = image.getGraphics();
  14.  
  15.         String current_row = "";
  16.         Image docImage = null;
  17.         try {
  18.             for (int i = 0; i < height; i++) {
  19.                 current_row = request.getParameter("r" + i) == null ? "" : request.getParameter("r" + i);
  20.                 System.out.println("Estoy dentro del ciclo y esta es la info : " + request.getParameter("r" + i) == null ? "" : request.getParameter("r" + i));
  21.                 String current_col[] = current_row.split(",");
  22.                 int current_width = 1;
  23.                 for (int m = 0; m < current_col.length; m++) {
  24.                     String pixel[] = current_col[m].split(":");
  25.  
  26.                     if (pixel.length > 1) {
  27.                         g.setColor(this.charColor(pixel[0]));
  28.                         g.drawLine(current_width, i, current_width + Integer.parseInt(pixel[1]), i);
  29.                         current_width = current_width + Integer.parseInt(pixel[1]);
  30.                     } else if (pixel.length == 1) {
  31.                         g.setColor(this.charColor(pixel[0]));
  32.                         g.drawLine(current_width, i, current_width + 1, i);
  33.                         current_width++;
  34.                     }
  35.  
  36.  
  37.                 }
  38.             }
  39.  
  40.             docImage = Image.getInstance(image, null);
  41.  
  42.         } catch (Exception e) {
  43.             e.printStackTrace();
  44.         }
  45.         g.create();
  46.  
  47.         Document doc = new Document();
  48.         ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
  49.         PdfWriter docWriter = null;
  50.         PdfWriter dos = null;
  51.    
  52.         try {
  53.             docWriter = PdfWriter.getInstance(doc, baosPDF);
  54.             dos = PdfWriter.getInstance(doc, new FileOutputStream("C:\\ejemplo.pdf"));
  55.            
  56.             doc.open();
  57.  
  58.             Table table = new Table(1, 1);
  59.             table.setWidth(100);
  60.             table.setTableFitsPage(true);
  61.             table.setAlignment(Table.ALIGN_LEFT);
  62.             table.setBorder(Table.NO_BORDER);
  63.  
  64.             Cell cell = new Cell();
  65.             cell.setBorder(Cell.NO_BORDER);
  66.             cell.setVerticalAlignment(Cell.ALIGN_TOP);
  67.             cell.add(docImage);
  68.  
  69.             table.addCell(cell);
  70.             doc.add(table);
  71.  
  72.             doc.close();
  73.  
  74.         } catch (DocumentException e) {
  75.             e.printStackTrace();
  76.         }
  77.  
  78.  
  79.  
  80.         try {
  81.             FacesContext facesContext = FacesContext.getCurrentInstance();
  82.             HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
  83.                //  response.resetBuffer();
  84.  
  85.             response.setContentType("application/pdf");
  86.  
  87.             response.setContentLength(baosPDF.size());
  88.             System.out.println("Tamaño BAOSPDF : " + baosPDF.size());
  89.             //     System.out.println("RespuestA ? : " + response.getOutputStream().toString());
  90.             //      ServletOutputStream out = response.getOutputStream();
  91.        
  92.             ServletOutputStream out = response.getOutputStream();
  93.             baosPDF.writeTo(out);
  94.             out.write(baosPDF.toByteArray());
  95.             out.flush();
  96.             out.close();
  97.             //   baosPDF.flush();
  98.             //  baosPDF.close();
  99.             facesContext.responseComplete();
  100.         } catch (Exception e) {
  101.             System.out.println("Error : " + e.getMessage());
  102.             e.printStackTrace();
  103.         }
  104.        
  105.  
  106.         System.out.println("Sali del metodo dopost");
  107.  
  108.     }

El error que me sale es el siguiente "INFO: Error : PWC3990: getWriter() has already been called for this response" He buscado multiples opciones, alternativas, y ninguna me ha servido, por eso acudo a ustedes, espero que me puedan ayudar o darme ideas al respecto, gracias!


PD: Coloque el mime type a word para ver lo que escribe y solo escribe html y body xD , si necesitan mas datos me avisan

PD2: El error esta exactamente acá *ServletOutputStream out = response.getOutputStream();

Después de varios dias de intento, la solución era bastante ridicula y simple xD

en el xhtml que llama el método lo tenia asi :



Código html4strict:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml"
  3.      xmlns:h="http://java.sun.com/jsf/html"
  4.      xmlns:f="http://java.sun.com/jsf/core">
  5. <h:body>
  6. #{pdfMB.doPost()}
  7. </h:body>
  8. </html>

Y la solución es así.

La cosa es que al generar la pagina llamaba automaticamente al evento y generaba una excepcion en el response, pero si hacia que yo generara un evento y de ahí lo llamará automaticamente, entonces si funcionaba bien.

Me di cuenta porque si colocaba un boton que llamara al metodo me pintaba en negro el pdf, y no me tomaba los datos, entonces debí automatizar eso, y aqui la respuesta.

Código html4strict:
Ver original
  1. <?xml version='1.0' encoding='UTF-8' ?>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml"
  5.      xmlns:h="http://java.sun.com/jsf/html"
  6.      xmlns:f="http://java.sun.com/jsf/core">
  7.  
  8.  
  9.     <h:body>
  10.         <h:form>
  11.             <f:event listener="#{pdfMB.doPost()}" type="preRenderView" />
  12.        <!-- <h:commandButton action="{pdfMB.doPost()}" />-->
  13.         </h:form>
  14.     </h:body>
  15.  
  16.  
  17. </html>

Última edición por DonVidela; 19/10/2011 a las 12:05 Razón: Solucionado

Etiquetas: flash, jsf, pdf
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:01.