java - 使用 PDFMergerUtility 合并两个 pdf 文档会抛出:IOException("Page tree root must be a dictionary")
问题描述
我有一个 spring boot 应用程序,我正在尝试合并两个 pdf 文件。我从另一个服务作为字节数组获得的一个,以及我在我的资源文件中本地拥有的一个:/static/documents/my-file.pdf。这是我如何从资源文件中获取字节数组的代码:
public static byte[] getMyPdfContentForLocale(final Locale locale) {
byte[] result = new byte[0];
try {
final File myFile = new ClassPathResource(TEMPLATES.get(locale)).getFile();
final Path filePath = Paths.get(myFile.getPath());
result = Files.readAllBytes(filePath);
} catch (IOException e) {
LOGGER.error(format("Failed to get document for local %s", locale), e);
}
return result;
}
我正在获取文件并获取字节数组。后来我尝试将这两个文件与以下代码合并:
PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
pdfMergerUtility.addSource(new ByteArrayInputStream(offerDocument));
pdfMergerUtility.addSource(new ByteArrayInputStream(merkblattDocument));
ByteArrayOutputStream os = new ByteArrayOutputStream();
pdfMergerUtility.setDestinationStream(os);
pdfMergerUtility.mergeDocuments(null);
os.toByteArray();
但不幸的是它抛出了一个错误:
throw new IOException("Page tree root must be a dictionary");
我已经检查过了,它在抛出它之前进行了验证:
if (!(root.getDictionaryObject(COSName.PAGES) instanceof COSDictionary))
{
throw new IOException("Page tree root must be a dictionary");
}
我真的不知道这意味着什么以及如何解决它。最奇怪的是,我创建了全新的项目并尝试使用相同的代码来合并两个文档(相同的文档)并且它可以工作!
此外,我尝试过的是:
- 如果没问题就换spring boot版本
- 像这样设置 mergeDocuments 方法:pdfMergerUtility.mergeDocuments(setupMainMemoryOnly())
- 像这样设置 mergeDocuments 方法:pdfMergerUtility.mergeDocuments(setupTempFileOnly())
- 使用不使用 java.nio 中的文件的不同方法获取字节:
- 并且还在不同的线程中执行了这个
- 合并仅本地存储的文件(在资源中)
- 合并我从另一项服务获得的文件 - 顺便说一句,这就是为什么我确信他没问题
有人能帮忙吗?
解决方案
Tilman Hausherr所说的问题在于您可以在 pom 文件中找到的资源过滤。如果您有不允许修改它的情况,那么这种方法将帮助您:
final String path = new
ClassPathResource(TEMPLATES.get(locale)).getFile().getAbsolutePath();
final File file = new File(path);
final Path filePath = Paths.get(file.getPath());
result = Files.readAllBytes(filePath);
然后只需将字节传递给 pdfMergerUtility 对象(甚至是整个文件而不是字节列表)。
推荐阅读
- apache-calcite - 如何将 Calcite 逻辑计划转换为另一个数据引擎的物理计划?
- java - 如何在提升基础 ci 环境中处理项目的 jira 版本和 maven 版本控制
- c# - 未捕获任务异常
- common-lisp - 打开共享对象时出错:如何嵌入共享对象或以编程方式指定不同的路径名?
- r - 将 stat_compare_means 与自己的 p.signif 标签一起使用
- openlayers - Openlayers 6 - 在线显示 Geojson 标签
- android - ChildEventListener 没有正确填充列表视图
- php - 当我尝试将 wordpress 中的自定义表单提交到模板页面时,我收到警告并且无法发布
- javascript - If 语句 - 电子邮件验证/检查
- angular - 使用primeNG和dataTable进行CSV导出的嵌套json对象