很常見的會遇到打印pdf的操作
我使用的是print.js
使用方法如下:
// 打印類屬性、方法定義 /* eslint-disable */ const Print =function(dom, options) { if (!(this instanceof Print)) return new Print(dom, options); this.options = this.extend({ 'noPrint': '.no-print' }, options); if ((typeof dom) === "string") { this.dom = document.querySelector(dom); } else { this.isDOM(dom) this.dom = dom; } this.init(); }; Print.prototype = { init: function () { var content = this.getStyle() + this.getHtml(); this.writeIframe(content); }, extend: function (obj, obj2) { for (var k in obj2) { obj[k] = obj2[k]; } return obj; }, getStyle: function () { var str = "", styles = document.querySelectorAll('style,link'); for (var i = 0; i < styles.length; i++) { str += styles[i].outerHTML; } str += "<style>" + (this.options.noPrint ? this.options.noPrint : '.no-print') + "{display:none;}</style>"; return str; }, getHtml: function () { var inputs = document.querySelectorAll('input'); var textareas = document.querySelectorAll('textarea'); var selects = document.querySelectorAll('select'); for (var k in inputs) { if (inputs[k].type == "checkbox" || inputs[k].type == "radio") { if (inputs[k].checked == true) { inputs[k].setAttribute('checked', "checked") } else { inputs[k].removeAttribute('checked') } } else if (inputs[k].type == "text") { inputs[k].setAttribute('value', inputs[k].value) } } for (var k2 in textareas) { if (textareas[k2].type == 'textarea') { textareas[k2].innerHTML = textareas[k2].value } } for (var k3 in selects) { if (selects[k3].type == 'select-one') { var child = selects[k3].children; for (var i in child) { if (child[i].tagName == 'OPTION') { if (child[i].selected == true) { child[i].setAttribute('selected', "selected") } else { child[i].removeAttribute('selected') } } } } } return this.dom.outerHTML; }, writeIframe: function (content) { var w, doc, iframe = document.createElement('iframe'), f = document.body.appendChild(iframe); iframe.id = "myIframe"; iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;"; w = f.contentWindow || f.contentDocument; doc = f.contentDocument || f.contentWindow.document; doc.open(); doc.write(content); doc.close(); this.toPrint(w); setTimeout(function () { document.body.removeChild(iframe) }, 500) }, toPrint: function (frameWindow) { try { setTimeout(function () { frameWindow.focus(); try { if (!frameWindow.document.execCommand('print', false, null)) { frameWindow.print(); } } catch (e) { frameWindow.print(); } frameWindow.close(); }, 10); } catch (err) { console.log('err', err); } }, isDOM: (typeof HTMLElement === 'object') ? function (obj) { return obj instanceof HTMLElement; } : function (obj) { return obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string'; } }; const MyPlugin = {} MyPlugin.install = function (Vue, options) { // 4. 添加實例方法 Vue.prototype.$print = Print } export default MyPlugin
以上代碼是print.js的全部內容,
在需要打印的頁面引入,如下代碼
import Vue from 'vue' import Print from './print' Vue.use(Print)
打印操作時:
<div @click="print(record)">打印處方單</div>
print() { this.$print(this.$refs.prescription)//prescription為自己命名的ref值
}
執行以上代碼即可執行打印操作。需要注意的是,若想成功打印文件,需要在ref為prescription的外邊包一層div,否則預覽時,將會看到空白頁面。
下邊來說還可能出現的問題:
預覽時會發現樣式亂掉了,那么該怎么辦呢
你可以將css代碼放在@media print{}里面
@media print{ //我只是舉例,沒多寫樣式 span:{ display:block; } }
也可以使用引入的方法
@media print { @import url('./prescription.css');//你的css路徑
但是,很快你會發現可能不生效,原因是你在使用print.js時安裝了vue-print-nb,而在使用過程中不允許要打印的元素存在父級元素,所以,我們剛才為了使打印的文件能夠預覽還包了一個div在外邊。
那么我們該怎么辦?怎么辦?
解決辦法就是:還是老老實實把這個頁面的樣式寫成行內式才算完美解決問題。