利用vue实现打印页面的几种方法总结
作者:啊啊啊林 发布时间:2023-07-02 17:09:48
标签:vue,打印页面
1.第一种就是直接调用 window.print()方法
这种方法的坏处就是 默认打印整个页面,不能打印局部页面。
2.第二种使用v-print
首先先下载 npm install vue-print-nb --save
然后在main.js中引入 main.js中引入
import Print from 'vue-print-nb'
Vue.user(Print)
由于我用的是jeecg所以和你们不太一样
然后就是在代码里面用了
<div class='preview_content2' id="print" ref="print">
//以是要打印的内容
<div style='text-align: center;margin-top: 100px;'>
<a-button type='primary' size="large" v-print=""#print"" style="width: 150px">
打印
</a-button>
</div>
//打印结束
</div>
点击上面那个打印按钮 就会自动打印你想要的部分了
3.第三种使用print.js
使用这种的好处就是可以在打印前进行一些自己想要的操作 比如校验
第一步先下载print.js
我这边直接把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 = this.isDOM(dom) ? dom : dom.$el;
}
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 = 0; k < inputs.length; k++) {
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)
} else {
inputs[k].setAttribute('value', inputs[k].value)
}
}
for (var k2 = 0; k2 < textareas.length; k2++) {
if (textareas[k2].type == 'textarea') {
textareas[k2].innerHTML = textareas[k2].value
}
}
for (var k3 = 0; k3 < selects.length; k3++) {
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;";
iframe.setAttribute('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();
var _this = this
iframe.onload = function(){
_this.toPrint(w);
setTimeout(function () {
document.body.removeChild(iframe)
}, 100)
}
},
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
然后把js放在自己的目录中
我的目录是这样子的
第二步在main.js中引入
import Print from '@/utils/printTest'
// Vue.use(Print)
最后就是在页面中使用了
来源:https://blog.csdn.net/dhjsma/article/details/124741536
0
投稿
猜你喜欢
- 一、前言html和css的学习大致完成,我们进入重要的JavaScript学习阶段二、什么是JavaScript,有什么用?Javascri
- 帮网友小改了一下主题. 任务比较简单, 只是为一个三栏主题添加对 Widget 的支持而已,就先从这次简单的案例开始说吧.单侧边栏funct
- Python 读取WAV文件import waveimport structfrom scipy import *from pylab im
- 在进行matplotlib画图的时候,经常会出现这个的报错,虽然知道是因为没有对应的字体的原因,但是,将字体下载后放到目标路径下,仍然没有办
- 本文主要是对leveldb进行一个简单的介绍及使用Python语言对其进行操作的代码示例,具体如下。leveldb 是google实现的一种
- Python 提供了如下两个函数来检查类型:issubclass(cls, class_or_tuple):检查 cls 是否为后一个类或元
- http协议是无状态的。下一次去访问一个页面时并不知道上一次对这个页面做了什么。无状态的应用层面的原因是:浏览器和服务器之间的通信都遵守HT
- 为了访问数据库,就要提供数据库连接类,在C#中,是通过Connection类来实现的四种类型的连接方式SQLConnectionADOCon
- 本文实例讲述了Python基于pyCUDA实现GPU加速并行计算功能。分享给大家供大家参考,具体如下:Nvidia的CUDA 架构为我们提供
- eval函数就是实现list、dict、tuple与str之间的转化str函数把list,dict,tuple转为为字符串一、字符串转换成列
- 最近在研究深度学习视觉相关的东西,经常需要写python代码搭建深度学习模型。比如写CNN模型相关代码时,我们需要借助python图像库来读
- 一、数字类型。数字类型按照我的分类方法分为三类:整数类、小数类和数字类。 我所谓的“数字类”,就是指DECIMAL
- 组件值校验,即在组件值变化时判断是否满足校验逻辑,若不满足校验逻辑,可以拿到校验错误信息进行错误提示或其他逻辑处理。声明 valueVali
- 与Python标准库模块命名冲突Python的一个优秀的地方在于它提供了丰富的库模块。但是这样的结果是,如果你不下意识的避免,很容易你会遇到
- 本文实例讲述了python监控网站运行异常并发送邮件的方法。分享给大家供大家参考。具体如下:这是一个简单的python开发的监控程序,当指定
- 第二次遇到FF下正则兼容性问题( 第一次是关于"g"全局标志的,现在网上已有很多相关文章介绍)。以下正则在FF和IE下的
- 这个是JS控制图片滚动的效果,当鼠标结果新闻标题时开始滚动到对应的图片,可以作为图片新闻。效果图:<!DOCTYPE HTML PUB
- 我用的是mac mini然后在上边装好了sublime text2 插件依然是go sublime和go build这两个就不介绍了,因为之
- Stickyworld 的网页应用已经支持视频拨放一段时间,但都是通过YouTube的嵌入模式实现。我们开始提供新的版本支持视频操作,可以让
- 介绍PaddleOCR 是一个基于百度飞桨的OCR工具库,包含总模型仅8.6M的超轻量级中文OCR,单模型支持中英文数字组合识别、