我有一个 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 中的文件的不同方法获取字节:
- 并且还在不同的线程中执行了这个
- 合并仅本地存储的文件(在资源中)
- 合并我从另一项服务获得的文件 - 顺便说一句,这就是为什么我确信他没问题
有人能帮忙吗?