Previewing an XML Print Job

This sample demonstrates how you can process an XML print job and receive a preview of the data that would have been printed for that XML document and XSL template.

Figure 1. Previewing an XML Print Job
import curam.util.administration.struct.XSLTemplateInstanceKey;
import curam.util.exception.AppException;
import curam.util.exception.DatabaseException;
import curam.util.exception.InformationalException;
import curam.util.internal.xml.impl.XMLPrintStreamConstants;
import curam.util.type.Blob;
import curam.util.xml.impl.XMLDocument;
import curam.util.xml.impl.XMLEncodingConstants;
import curam.util.xml.impl.XMLPrintStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class XMLServerTest {

  MyResult previewJob(
    final XSLTemplateInstanceKey tempKey,
    final MyStruct myStruct)
    throws DatabaseException, AppException,
    InformationalException, IOException {

    final XMLPrintStream myPrintStream =
      new XMLPrintStream();
    final ByteArrayOutputStream previewBuffer =
      new ByteArrayOutputStream();
    myPrintStream.setPreviewStream(previewBuffer);

    // Explicitly specify that a PDF document be created:
    myPrintStream.setJobType(
      XMLPrintStreamConstants.kJobTypePDF);

    myPrintStream.open(tempKey, MyPC, 1234);
    final XMLDocument myDoc =
      new XMLDocument(
        myPrintStream.getStream(),
        XMLEncodingConstants.kEncodeISOLATIN1);
    myDoc.open("A User", "31-Dec-1999", "1.0", "Sample 1");
    myDoc.add(myStruct);
    myDoc.close();
    myPrintStream.close();

    // Now that we have created the PDF document the
    // following code illustrates three things that
    // can be done with it.

    // (1) Save the document to disk.
    final FileOutputStream previewFile =
      new FileOutputStream("/preview.pdf");
    previewBuffer.writeTo(previewFile);
    previewFile.close();

    // This class contains both a String and
    // a Blob for demonstration purposes.
    final MyResult result = new MyResult();

    // (2) Store the PDF preview in a String:
    result.previewDocString =  previewBuffer.toString();

    // (3) Store the PDF document in a Blob:
    result.previewDocBlob =
      new curam.util.type.Blob(previewBuffer.toByteArray());

    return result;
  }

}

Having received the PDF preview of the data, this sample illustrates three ways in which the preview can be used:

  1. Save it to disk.
  2. Store it in a String variable.
  3. Store it in a Blob. This is recommended if the document is to be stored on the database.

This example used an java.io.ByteArrayOutputStream as a buffer to hold the generated PDF document because this class was most suited to the three examples above. However any sub-class of java.io.OutputStream can be used, depending on your needs. For example, a java.io.FileOutputStream could be used if you wish to write the data to a file.