1. 前言
- 預(yù)覽和打印其實(shí)一樣
2. 首先注意請(qǐng)求頭這塊的配置
export function handPrint(data) {
return request({
url: CommonDataAPI.XXurl,
method: "get",
params: data,
responseType: "blob"
});
}
responseType: "blob"
- 具體和自己的服務(wù)器人員溝通
3. pdf預(yù)覽
- PDF預(yù)覽 打開新頁面 二進(jìn)制文件流
pdfViewBlob(res, fileName) {
if (!res.data || res.data.size == 0) {
newMessage.error("解析數(shù)據(jù)為空博杖!");
return;
}
if (isIE()) {
// 如果是ie瀏覽器下載到本地
let blob = new Blob([res.data], {
type: "application/octet-stream;charset=utf-8",
});
window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {
// 如果不是ie瀏覽器,新窗口打開預(yù)覽文件饮寞,自行下載
let pdfUrl = "";
const binaryData = [];
binaryData.push(res.data);
//獲取blob鏈接
pdfUrl = window.URL.createObjectURL(
new Blob(binaryData, { type: "application/pdf" })
);
let newWindow = window.open();
newWindow.location.href = pdfUrl;
}
},
4. 打印
// 打印
async handlePrint(row) {
let vm = this;
let params = {
key:val,
}
const res = await handPrint(params);
if (!(res && res.data)) {
return this.$message.error("數(shù)據(jù)獲取失敗");
}
if (res.status == 200) {
let fileReader = new FileReader();
fileReader.readAsText(res.data);
fileReader.onload = function (result) {
try {
let jsondata = JSON.parse(result.target.result);
if (jsondata.code != 0) {
vm.$message.error(jsondata.msg);
return;
}
} catch {
// vm.$printFn.pdfViewBlob(res, fileName + ".pdf");
vm.$printFn.pdfViewBlob(res, "xxx.pdf");
}
};
} else {
// this.$message.error("數(shù)據(jù)解析失敗");;
}
},
參考資料
初心
我所有的文章都只是基于入門,初步的了解岖寞;是自己的知識(shí)體系梳理,如有錯(cuò)誤,道友們一起溝通交流;
如果能幫助到有緣人,非常的榮幸,一切為了部落
的崛起;
共勉