VUE调用打印机打印页面(VUE3版)

之前有个VUE2版的实现方法:

VUE调用打印机打印页面_vue调用一体式打印机-CSDN博客

因为项目需要,现在要搞一个VUE3版的

大致的需求是:通过点击一个按钮,弹出一个对话框,然后对话框上有个打印按钮,点了后,会把需要打印的内容通过调用本地的打印机打印出来。

(吐槽一下那个AI啊,折腾了两个小时,一直离成功差那么一点点,让我装这个插件那个插件的。后来只能自己现学现用把代码改出来了。所以说人工智能顶替程序员的事不太靠谱。)

实现的方法:

1.先做一个print.js

javascript 复制代码
export default {
    mounted(el, binding) {
        const content = binding.value;
        console.log(`打印内容: ${content}`);
        // 执行实际的打印逻辑
    }
};

2.在man.js里注册一下

javascript 复制代码
import printDirective from './directives/print'; // 确保路径正确

app.directive('print', printDirective);

3.在页面里调用

html的部分

html 复制代码
    <div id="app1">
      <el-dialog v-model="dialogVisible" title="对话框" ref="dialogRef">
        <div ref="printArea">
          <p>{{ dialogContent }}</p>
          <img :src="profileImageUrl" alt="Profile Image" @load="onImageLoad" />
          <p>测试用的文字</p>
          <table>
            <tr>
              <td>1</td>
              <td>2</td>
            </tr>
          </table>
        </div>
        <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="printDialogContent">打印</el-button>
        </span>
        </template>
      </el-dialog>
      <el-button @click="showDialog">显示对话框</el-button>
    </div>

js的部分

javascript 复制代码
// 对话框状态
const dialogVisible = ref(false);

// 对话框内容
const dialogContent = ref('这是要打印的内容');

// 图片路径
const profileImageUrl = 'https://via.placeholder.com/150';

// 对话框引用
const dialogRef = ref(null);

// 打印区域引用
const printArea = ref(null);

// 图片是否已加载完成
const imageLoaded = ref(false);

// 显示对话框
const showDialog = () => {
  dialogVisible.value = true;
};

// 图片加载完成事件
const onImageLoad = () => {
  imageLoaded.value = true;
};

// 打印对话框内容
const printDialogContent = () => {
  if (dialogVisible.value && dialogRef.value && printArea.value) {
    const printElement = printArea.value;

    if (!imageLoaded.value) {
      console.warn('图片尚未加载完成,请稍后重试。');
      return;
    }

    const createPrintWindow = () => {
      const newWindow = window.open('', '_blank'); // 创建一个新的空白窗口
      if (newWindow) {
        setTimeout(() => {
          newWindow.document.write('<html><head><title>打印内容</title></head><body>');
          newWindow.document.write(printElement.innerHTML);
          newWindow.document.write('</body></html>');
          newWindow.document.close();

          // 触发打印
          newWindow.print();

          // 关闭窗口
          newWindow.close();
        }, 100); // 延迟100毫秒执行
      } else {
        console.error('无法打开新窗口');
      }
    };

    createPrintWindow();
  }
};

这里有个坑,因为主动打开窗口会被EDGE拦截,所以就延迟了100ms,如果用谷歌浏览器打开,就完全没这个问题。

相关推荐
大家的林语冰7 小时前
Canvas 文艺复兴,HTML-in-Canvas 炫酷特效摆拍走红,Canvas 中也能渲染交互式的 HTML 元素了
前端·javascript·html
WebGirl7 小时前
Visual Studio Code (VSCode) 中配置 MCP
前端
JarvanMo7 小时前
Fluwx 6.0 预览版本他来了
前端
KaMeidebaby7 小时前
卡梅德生物技术快报|单 B 细胞抗体筛选服务:技术架构、流程实现与数据验证
前端·数据库·其他·百度·新浪微博
爱勇宝7 小时前
别焦虑,也别躺平:给年轻程序员的一封信
前端·后端·架构
OpenTiny社区7 小时前
2026 OpenTiny NEXT 产品调研启动!
前端·开源·github
德莱厄斯7 小时前
GIS 开发要变天?看看高德空间智能给我们带来了什么!
前端·gis·agent
JarvanMo8 小时前
Flutter.Dart的主构造函数(primary constructor)可能马上就要来了
前端
用户65868180338408 小时前
一个前端CLAUDE.md
前端