html - Flying Saucer - 从 pdf 链接打开附件

标签 html pdf pdf-generation flying-saucer

我想知道是否有办法创建链接(使用 HTML)来打开嵌入在 pdf 文档中的附件。

类似这样的事情...

<a href="attachment.pdf">Open the attachment file</a> or <a href="file:///attachment.pdf">Open the attachment file</a>

enter image description here

有什么建议或推荐吗?

谢谢

最佳答案

在阅读下一篇有用的文章后,我已经能够实现这个场景

https://groups.google.com/forum/#!topic/flying-saucer-users/KuwPoTjaQYU

步骤:

  1. 查看下一个存储库 https://github.com/osnard/flyingsaucer
  2. 分析文件 ITextOutputDevice.java ,方法 processLink(RenderingContext c, Box box)
  3. 根据您的需求进行更改
  4. 构建项目并使用生成的 jar 文件

这里是基于 Base64 内容创建嵌入文件的代码。

Java

...String doEmbedFile = handler.getAttributeValue( elem, "data-fs-embed-file" );
    if ("true".equals(doEmbedFile.toLowerCase())) {
        String fileName = new File(uri).getName();
        String content = handler.getAttributeValue( elem, "content" );

        com.lowagie.text.Rectangle targetArea = checkLinkArea(c, box);
        if (targetArea == null) {
            return;
        }
        try {
            if (!_attachments.contains(fileName)) {
                byte[] fileBytes = Base64.getDecoder().decode(content.getBytes("UTF-8")); 
                PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(_writer, null, fileName, fileBytes);
                fs.addDescription(fileName, true);
                _writer.addFileAttachment(fs);
                _attachments.add(fileName);
            }
            targetArea.setBorder(0);
            targetArea.setBorderWidth(0);

            //This only works on Adobe Acrobat Reader
            PdfAction action = PdfAction.javaScript(
                "this.exportDataObject({cName:\"" + fileName + "\", nLaunch:2});",
                _writer
            );...

HTML

<body><div id='div1'><p><a href='test.png' data-fs-embed-file='true' content='iVBORw0KGgoAAAANSUhEU...'>open test.png file</a></p></div><div id='div2'><p><a href='test.pdf' data-fs-embed-file='true' content='JVBERi0xLjUNCiW1tbW1D...'>open test.pdf file</a></p></div><div id='div3'><p><a href='test.txt' data-fs-embed-file='true' content='VEVFRUVFRUVFRVNUIFRYVA=='>open test.txt file</a></p></div></body>

*base64 内容被截断

输出

enter image description here

问候,我希望它可以帮助其他人

关于html - Flying Saucer - 从 pdf 链接打开附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48258914/

相关文章:

html - 调整 html 复选框的大小以用于 pdf 输出

iphone - IOS的密码认证如何设计和实现?

javascript - 如何避免下载整个 PDF 来显示

javascript - Rails 3 wicked_pdf 和分页

javascript - jQuery 验证不适用于输入数组

javascript - 带有下拉列表的搜索栏

MATLAB:以黑白打印 pdf

html - iTextSharp - 将一段 HTML 输出到 PDF(到表格单元格中)

html - 是否可以将 HTML 属性应用于 CSS 中的 Div 标签?

html - 短/详细按钮