繁体   English   中英

HTML嵌入式PDF所有链接覆盖以在新选项卡中打开(target =“_ blank”)

[英]HTML embedded PDF all links override to open in a new tab (target=“_blank”)

我目前在网页中嵌入了PDF。 PDF中包含多个超链接,但单击时链接在父框架中打开。 这会将用户带到一个新页面,但不能选择返回原始PDF(导航已关闭)。 我似乎无法弄清楚如何在新窗口中打开链接。

样本PDF

<embed src="https://www.antennahouse.com/XSLsample/pdf/sample-link_1.pdf" type="application/pdf" />

问题

单击此PDF上的第二个( 外部 )链接将导航到同一选项卡中的另一个网站。

工作Plunkr

PDF文档最初是在PowerPoint中创建的,这使我无法添加正确的href属性。 有没有办法修改PDF中的链接以包含target =“_ blank”?

如果没有,我想知道是否有一些我可以包含在html代码中的东西,它将普遍控制链接如何打开。

欢迎任何建议。

谢谢。

只是为了快速确定这个答案,这应该适用于现代浏览器,并且只有当PDF和PDFJS托管在您嵌入它的同一域中时。

这里的诀窍是强制使用PDF.js并覆盖Chrome的默认行为,将其呈现为扩展名。 这样你就得到一个带有html元素的iframe,你可以操作, 如果你不尝试做CORS 如果这是一个与CORS相关的用例,那么你很幸运,因为编辑CORS pdf是一种安全风险并且被理所当然地禁止使用。

您将首先想要按照“强制”使用示例设置站点。 这里的资源:

https://github.com/mozilla/pdf.js/wiki/Setup-PDF.js-in-a-website https://pdfobject.com/examples/pdfjs-forced.html

你也需要在网络服务器上运行它,因为它不能单独从文件系统中正确服务。万岁更多的CORS问题。

然后,你将设置你的页面并像这样调用它(基于@Paco的主旨)

<html>
<head>
    <title>test pdf</title>
</head>
<div id="pdf"
     style="width:900px; height:500px"></div>
<script src="https://pdfobject.com/js/pdfobject.min.js"></script>
<script>
    var options = {
        pdfOpenParams: {
            page: 1,
            view: "Fit",
            toolbar: 0
        },
        forcePDFJS: true, //*** Forces the use of PDF.js instead of default behavior
        PDFJS_URL: "web/viewer.html" //*** Required to use PDF.js
    };
    PDFObject.embed("../pdf-test.pdf", "#pdf", options);

    document.querySelector('#pdf iframe').onload = function () {
        //can try and hook the PDF.js event for rendering completed and call it then instead. 
        //https://stackoverflow.com/questions/12693207/how-to-know-if-pdf-js-has-finished-rendering
        setTimeout(function () {
            document.querySelector('#pdf iframe').contentDocument.querySelectorAll('a:not(.bookmark)').forEach(function (a, i) {
                a.setAttribute('target', '_blank');
            })
        }, 5000)

    }
</script>
</body>
</html>

使用Adobe Acrobat右键单击链接属性。 单击“操作”选项卡。 如果列出了任何操作,请删除它们,然后选择Run a Javascript。 单击添加。 将出现一个框,供您添加以下javascript。 app.launchURL(“ http://www.yourlink.com ”,true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM