为 PDF 文档中的嵌入文件创建“下载”锚点

问题描述

我的目标是输出一个尽可能接近网页的 PDF 文档。

我正在使用 openhtmltopdf(Apache 的 PDF Box 的叠加层)构建带有 Thymeleaf 模板的文档。

这是在 Firefox PDF 查看器中看到的生成文档的示例:

Example of generated document

本文档本身包含用户提供的内容(其他 PDF 文档、图片等)。由于 openhtmltopdf 不(据我所知)支持附件,因此我必须在构建文档后重新打开它,并使用 PDF Box documentation 提供的示例的变体来嵌入它们:

private void embedAllDocuments(PDDocument pdf,List<Document> docs) {
    final PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
    final Map<String,PDComplexFileSpecification> efMap = new HashMap<>();
    for (final Document doc : docs) {
        final Fichier fichier = doc.getFichier();
        final String path = fichierService.getAbsolutePath(fichier).toString();
        final String name = fichier.getIdFichier() + "_" + fichier.getNom();
        // first create the file specification,which holds the embedded file
        final PDComplexFileSpecification fs = new PDComplexFileSpecification();
        fs.setFile(name);
        fs.setFileUnicode(name);
        // set some of the attributes of the embedded file
        try {
            final PDEmbeddedFile ef = new PDEmbeddedFile(pdf,new FileInputStream(path));
            ef.setSubtype(fichierService.getMimeTypeFromExtension(fichier));
            ef.setSize(fichier.getTaille().intValue());
            ef.setCreationDate(DateUtils.toCalendar(fichier.getInstantDepot()));
            fs.setEmbeddedFile(ef);
        } catch (final IOException e) {
            throw new TechnicalException(ErrTechnicalResource.FILE_IO_FAILURE);
        }
        // Now add the entry to the embedded file tree and set in the document
        efMap.put("fic_" + fichier.getIdFichier(),fs);
    }
    efTree.setNames(efMap);
    // attachments are stored as part of the "names" dictionary in the catalog
    final PDDocumentNameDictionary names = new PDDocumentNameDictionary(pdf.getDocumentCatalog());
    names.setEmbeddedFiles(efTree);
    pdf.getDocumentCatalog().setNames(names);
}

这导致文件被附加并在文档中被带有唯一标识符的死文本引用:

Embedded files in the document

它可以工作,但非常不方便。

我想为这些创建“下载”超链接,类似于允许导航到文档各个部分的摘要锚点。因此,用户只需点击链接即可访问(“下载”)相应的附件。

我假设使用 openhtmltopdf 不可能实现该结果(尤其是因为它本身似乎不支持附件)。出于许可原因,我不想使用 iText。

所以我最好的做法是:

  1. 使用 openhtmltopdf 生成文档。
  2. 使用 PDF Box 重新打开文档。
  3. 添加附件文件
  4. 解析文档(大概使用 PDF Box)。
  5. 找到死文本文件名。
  6. 名称替换为指向其各自附件的有效超链接

我需要步骤 (5) 的帮助。 我不知道如何查看完整的 PDF 文档中的特定文本。

我应该能够在步骤 (6) 中创建超链接,但我不确定如何用它实际替换现有文本。


issue 中所述,实际上现在(自 2021 年 3 月起)可以使用 download 上的 <a> 属性直接嵌入带有 openhtmltopdf文件}} 标签。它需要额外的调整,因为您必须为构建器定义自定义资源访问控制器,但不再需要通过 PDF Box

我在他们的 wiki 上添加corresponding article 以及更多详细信息。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...