2017-08-09 14 views
3

作爲每Chrome版本> = 60通過任何頂部框架的導航選項的PDF視圖功能等鉻阻斷PDF視圖來新標籤

<A HREF=」data:…」> 
window.open(「data:…」) 
window.location = 「data:…」 

已被阻止由谷歌討論可在Google Groups找到。現在的問題是如何在網上顯示PDF,而不用明確或強制使PDF下載。我以前的代碼看上去如下經由window.open以查看PDF數據

dataFactory.getPDFData(id, authToken) 
.then(function(res) { 
    window.open("data:application/pdf," + escape(res.data)); 
},function(error){ 
    //Some Code 
}).finally(function(){ 
    //Some Code 
}); 

在上述我提取服務器的PDF數據並顯示它。但由於window.open被Chrome屏蔽,並被here專家之一建議使用來打開PDF數據,我嘗試過,但它不起作用。它總是說無法加載PDF數據如下

enter image description here

爲更新的JS代碼如下如下:

dataFactory.getPDFData(id, authToken) 
.then(function(res) { 
    $scope.pdfData = res.data; 
},function(error){ 
    //Some Code 
}).finally(function(){ 
    //Some Code 
}); 

和HTML看起來如下:

<iframe src="data:application/pdf;base64,pdfData" height="100%" width="100%"></iframe> 

如何繼續並恢復原始PDF視圖功能?我搜索了其他堆棧問題,但運氣不好,如何解決這個問題。可能是我做了錯誤的事情或錯過了iframe代碼,但它沒有解決。

+0

與jsPdf庫有類似的問題。 Iframe解決了我的問題。可以在新窗口中打開PDF,但我無法下載。 – slotomo

回答

1

無法找到想要的結果後,我想出了以下方法來解決問題。 而不是在新的頁面上打開PDF我做的只要用戶點擊打印按鈕PDF文件自動下載。以下是相同的來源。

//File Name 
var fileName = "Some File Name Here"; 

var binaryData = []; 
binaryData.push(serverResponse.data);  //Normal pdf binary data won't work so needs to push under an array 

//To convert the PDF binary data to file so that it gets downloaded 
var file = window.URL.createObjectURL(new Blob(binaryData, {type: "application/pdf"})); 
var fileURL = document.createElement("fileURL"); 
fileURL.href = file; 
fileURL.download = serverResponse.name || fileName; 
document.body.appendChild(fileURL); 
fileURL.click(); 

//To remove the inserted element 
window.onfocus = function() {      
    document.body.removeChild(fileURL) 
} 
0

在你的舊代碼: 「數據:應用程序/ PDF,」 +逃生(res.data)

在新: 您的IFRAME SRC就像是 「數據:應用程序/ PDF格式; BASE64,pdfData」

嘗試從src中刪除base64,它似乎已經存在於'pdfdata'的值中。