1

I have an xml file and .doj template file. trying to download a PDF using itext.jar and ACTRuntime.jar. It is working in java 1.6 and Websphere 7 but after migrating to java 8 and websphere 8, it is just downloading blank white pages in PDF format. Below is the code that I'm using to produce PDF.

public ByteArrayOutputStream generatePdf(){
    ArrayList<Object> bean = new ArrayList<Object>();

    BeanOne beanOne = new BeanOne();
    beanOne.setId(1);
    beanOne.setName("Sai");
    beanOne.setPhone("1234567890");

    BeanOne beanOne1 = new BeanOne();
    beanOne1.setId(2);
    beanOne1.setName("Ram");
    beanOne1.setPhone("9876543210");

    bean.add(beanOne);
    bean.add(beanOne1);

    AppDataHandler ds = new AppDataHandler();

    String tableName = "First Talbe";

    Object obj= bean.get(0);

    Vector fields = new Vector();
    fields.addElement("id = getId");
    fields.addElement("name = getName");
    fields.addElement("phone = getPhone");

    ds.registerObjectAsTable(obj, tableName, fields);
    ds.registerDataSet(tableName, bean);

    FileInputStream reportTemplateInputStream = new FileInputStream(new File("/template.jod"));

    ACJEngine acjEngine = new ACJEngine();
    acjEngine.readTemplate(reportTemplateInputStream);

    TemplateManager templateManager = acjEngine.getTemplateManager();
    templateManager.setLabel("ID", "ID");
    templateManager.setLabel("NAME", "NAME");
    templateManager.setLabel("PHONE", "PHONE");

    acjEngine.setX11GfxAvailibility(false);
    acjEngine.setDataSource(ds);
    ACJOutputProcessor ec = new ACJOutputProcessor();
    IViewerInterface ivi = acjEngine.generateReport();
    ByteArrayOutputStream generatedPDFStream = new ByteArrayOutputStream();
    ec.setPDFProperty("OutputStream", generatedPDFStream);
    ec.generatePDF();
    reportTemplateInputStream.close();
    Object[] pdfFromActuateArray = new Object[1];
    pdfFromActuateArray[0] = generatedPDFStream.toByteArray();
    return mergePdfsUsingItext(pdfFromActuateArray);
}

private ByteArrayOutputStream mergePdfsUsingItext(Object[] documents) throws com.itextpdf.text.DocumentException {
    ByteArrayOutputStream content = new ByteArrayOutputStream();
    int f;
    byte[] byteDoc = null;
    for (f = 0; f < documents.length; ++f) {
        byteDoc = (byte[]) documents[f];
        if (byteDoc != null)
            break;
    }
    PdfReader reader = new PdfReader(byteDoc);
    int n = reader.getNumberOfPages();
    Document document = new Document(reader.getPageSizeWithRotation(1));
    PdfWriter writer = PdfWriter.getInstance(document, content);
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    PdfImportedPage page;
    int rotation;
    while (f < documents.length) {
        int i = 0;
        while (i < n) {
            i++;
            document.setPageSize(reader.getPageSizeWithRotation(i));
            document.newPage();
            page = writer.getImportedPage(reader, i);
            cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
        }
        f++;
        if (f < documents.length) {
            reader = new PdfReader((byte[]) documents[f]);
            n = reader.getNumberOfPages();
        }
    }
    document.close();
    return content;
}

With the above code I'm getting the ByteArrayOutputStream which I'm printing on the jsp page with content-type as application/pdf.

The result pdf is completely blank. Hope Someone could explain the issue. Also please suggest any good alternative to this code.

Thanks in Advance.

Venkata
  • 135
  • 8
  • Looks like an encoding problem. PDF is a binary file format. Part of it is ASCII (for instance how the content is distributed over different pages), but the content of a page itself is compressed into a binary stream. If the binary stream is corrupted (e.g. because Websphere is configured incorrectly), then you'll see the pages, but no content. This is a question for Websphere support, not for iText (since iText is doing the job correctly with Websphere 7). – Bruno Lowagie Mar 01 '18 at 16:42

0 Answers0