【问题标题】:FileOutputStream not appending while writing the bytesFileOutputStream 在写入字节时未附加
【发布时间】:2016-05-11 09:03:19
【问题描述】:

我有两个文件,其中一个是使用 jasper 生成的,另一个文件在本地磁盘上。我想要做的是将这两个文档合并到第三个文件中。

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
}

但我没有得到预期的结果,mergedFile 只包含supportedDocInputStream 的数据

【问题讨论】:

    标签: java io jasper-reports


    【解决方案1】:

    连接两个 PDF 文件的内容不会导致合并的 PDF 文件。

    如果你想合并两个 PDF 文件,你可以使用 iText,例如 function that can use iText to concatenate / merge pdfs together - causing some issues

    【讨论】:

    • 没错。由于 Jasper-Reports 已经包含 iText 的副本(尽管是旧版本)...
    • 我确实使用 iText 进行合并,但是上面的代码有什么问题,为什么第二个文件会覆盖第一个文件。
    • 可能会发生覆盖,因为在导出报告后未刷新流,请参阅 tobi6 的答案。除此之外,代码的问题是连接两个 PDF 文件的内容不会生成有效的 PDF 文件。
    【解决方案2】:

    由于您使用两种不同的方法将数据写入流中,请尝试使用

    mergedFile.flush();
    

    直接在exportReportToPdfStream之后。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-09
      • 2015-06-12
      • 2019-09-06
      • 2017-11-10
      • 2014-07-29
      • 2015-02-21
      • 1970-01-01
      • 2014-01-17
      相关资源
      最近更新 更多