如何解决为 PDF 文档中的嵌入文件创建“下载”锚点
我正在使用 openhtmltopdf(Apache 的 PDF Box 的叠加层)构建带有 Thymeleaf 模板的文档。
这是在 Firefox PDF 查看器中看到的生成文档的示例:
本文档本身包含用户提供的内容(其他 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);
}
这导致文件被附加并在文档中被带有唯一标识符的死文本引用:
它可以工作,但非常不方便。
我想为这些创建“下载”超链接,类似于允许导航到文档各个部分的摘要锚点。因此,用户只需点击链接即可访问(“下载”)相应的附件。
我假设使用 openhtmltopdf 不可能实现该结果(尤其是因为它本身似乎不支持附件)。出于许可原因,我不想使用 iText。
所以我最好的做法是:
- 使用 openhtmltopdf 生成文档。
- 使用 PDF Box 重新打开文档。
- 添加附件文件。
- 解析文档(大概使用 PDF Box)。
- 找到死文本文件名。
- 将名称替换为指向其各自附件的有效超链接。
我需要步骤 (5) 的帮助。 我不知道如何查看完整的 PDF 文档中的特定文本。
我应该能够在步骤 (6) 中创建超链接,但我不确定如何用它实际替换现有文本。
如 issue 中所述,实际上现在(自 2021 年 3 月起)可以使用 download
上的 <a>
属性直接嵌入带有 openhtmltopdf 的文件}} 标签。它需要额外的调整,因为您必须为构建器定义自定义资源访问控制器,但不再需要通过 PDF Box。
我在他们的 wiki 上添加了 corresponding article 以及更多详细信息。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。