[英]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.