【问题标题】:Corrupted PDF file after generating from ByteArrayOutputStream in Java从 Java 中的 ByteArrayOutputStream 生成后损坏的 PDF 文件
【发布时间】:2021-09-22 07:08:05
【问题描述】:

我正在尝试使用 ReportClientDocument、ByteArrayInputStream 和 ByteArrayOutputStream 读取 .rpt 文件并生成 pdf。生成pdf文件后,我无法打开它。它显示“它可能已损坏或使用预览无法识别的文件格式。”下面提供了我的源代码

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();
    }
}

我可以读取 .rpt 文件并在控制台中打印它。请帮助我找到正确生成 pdf 的最佳方法。

【问题讨论】:

  • 我不熟悉 ReportClientDocument。但我可以肯定地说您不想使用 ObjectOutputStream 实例。它将创建 Java 特定的数据格式,当然不是 PDF。
  • 去掉ObjectOutputStream。你没有使用ObjectInputStream,所以你仍然有它的编码。你不需要。或者用ObjectInputStream阅读你写的东西。

标签: java pdf bytearrayoutputstream bytearrayinputstream rpt


【解决方案1】:

我不熟悉ReportClientDocument。据我了解,它本身不是 PDF 文档,而是可以保存为 PDF 的报告。 ObjectOutputStream 无法实现这一点,因为它是 Java 特定格式,与 PDF 无关。

PDF 导出似乎需要PrintOutputController。因此,您的代码看起来更像这样:

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);
    }
}

请注意,不需要字节数组流。它们是绕道而行,会减慢您的程序并增加内存消耗。

【讨论】:

  • 我之前已经尝试过使用 . InputStream = rcd.getPrintOutputController().export(ReportExportFormat.PDF);我得到了与库相关的异常 com.crystaldecisions.sdk.occa.report.lib.ReportSDKException: org/w3c/dom/ls/DocumentLS---- 错误代码:-2147467259 错误代码名称:在 com.crystaldecisions.sdk.occa 失败.report.application.PrintOutputController.if(SourceFile:237)
  • 你最好打开一个新问题来询问这个特定问题。
猜你喜欢
  • 1970-01-01
  • 2016-01-25
  • 1970-01-01
  • 2012-09-08
  • 2022-10-05
  • 1970-01-01
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
相关资源
最近更新 更多