add pdf预览功能demo

This commit is contained in:
路 范
2021-09-16 17:00:05 +08:00
parent 80d9a8c08b
commit 1165188074

View File

@@ -15,13 +15,15 @@
import pdf from "vue-pdf";
import jquery from "jquery";
import html2canvas from "html2canvas";
import axios from "axios";
import request from "@/common/util";
export default {
components: {
pdf
pdf,
},
data() {
return {
url: ""
url: "",
};
},
created() {
@@ -30,7 +32,7 @@ export default {
},
mounted() {
// 截图
html2canvas(this.$refs.content).then(canvas => {
html2canvas(this.$refs.content).then((canvas) => {
canvas.id = "mycanvas";
const base64 = canvas.toDataURL("image/png");
jquery("a").prop("href", base64);
@@ -39,12 +41,74 @@ export default {
methods: {
onInit() {
this.onLoadData();
request({
url: "http://localhost:5566/agreement/info",
method: "post",
data: {
ticket: "8a1188557bda7894017bed584f803119-ticket",
id: "C655515B-FD91-48F2-BD79-EAABAFB7077C",
},
headers: { "Content-Type": "application/json; charset=UTF-8" },
responseType: "arraybuffer", //一定要设置响应类型否则页面会是空白pdf
}).then((result) => {
const binaryData = [];
binaryData.push(result.data);
//获取blob链接
this.url = window.URL.createObjectURL(
new Blob(binaryData, { type: "application/pdf" })
);
// if (pdfUrl) {
// this.handlePrint(pdfUrl);
// }
});
// axios({
// method: "post",
// url: "http://10.19.94.9:7099/api/agreement/info",
// responseType: "blob",
// data: {
// ticket: "8a1188557bda7894017bed584f803119-ticket",
// id: "C655515B-FD91-48F2-BD79-EAABAFB7077C",
// },
// }).then((res) => {
// debugger;
// console.log(res);
// if (!res) {
// return;
// }
// let blob = new Blob([res.data], {
// type: "application/octet-stream",
// });
// let url = window.URL.createObjectURL(blob);
// let link = document.createElement("a");
// link.style.display = "none";
// link.href = url;
// document.body.appendChild(link);
// link.click();
// });
},
handlePrint(pdf) {
if (document.getElementById("print-iframe")) {
document.body.removeChild(document.getElementById("print-iframe"));
}
//判断iframe是否存在不存在则创建iframe
let iframe = document.getElementById("print-iframe");
if (!iframe) {
iframe = document.createElement("IFRAME");
let doc = null;
iframe.setAttribute("src", pdf);
iframe.setAttribute("id", "print-iframe");
document.body.appendChild(iframe);
doc = iframe.contentWindow.document;
doc.close();
iframe.contentWindow.focus();
}
iframe.contentWindow.print();
},
onLoadData() {
console.log(this.$route.params);
this.url = this.$route.params.url;
}
}
// console.log(this.$route.params);
// this.url = this.$route.params.url;
},
},
};
</script>
<style lang="less" scoped>