Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript HTML嵌入PDF所有链接覆盖以在新选项卡中打开(目标=“U blank”)_Javascript_Html_Pdf_Powerpoint - Fatal编程技术网

Javascript HTML嵌入PDF所有链接覆盖以在新选项卡中打开(目标=“U blank”)

Javascript HTML嵌入PDF所有链接覆盖以在新选项卡中打开(目标=“U blank”),javascript,html,pdf,powerpoint,Javascript,Html,Pdf,Powerpoint,我目前有一个PDF嵌入到一个网页。PDF中有多个超链接,但单击时链接会在父框架中打开。这会将用户带到一个新页面,但没有返回原始PDF的选项(导航已关闭)。我似乎不知道如何在新窗口中打开链接 样本PDF <embed src="https://www.antennahouse.com/XSLsample/pdf/sample-link_1.pdf" type="application/pdf" /> 问题 单击此PDF上的第二个(外部)链接将导航到 同一选项卡中的另一个网站 工作

我目前有一个PDF嵌入到一个网页。PDF中有多个超链接,但单击时链接会在父框架中打开。这会将用户带到一个新页面,但没有返回原始PDF的选项(导航已关闭)。我似乎不知道如何在新窗口中打开链接

样本PDF

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

问题

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

工作

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

如果没有,我想知道在html代码中是否有某种东西可以普遍控制链接的打开方式

欢迎提出任何建议


谢谢。

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

为了快速确定这个答案,这应该适用于现代浏览器,并且如果PDF和PDFJ托管在您嵌入它的同一个域上,则只适用于浏览器

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

您需要按照“强制”使用的示例来设置站点。这里的资源:

您还需要在Web服务器上运行它,因为它不能单独从文件系统上正确地运行。。万岁!更多的CORS问题

然后,您将设置您的页面,并像这样调用它(基于@Paco的要点)


测试pdf
变量选项={
pdfOpenParams:{
页码:1,
视图:“适合”,
工具栏:0
},
forcePDFJS:true,//***强制使用PDF.js而不是默认行为
PDFJS_URL:“web/viewer.html”//***需要使用PDF.js
};
嵌入(“../pdf test.pdf”,“pdf”,选项);
document.querySelector(“#pdf iframe”).onload=function(){
//可以尝试挂接PDF.js事件以完成渲染,然后调用它。
//https://stackoverflow.com/questions/12693207/how-to-know-if-pdf-js-has-finished-rendering
setTimeout(函数(){
document.querySelector('#pdf iframe').contentDocument.queryselectoral('a:not(.bookmark')).forEach(函数(a,i){
a、 setAttribute('target','u blank');
})
}, 5000)
}

是否有任何通用方法来使用
app.launchURL(“http://www.yourlink.com“,对)
对于html页面中的所有链接,是否包含iframe或嵌入对象?问题是在html/CSS/JavaScript中寻找通用方法来覆盖嵌入PDF的链接目标。用Acrobat编辑PDF不是一个可以接受的答案。@Paco你能不能找到一个可以工作的代码笔或其他东西,原来的代码笔似乎因为CORsThanks@MattiPrice而失效,下面是一个HTML的要点,可以保存到文件中,然后加载到大多数浏览器中,以显示示例:
<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>