1 Reply Latest reply: May 28, 2009 12:05 AM by Ankit Khujneri RSS

    Problem generating PDF using LifeCycle Designer ES

    Ankit Khujneri

      Hi,

       

      I am facing one problem while creating PDF using life cycle designer tool.

      I am trying to generate a PDF that will contain a simple textInput with some text. I am using a snapshot class to capture this component.

      This captured image is converted to base64 using base64Encoder.Now, this encoded string creates a problem with lifeCycle Designer..

      If I enter,say 7, in textInput, the PDF gets generated properly with a complete snaphot of text box and the text within. But if I enter some other text, say XXDD, the PDF generation fails. Is there any problem with decoding the base64 encoded string?

      Here is some code snippet :

       

      // Flex code

      private function generateImagePdf() : void {
                             
                  var url:String = "/pdfSampleApp";
                           
              var snapshot:ImageSnapshot = ImageSnapshot.captureImage(uname);// uname is a textInput
              containerImage = ImageSnapshot.encodeImageAsBase64(snapshot);
              url = url + "/PDFGeneratorServlet?xmlData="+xmlImageModel";
               navigateToURL(new URLRequest(url), "_New");
             
              }

      // this XML is passed as parameter

      <mx:XML id="xmlImageModel" >
          <root><imagefield>{uname}</imagefield></root>
      </mx:XML>

      //java

      //This code wil build the document from param String. This param is a parameter from request.

      String param = req.getParameter("xmlData");

      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                  DocumentBuilder db;
                  Document doc = null;
                  db = dbf.newDocumentBuilder();
                  doc = db.parse(new InputSource(new StringReader(param)));

       

        processElement(doc.getDocumentElement());

      helper.importDataset(doc);

      // Save new PDF as a byte array in the current session
                  byte[] bytes = helper.saveToByteArray();

                  // Close any resources
                  helper.close();

                  resp.setContentType("application/pdf");
                  resp.setContentLength(bytes.length);

                  BufferedOutputStream bos = new BufferedOutputStream(resp
                          .getOutputStream());
                  bos.write(bytes);

       

      private void processElement(Element elem) throws Exception {
              System.out.println("****************************************");
              System.out.println("Tag:  " + elem.getNodeName());
              //System.out.println("NS:   " + elem.getNamespaceURI());

              NodeList children = elem.getChildNodes();
              for (int ii = 0; ii < children.getLength(); ii++) {
                  Node child = children.item(ii);
                  if (child instanceof Element)
                      processElement((Element) child);
                  else if (child instanceof Text) {
                      String a = child.getNodeValue();
                      a = a.replace(' ', '/');
                      child.setNodeValue(a);

                      System.out.println("Text: " + child.getNodeValue());
                  }
              }
              System.out.println("****************************************");
          }


       

       

       

      On printing the value of the node <imageField> gives me data like:

               iVBORw0KGgoAAAANSUhEUgAAAN4AAAA4CAYAAACSekYo.// this is Base64 encoded string. For this case the PDF generation is successful, but if the string contains some spaces in between, the PDF generation fails.

       

      Please suggest!

        • 1. Re: Problem generating PDF using LifeCycle Designer ES
          Ankit Khujneri Community Member

          Hi All,

           

          The problem has been resolved.

           

          One learning: Never use the GET request method when passing the such huge data.

          //Flex code

           

          **********************************Instead of*****************************************

           

          var url:String = "/pdfSampleApp";
                               
                  var snapshot:ImageSnapshot = ImageSnapshot.captureImage(uname);// uname is a textInput
                  containerImage = ImageSnapshot.encodeImageAsBase64(snapshot);
                  url = url + "/PDFGeneratorServlet?xmlData="+xmlImageModel";
                   navigateToURL(new URLRequest(url), "_New");

           

          ********************************Use*****************************************************

          var req : URLRequest = new URLRequest(url)
                       var params : URLVariables = new URLVariables();
                       params.xmlData = xmlImageModel;
                       params.type  = type;
                       req.data = params;
                       req.method = URLRequestMethod.POST;
                        navigateToURL(req,"_New");

           

          Rest remains the same!

           

          Cheers!