-1

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

Govinda Sakhare
  • 5,009
  • 6
  • 33
  • 74

2 Answers2

1

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

Community
  • 1
  • 1
dada67
  • 4,723
  • 17
  • 19
  • Exactly. And as Jasper-Reports already includes a copy of iText (albeit an old version)... – mkl May 12 '16 at 16:03
  • I did use iText for merging, but what is the issue with the above code why 2nd file overriding the first one. – Govinda Sakhare May 13 '16 at 06:35
  • 1
    The overwrite probably happens because the stream is not flushed after exporting the report, see tobi6's answer. Apart from that, the issue with the code is that concatenating the contents of two PDF files does not result in a valid PDF file. – dada67 May 16 '16 at 12:57
0

Since you use two different approaches to write data into the stream, try using

mergedFile.flush();

directly after exportReportToPdfStream.

tobi6
  • 8,033
  • 6
  • 26
  • 41