1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| <html lang="zh-CN"> <head> <meta charset="utf-8"/> <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"/> <title>打印测试页面</title> <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/print-js/1.6.0/print.min.js"></script> </head> <body> <h1>打印测试页面</h1> <h2>这是主页面的内容....</h2> <h2>这是主页面的内容....</h2> <h2>这是主页面的内容....</h2> <div> <button onclick="print1()">打印1(使用print-js插件)</button> <button onclick="print2()">打印2(自己实现)</button> </div> <h2>这是主页面的内容....</h2> <h2>这是主页面的内容....</h2> <h2>这是主页面的内容....</h2> <script>
const url = 'https://image.dqv5.com/files/Alibaba_Java_Coding_Guidelines.pdf';
function print1() { printJS(url); }
function print2() { axios({ url: url, headers: { "Content-type": "application/pdf" }, responseType: "arraybuffer", }).then(res => { const data = res.data; var pdfFile = new Blob([data], { type: "application/pdf" }); var pdfUrl = URL.createObjectURL(pdfFile); myPrint(pdfUrl); }); }
function myPrint(url) { var iframe = this._printIframe; if (!this._printIframe) { iframe = this._printIframe = document.createElement('iframe'); document.body.appendChild(iframe);
iframe.style.display = 'none'; iframe.onload = function () { setTimeout(function () { iframe.focus(); iframe.contentWindow.print(); }, 1); }; } iframe.src = url; }; </script> </body> </html>
|