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

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>

相关文章:

  • NodeJS Koa 后端用户会话管理,JWT, Session,长短Token,本文一次性讲明白
  • evo precision evaluation
  • 【分治法 容斥原理 矩阵快速幂】P6692 出生点|普及+
  • virtualbox 如何虚拟机ip固定
  • DFT测试之TAP/SIB/TDR
  • AudioRelay 0.27.5 手机充当电脑音响
  • Python: 操作 Excel折叠
  • VTK如何让部分单位不可见
  • js树形菜单功能总结
  • excel数据对比找不同:6种方法核对两列数据差异
  • 三十四、面向对象底层逻辑-SpringMVC九大组件之FlashMapManager接口设计哲学
  • 一文学会《使用Auto CAD2020绘制Allegro PCB板框》
  • Excel 表格内批量添加前缀与后缀的实用方法
  • RabbitMQ入门4.1.0版本(基于java、SpringBoot操作)
  • cnn卷积神经变体
  • 若依添加添加监听容器配置(删除键,键过期)
  • 什么?连接服务器也能可视化显示界面?:基于X11 Forwarding + CentOS + MobaXterm实战指南
  • [BJDCTF2020]Easy MD5 1
  • Excel 模拟分析之单变量求解简单应用
  • redhat系统中删除多余的硬盘
  • 做网站和做app那个简单/网站维护收费标准
  • 做网站需要什么技术员/百度推广工具有哪些
  • 广告制作材料价格单/qq群怎么优化排名靠前
  • 什么网站可以做软件有哪些/网络推广策划方案模板
  • 惠州网站建设找惠州邦/杭州优化排名哪家好
  • wordpress本地登陆/沈阳seo排名收费