当前位置: 代码网 > it编程>编程语言>Javascript > vue3中使用print-js组件实现打印操作步骤

vue3中使用print-js组件实现打印操作步骤

2025年02月13日 Javascript 我要评论
第一步:安装依赖yarn add print-js第二步:创建打印组件:printhtmlcomp.vue<template><div id="printarea_123456789

第一步:安装依赖

yarn add print-js

第二步:创建打印组件:printhtmlcomp.vue

<template>
	<div id="printarea_123456789">
		<!--  默认插槽,传入打印内容    -->
		<slot></slot>
	</div>
</template>
<script setup lang="ts">
import printjs from 'print-js';
defineoptions({ name: 'printhtmlcomp' });
const props = defineprops({
	fontsize: {
		type: number,
		default: 14,
		required: false,
	},
});
const handleprint = () => {
	printjs({
		printable: 'printarea_123456789', // 打印区域的id
		type: 'html',
		scanstyles: true,
		targetstyles: ['*'], // 使用dom的所有样式,很重要
		font_size: props.fontsize + 'px', // 字体大小 --很重要,这里字体要和css中字体大小保持一致,默认是16px
	});
};
defineexpose({ handleprint });
</script>

 第三步:处理打印预览的界面 printpop.vue

这个界面的的样式调整直接关系到打印的样式的,即想要打印什么样的效果就在这个界面怎么调试。

 
	<el-dialog v-model="state.dialogvisible" :destroy-on-close="true" :title="state.title" width="780px" @close="close">
		<printhtmlcomp ref="printref">
            <!---这里是想要打印的-->
			<div style="font-size: 20px">hello world</div>
			<div style="font-size: 20px">hello world</div>
			<div style="font-size: 20px">hello world</div>
			<div style="font-size: 20px">hello world</div>
			<div style="font-size: 20px">hello world</div>
			<div style="font-size: 20px">hello world</div>
			<div style="font-size: 20px">hello world</div>
		</printhtmlcomp>
		<template #footer>
			<el-button @click="close">取 消</el-button>
			<el-button :loading="state.saveloading" type="primary" @click="handleprint">打 印</el-button>
		</template>
	</el-dialog>
<script lang="ts" setup>
import { reactive, ref } from 'vue';
defineoptions({ name: 'workapplyplanprint' });
const state = reactive({
	dialogvisible: false,
});
const showedit = async () => {
	state.dialogvisible = true;
};
const close = () => {
	state.dialogvisible = false;
};
// 打印功能的函数
const handleprint = async () => {
	printref.value?.handleprint();
};
defineexpose({ showedit });
</script>
 

html展示效果

打印预览效果:

 完成,功德+1。

关于遇到的问题以及解决方法,后续更新!!!!

到此这篇关于vue3中使用print-js组件实现打印操作的文章就介绍到这了,更多相关vue3中使用print-js组件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com