飞碟 - 从pdf链接打开附件

时间:2018-01-15 07:58:11

标签: 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

有任何建议或建议吗?

由于

2 个答案:

答案 0 :(得分:1)

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

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文件
  5. 这里是基于base64内容创建嵌入式文件的代码。

    <强>爪哇

    ...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

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

答案 1 :(得分:0)

只需在新标签页上打开它,在锚标记中添加属性target =“_ blank”

<a href="http://link.to/file.pdf" target="_blank">Open attachment</a>
相关问题