当前位置: 首页 > article >正文

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

第一步:安装依赖

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。

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


http://www.kler.cn/a/539437.html

相关文章:

  • C++ decltype 规则推导
  • 代码随想录二刷|回溯4
  • C++并发编程指南 09(共享数据)
  • VSCode 下载与使用教程:附百度网盘地址
  • DeepSeek和ChatGPT的优劣或者区别(答案来DeepSeek和ChatGPT)
  • 使用 Postman 进行 API 测试:从入门到精通
  • 蓝桥杯51单片机练习(国信长天比赛用)
  • c/c++蓝桥杯经典编程题100道(18)括号匹配
  • Win10+Ollama+AnythingLLM+DeepSeek构建本地多人访问知识库
  • 大数据示例:改变业务的 6 种方式
  • 【虚幻引擎UE】AOI算法介绍与实现案例
  • 【C++八股】std::atomic —— 原子操作
  • ASP.NET Core 如何使用 C# 向端点发出 POST 请求
  • openAI官方prompt技巧(二)
  • 基于springboot+vue的文物管理系统的设计与实现
  • android手机安装deepseek-r1:1.5b
  • DeepSeek开源多模态大模型Janus-Pro部署
  • 在 Linux 系统下,解压 `.tar.gz`
  • 14vue3实战-----获取用户信息和用户的菜单树信息
  • 解决Redisson在Kubernetes中连接旧Redis主节点的问题
  • Vue3 进阶-自定义事件用法全解析 ✨
  • 大语言模型需要的可观测性数据的关联方式
  • LeetCode热题100-最大子数组和【JavaScript讲解】
  • webpack配置之---output.filename
  • 【DeepSeek】私有化本地部署图文(Win+Mac)
  • Windows编程:在 VS2010 里面,打开一个项目