Search code examples
javaiojasper-reports

FileOutputStream not appending while writing the bytes


I have two files one of which is generated by using the jasper, and another file is on a local disk. What I want to do is merge these two documents on the third file.

FileInputStream supportingDocInputStream = new FileInputStream("/home/xyz/Desktop/reject.pdf");

FileOutputStream mergedFile = new FileOutputStream(new File("/home/xyz/Desktop/original.pdf"),true);

HashMap<String, Object> map = new HashMap<String, Object>(1);
map.put("request_id", id);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JasperDesign design = JRXmlLoader.load("/home/xyz/Desktop/jaspers/sample.jrxml");
JasperReport report = JasperCompileManager.compileReport(design);
JasperPrint print = JasperFillManager.fillReport(report, map, con);
JasperExportManager.exportReportToPdfStream(print, mergedFile);

int read=0;

while((read=supportingDocInputStream.read())!=-1){
    mergedFile.write(read); // overwriting the jasper's data
}

But I am not getting expected result, mergedFile is containing only data of supportedDocInputStream


Solution

  • Concatenating the contents of two PDF files does not result in a merged PDF file.

    If you want to merge two PDF files you can use iText, see for instance function that can use iText to concatenate / merge pdfs together - causing some issues