首页 > 解决方案 > 使用 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");
    }

我真的不知道这意味着什么以及如何解决它。最奇怪的是,我创建了全新的项目并尝试使用相同的代码来合并两个文档(相同的文档)并且它可以工作!

此外,我尝试过的是:

  1. 如果没问题就换spring boot版本
  2. 像这样设置 mergeDocuments 方法:pdfMergerUtility.mergeDocuments(setupMainMemoryOnly())
  3. 像这样设置 mergeDocuments 方法:pdfMergerUtility.mergeDocuments(setupTempFileOnly())
  4. 使用不使用 java.nio 中的文件的不同方法获取字节:
  5. 并且还在不同的线程中执行了这个
  6. 合并仅本地存储的文件(在资源中)
  7. 合并我从另一项服务获得的文件 - 顺便说一句,这就是为什么我确信他没问题

有人能帮忙吗?

标签: javaspring-bootpdfpdfbox

解决方案


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 对象(甚至是整个文件而不是字节列表)。


推荐阅读