Loading ...
Sorry, an error occurred while loading the content.

urgent help with pdf rendering

Expand Messages
  • soken78
    Hello I have been able to successfully render a pdf from a specified xsl and xml file. However only the static information within the pdf is shown. I am
    Message 1 of 2 , Aug 29, 2003
    • 0 Attachment
      Hello I have been able to successfully render a pdf from a specified
      xsl and xml file. However only the static information within the pdf
      is shown. I am suspicious that maybe the xml is empty. Can someone
      please verfiy that the following code is correct, specifically the
      line: byte[] xmlSrc = xml.getBytes();. I think that this might be
      causing the problem. If it is how can I convert the string to a
      byte[] and then convert that byte[] to a StreamSource?

      This is quite urgent. I hope someone can help and thank you in
      advance.

      T.

      The code is below:

      public byte[] print(String xsl, String xml)
      {
      String xslSrc = xsl;
      byte[] xmlSrc = xml.getBytes();

      for (int i = 0; i < xmlSrc.length; i++)
      {

      }


      com.hp.nimius.util.trace.Trace.debug("xmlSrc from FopToPDF is:
      " + xmlSrc);
      byte[] pdfResult = null;
      int renderType = getRenderType("pdf");

      //Construct a Driver
      Driver driver = new Driver();

      //Setup Renderer (output format)
      driver.setRenderer(renderType);

      try
      {
      //Render the xslt/xml to create a fo file and pipe through
      to FOP
      StringBuffer fo = renderXSL(xslSrc, xmlSrc);
      //Render the FO file with FOP
      pdfResult = renderFOP(fo, renderType);
      }
      catch (Exception ex)
      {
      System.err.println("*** exception in PrinterFOP: " +
      ex.toString());
      ex.printStackTrace();
      }
      return pdfResult;
      }


      /**
      * call the FOP parser to render a fo to a printout
      *
      * @param fo java.lang.StringBuffer
      * @param renderType int
      * @return byte[]
      */
      private byte[] renderFOP(StringBuffer fo, int renderType) throws
      IOException, FOPException
      {

      ByteArrayOutputStream out = new ByteArrayOutputStream();
      StringReader inReader = null;

      try
      {
      inReader = new StringReader(fo.toString());
      InputSource in = new InputSource(inReader);
      Driver driver = new Driver(in, out);
      driver.setRenderer(renderType);
      driver.run();
      }
      finally
      {
      if (inReader != null)
      inReader.close();
      }
      return out.toByteArray();
      }

      /**
      * render a XML against an xslFile to create a fo file to be later
      used to
      * render PDF.
      *
      * @param xmlFile java.lang.String
      * @param xslFile java.lang.String
      * @return StringBuffer
      */
      public StringBuffer renderXSL(String xslFile, byte[] xmlFile)
      throws TransformerException,
      TransformerConfigurationException, JBSException, Exception
      {
      StringWriter outWriter = new StringWriter();
      StreamSource xslResult = new StreamSource(xslFile);
      StreamSource xmlSource = new StreamSource(new
      ByteArrayInputStream(xmlFile));

      TransformerFactory tFactory =
      TransformerFactory.newInstance();
      Transformer transformer = tFactory.newTransformer(xslResult);
      transformer.transform(xmlSource, new StreamResult(outWriter));

      return outWriter.getBuffer();
      }
    • Chris Bowditch
      ... What XSL-FO formatter are you using? ... To convert string to byte array use the getBytes() method on String. StreamSource takes InputStream as argument to
      Message 2 of 2 , Sep 1, 2003
      • 0 Attachment
        >From: "soken78" <soken78@...>
        >
        >Hello I have been able to successfully render a pdf from a specified
        >xsl and xml file.

        What XSL-FO formatter are you using?

        >However only the static information within the pdf
        >is shown. I am suspicious that maybe the xml is empty. Can someone
        >please verfiy that the following code is correct, specifically the
        >line: byte[] xmlSrc = xml.getBytes();. I think that this might be
        >causing the problem. If it is how can I convert the string to a
        >byte[] and then convert that byte[] to a StreamSource?

        To convert string to byte array use the getBytes() method on String.
        StreamSource takes InputStream as argument to its contructor, so create a
        ByteArrayInputStream based on your byte array, e.g.

        new StreamSource(new ByteArrayInputStream(yourString.getBytes()));

        >
        >This is quite urgent. I hope someone can help and thank you in
        >advance.
        >

        <snip/>

        I had a quick look at your code and noticed that you don't close the output
        stream in your renderFOP method. You need to close the ByteArrayOutputStream
        to ensure all data is flushed to the array.

        The problem could also be in your stylesheet, post small snippets of your
        XSL and XML files if you still have no luck.

        Chris

        _________________________________________________________________
        Use MSN Messenger to send music and pics to your friends
        http://www.msn.co.uk/messenger
      Your message has been successfully submitted and would be delivered to recipients shortly.