vue使用pdf.js預(yù)覽pdf文件的方法
我們在頁面進(jìn)行pdf預(yù)覽的時候,由于有些文件不能夠進(jìn)行打印和下載,這時候我們使用window自帶的pdf就很難滿足客戶的需求,因此需要另外的辦法來支持我們進(jìn)行特殊條件的pdf文件預(yù)覽,這里我采用引入pdf.js文件的形式來達(dá)到目的。
第一步:下載pdf.js
引入pdf.js文件
地址如下:http://mozilla.github.io/pdf.js/getting_started/

第二步,vue引入
我這里是把下載下來的文件放在了根目錄的piblic下

第三步,使用
主要是地址"/build/generic/web/viewer.html?file="+href,href為請求后端返回的文件路徑或者后端返回文件流前段解析后生成的文件路徑,前面拼接上/build/generic/web/viewer.html?file=,地址為自己引入pdf.js文件的路徑,也可以直接這樣寫/build/generic/web/viewer.html,他會直接去找。下面我使用的是前段解析文件流生成地址預(yù)覽的。
預(yù)覽方法window.open("/build/generic/web/viewer.html?file="+href);
axios({
method: 'get',
url:url,
headers: {
"token":auth,
},
responseType: 'blob',
}).then(response => {
type_ = type_.toLowerCase();
if (type_ == "docx") {
that._type_ = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
} else if (type_ == "doc") {
that._type_ = "application/msword"
} else if (type_ == "gif") {
that._type_ = "image/gif"
} else if (type_ == "jpeg" || type_ == "jpg") {
that._type_ = "image/jpeg"
} else if (type_ == "png") {
that._type_ = "image/png"
} else if (type_ == "pdf") {
that._type_ = "application/pdf"
} else if (type_ == "txt") {
that._type_ = "text/plain;charset=utf-8'"
} else if (type_ == "xls") {
that._type_ = "application/vnd.ms-excel"
} else if (type_ == "xlsx") {
that._type_ = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
}else if (type_ == "zip") {
that._type_ = "application/zip"
} else if (type_ == "ppt") {
that._type_ = "application/vnd.ms-powerpoint"
} else if (type_ == "pptx") {
that._type_ = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
}
if(type_ == "pdf"){
var blob = new Blob([response.data], { type: that._type_ })
var href = window.URL.createObjectURL(blob); //創(chuàng)建下載的鏈接
window.open("/build/generic/web/viewer.html?file="+href);
}
})
最后
如果你想要的禁掉pdf文件的下載、打印等功能,最簡單的方法是,找的自己導(dǎo)入文件里的viewer.html,路徑為build下的generic文件夾下的web里的viewer.html,如下:

在這個html里找到對應(yīng)下載的dom直接display:none就可以,切記不可以注掉,注掉會報錯。如下,紅色框中,一個是下載一個是打印,直接隱藏就可以。

如果有人問這樣也不安全,那可以和客戶商量不在頁面展示,因為只要頁面可以看到的東西,截屏也可以截下來,注定是不安全的。
到此這篇關(guān)于vue使用pdf.js來進(jìn)行pdf預(yù)覽的文章就介紹到這了,更多相關(guān)vue使用pdf.js預(yù)覽pdf內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺析Vue2和Vue3中雙向綁定機(jī)制的優(yōu)化與差異
Vue.js?核心特性之一就是雙向綁定,本文將深入探討?Vue2?和?Vue3?在雙向綁定上的區(qū)別,并分析這些改進(jìn)對性能和開發(fā)體驗的影響,希望對大家有所幫助2024-11-11
Element Notification通知的實現(xiàn)示例
這篇文章主要介紹了Element Notification通知的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
vue大型項目之分模塊運(yùn)行/打包的實現(xiàn)
這篇文章主要介紹了vue大型項目之分模塊運(yùn)行/打包的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

