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

PC端直接打印功能(包括两张图片合并功能)

一、 效果图

二、demo代码

<template><div class="box"><divref="printContent"class="print-content"><div class="print-title">打印图片</div><imgclass="preview-image":src="mergedImage"alt="Merged Image"/></div><div class="btn" @click="handlePrint">打印</div></div></template><script lang="ts" setup>
import { onMounted, ref } from 'vue';
import image1 from '../../assets/images/cs.jpg'
import image2 from '../../assets/images/huang1.png'
const printContent = ref();
const codeImg = ref()
const photoUrl = ref()
const mergedImage = ref(null);
onMounted(() => {mergeImages()
})
// 打印
const handlePrint = () => {return new Promise((resolve) => {// 创建隐藏的iframeconst iframe = document.createElement('iframe');iframe.style.position = 'fixed';iframe.style.right = '0';iframe.style.bottom = '0';iframe.style.width = '0';iframe.style.height = '0';iframe.style.border = '0';// 插入DOMdocument.body.appendChild(iframe);// 获取打印内容const content = `<html><head><title>打印</title><style>@page { margin: 0; }body { margin: 1cm; }img { max-width: 100%!important; height: auto!important;}</style></head><body>${printContent.value.innerHTML}</body></html>`;// 写入内容const doc = iframe.contentWindow.document;doc.open();doc.write(content);doc.close();// 打印完成后清理iframe.contentWindow.onafterprint = () => {document.body.removeChild(iframe);closeModal();resolve();};// 触发打印setTimeout(() => {iframe.contentWindow.focus();iframe.contentWindow.print();}, 500);});
};
// 合并两张图片
const mergeImages = async () => {const img1 = new Image();const img2 = new Image();img1.src = image1img2.src = image2await new Promise((resolve) => {img1.onload = resolve;});await new Promise((resolve) => {img2.onload = resolve;});const canvas = document.createElement('canvas');canvas.width = img1.width;canvas.height = img1.height;const ctx: any = canvas.getContext('2d');ctx.drawImage(img1, 0, 0);// 假设头像位置在左上角,你可以根据需要调整位置// const avatarSize = 300; // 头像大小ctx.drawImage(img2, 230, 30, 425, 425);  // 距离左边位置,距离上边位置,图片宽度,图片高度mergedImage.value = canvas.toDataURL('image/png');
};
</script>
<style lang="scss" scoped>
.box {width: 100%;height: 100%;box-sizing: border-box;padding: 20px;
}
@media print {/* 打印时隐藏所有内容 */body * {visibility: hidden !important;}/* 显示打印区域 */.print-content,.print-content * {visibility: visible !important;}/* 定位到页面顶部 */.print-content {position: absolute;left: 0;top: 0;width: 100% !important;}
}
.print-title {text-align: left;font-size: 28px;
}
.preview-image {width: 500px;
}
.btn{width: 80px;height: 44px;line-height: 44px;text-align: center;background-color: rgb(56, 68, 240);color: #fff;cursor: pointer;
}
</style>
http://www.xdnf.cn/news/12622.html

相关文章:

  • html 滚动条滚动过快会留下边框线
  • 2022年CSP-S1提高级第一轮题解
  • C++11新增标准讲解(上)
  • 【递归、搜索与回溯】专题三 穷举vs暴搜vs回溯vs剪枝
  • 【Vue】指令补充+样式绑定+计算属性+侦听器
  • 6.6 打卡
  • 西门子 S7-1200 PLC 海外远程运维技术方案
  • vue3+TS+eslint9配置
  • 《强连通分量》题集
  • 如何在Windows本机安装Python并确保与Python.NET兼容
  • day46python打卡
  • 1.4 编译库:静态库、动态库
  • Java并发包中的管程:Lock和Condition
  • 基于STM32语音识别柔光台灯
  • 基于深度学习的无人机轨迹预测
  • 《ERP原理与应用教程》第3版习题和答案
  • VSCode - VSCode 放大与缩小代码
  • 嵌入式开发之STM32学习笔记day22
  • 深入解析:为什么 Redis 比 MySQL 快
  • 如何轻松、安全地管理密码(新手指南)
  • 创客匠人:如何通过精准定位实现创始人IP打造与知识变现
  • [C语言实战]C语言操作MySQL数据库(八)
  • Ubuntu18.6 学习QT问题记录以及虚拟机安装Ubuntu后的设置
  • 下载和安装Visual Studio(开发ASP.NET MVC应用)
  • 华为仓颉语言初识:并发编程之同步机制(上)
  • TensorFlow安装全攻略:快速搭建AI开发环境
  • 图像识别预处理(配合pytesseract使用)
  • 基于最大相邻夹角的边缘点提取(matlab)
  • 华为大规模——重塑生产力
  • 软信天成:数据驱动型背后的人工智能,基于机器学习的数据管理