Generate Output in Memory

Use printToStream with a ByteArrayOutputStream to generate a single PDF stream in memory. Select EPrinterType.PDFFile to choose the PDF format; no output filename is required.

Setup: Java interface. For a supplied project and layout, see the Quick Start.

Using printToStream

The following excerpt assumes that job already has its form layout and data configured:

job.setPrinterType(EPrinterType.PDFFile);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
job.printToStream(stream);
byte[] pdfData = stream.toByteArray();

toByteArray copies the generated PDF into pdfData, which remains available after the stream is no longer used.

Complete Example

The complete method below includes the layout and data setup. Replace the layout filename and data-field name before calling createPdfInMemory().

import com.tecit.TFORMer.*;
import com.tecit.TFORMer.Enumerations.*;
import com.tecit.TFORMer.Printing.Job;
import com.tecit.TFORMer.Printing.JobDataRecordSet;
import com.tecit.TFORMer.Printing.JobDataRecordSet.Record;
import java.io.ByteArrayOutputStream;

public static void createPdfInMemory() {
    Job job = null;
    try {
        job = new Job();
        job.setRepositoryName("FILENAME_OF_YOUR_FORMLAYOUT.tff");
        JobDataRecordSet data = new JobDataRecordSet();
        Record record = new JobDataRecordSet.Record();
        record.setData(
            "NAME_OF_YOUR_DATAFIELD", "Value of the data field");
        data.add(record);
        job.setJobData(data);
        job.setPrinterType(EPrinterType.PDFFile);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        job.printToStream(stream);
        byte[] pdfData = stream.toByteArray();

        // Use pdfData here.
        System.out.println("PDF size: " + pdfData.length + " bytes");
    } catch (TFormerException exception) {
        exception.printStackTrace();
    } finally {
        if (job != null) {
            try {
                job.dispose();
            } catch (TFormerException exception) {
                exception.printStackTrace();
            }
        }
    }
}

The method prints the PDF byte count. Use pdfData at the marked location to pass the PDF to your application. It does not write a PDF file.

If an output operation creates several streams, use the printToStreams example. It shows an OutputStreamFactory that creates a file stream for each generated page.

Always call dispose, including when an exception occurs. Otherwise native resources allocated by TFORMer can remain in use.

Search documentation

Search in
More scopes
Additional scopes

All contents