Corrupted PDF file after generating from ByteArrayOutputStream in Java

866 Views Asked by At

I am trying to read .rpt file and generate pdf using ReportClientDocument,ByteArrayInputStream and ByteArrayOutputStream. After generating the pdf file I am unable to open it. It is showing "It may be damaged or use a file format that Preview doesn’t recognise." My Source code is provided below

public static void generatePDFReport()
{
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    LocalDateTime now = LocalDateTime.now();
    System.out.println(dtf.format(now));
    try {
        ReportClientDocument rcd = new ReportClientDocument();

        String rptPath="/Users/florapc/Desktop/Report/AcStatement.rpt";
        String outputPath=String.format("/Users/florapc/Desktop/Report/%s.pdf",dtf.format(now));
        File inputFile = new File(rptPath);
        File outputFile = new File(outputPath);
        rcd.open(rptPath, 0);
        System.out.println(rptPath);
        List<IParameterField> fld = rcd.getDataDefController().getDataDefinition().getParameterFields();

        List<String> reportContent = new ArrayList<String>();
        System.out.println(fld.size());
        for (int i = 0; i < fld.size(); i++) {

            System.out.println(fld.get(i).getDescription());
            reportContent.add(fld.get(i).getDescription().replaceAll("[^a-zA-Z0-9]", " "));
        }

                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(reportContent);
        byte[] bytes = bos.toByteArray();

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        byte[] byteArray = new byte[byteArrayInputStream.available()];
        int x = byteArrayInputStream.read(byteArray, 0, byteArrayInputStream.available());
        System.out.println(x);
        FileOutputStream fileOutputStream = new FileOutputStream(outputFile);;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();;
        byteArrayOutputStream.write(byteArray, 0, x);

        byteArrayOutputStream.writeTo(fileOutputStream);
        System.out.println(fileOutputStream);
        System.out.println("File exported succesfully");

        byteArrayInputStream.close();

        byteArrayOutputStream.close();
        fileOutputStream.close();
        rcd.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

I can read .rpt file and print it in console. Please help me finding the best way to generate pdf properly.

1

There are 1 best solutions below

2
On

I'm not familiar with ReportClientDocument. As far as I understand it's not a PDF document itself but a report that can be saved as PDF. ObjectOutputStream won't achieve this as it's Java specific format and has nothing to do with PDF.

It looks as if a PrintOutputController is needed for PDF export. Thus your code would look more like so:

FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
InputStream is = rcd.getPrintOutputController().export(ReportExportFormat.PDF);
copy(is, fileOutputStream);
fileOutputStream.close();

...

void copy(InputStream source, OutputStream target) throws IOException {
    byte[] buf = new byte[8192];
    int length;
    while ((length = source.read(buf)) > 0) {
        target.write(buf, 0, length);
    }
}

Note that no byte array streams are needed. They are a detour, slowing down your program and drive up memory consumption.