首页 > 其他分享 >打印功能实现--web端+移动端

打印功能实现--web端+移动端

时间:2024-07-03 12:11:32浏览次数:14  
标签:web console log -- 打印 Bluetooth Print device const

web端---调用本机的打印机即可

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Print Custom Content and Style</title>
<style>
@media print {
  /* 打印时隐藏页面中的元素 */
  #header, #footer { display: none; }

  /* 设置打印页面布局 */
  body { margin: 0; }
}

/* 自定义打印样式 */
.print-content {
  padding: 20px;
  background-color: #f0f0f0;
  border: 1px solid #ccc;
}
</style>
</head>
<body>
<div id="printSection" class="print-content">
  <!-- 这里放置您想要打印的内容 -->
  <h1>Custom Print Content</h1>
  <p>This is a custom content for printing.</p>
</div>

<button onclick="printSection()">Print</button>

<script>
function printSection() {
  let printContent = document.getElementById('printSection').outerHTML;
  let originalContent = document.body.innerHTML;
  document.body.innerHTML = printContent;
  window.print();
  document.body.innerHTML = originalContent;
}
</script>
</body>
</html>

 

移动端---使用蓝牙传输

 

<template>
  <van-button type="primary" @click="printToBluetoothPrinter">自定义打印</van-button>
</template>

<script>
export default {
  methods: {
    async printToBluetoothPrinter() {
      try {
        const device = await navigator.bluetooth.requestDevice({
          filters: [{ services: [0x1809] }]
        });
        
        const server = await device.gatt.connect();
        const service = await server.getPrimaryService(0x1802);
        const characteristic = await service.getCharacteristic(0x2A06);
        
        const printContent = `
          <h1 style="color: blue;">Custom Print Content</h1>
          <p style="font-size: 16px;">This is a dynamic custom print content.</p>
        `;
        
        const encoder = new TextEncoder();
        await characteristic.writeValue(encoder.encode(printContent));

        console.log('Print command sent successfully');
      } catch (error) {
        console.error('Error:', error);
      }
    }
  }
};
</script>

 

另附上一个蓝牙连接各阶段可调试:

// 检查浏览器是否支持 Web Bluetooth API
if ('bluetooth' in navigator) {
  console.log('Web Bluetooth API is supported');

  // 请求蓝牙设备连接
  navigator.bluetooth.requestDevice({
    filters: [{ services: [0x1809] }]
  })
  .then(device => {
    console.log('Bluetooth device selected: ', device.name);

    // 连接蓝牙设备
    return device.gatt.connect();
  })
  .then(server => {
    console.log('Bluetooth device connected: ', server);

    // 获取服务和特征值
    return server.getPrimaryService(0x1802);
  })
  .then(service => {
    console.log('Bluetooth service selected: ', service);
    return service.getCharacteristic(0x2A06);
  })
  .then(characteristic => {
    console.log('Bluetooth characteristic selected: ', characteristic);

    // 向蓝牙打印机发送打印指令
    let printData = 'Hello, Bluetooth Printer!';
    let encoder = new TextEncoder();
    return characteristic.writeValue(encoder.encode(printData));
  })
  .then(() => {
    console.log('Print command sent successfully');
  })
  .catch(error => {
    console.error('Error:', error);
  });
} else {
  console.log('Web Bluetooth API is not supported');
}

 

标签:web,console,log,--,打印,Bluetooth,Print,device,const
From: https://www.cnblogs.com/lftBlogs/p/18281354

相关文章

  • 使用Filter接口编写过滤器解决post乱码
    在使用tomcat9以及之前的版本,request-character-encoding和response-character-encoding使用的字符编码默认不是utf-8,所以导致前端发送到后台的中文乱码.如果使用的是tomcat10以及之后的版本,在apache-tomcat-10.1.25\conf\web.xml已设置好默认的字符集编码为utf-8,如果所示:......
  • linphone 回声消除
    声学回声消除是通过消除或者移除本地话筒中拾取到的远端的音频信号来阻止远端的声音返回去的一种处理方法。linphone上使用speex库实现了回声消除插件,speex库是目前开源的声学回声消除做的比较好的库。下面总结一下,linphone上的回声消除部分。 第一部分--配置linphone的声......
  • 2024年的NBA合同年
    在合同签字的那一刻,不同的人终于奔赴不同的旅程。BronyJames定义了湖人的Nepolism,同时也占据了一个本该进入NBA孩子的一个命运的位置。闹得最热闹的当属克莱汤普森klayThompson,2023年勇士开了1.2亿四年,可是克莱需要2.1四年的顶薪,谈判拉锯各种报价越谈越少,Klay场上的表现也越......
  • Linphone 配置参数(噪音调试)
    1,先找到跟语音相关的参数,其实有很多归纳一下无非就是回声消除参数、回声抑制参数、噪声处理参数、均衡器参数4个。2,要了解参数的用途。花费了一周时间还是值得的。agc=0 #这个是自动增益,试过=1会啸叫echocancellation=1#回声消除这个必须=1,否则会听到自己说话的声音ec_tail_len......
  • 随机数漫谈
    随机数对程序设计来说很重要,今天就从几方面探讨下一些常见的随机数相关的问题。本文只讨论整数相关的随机数,另外需要你对概率论有最基本的了解(至少知道古典概型是什么)。本文索引如何从rand7生成rand5go标准库的做法从rand5生成rand7充分利用每一个bit带有权重的随机......
  • 【playwright学习】github create issue test
    fromplaywright.sync_apiimportsync_playwright,Page,Playwright,Browser,[email protected](scope="module",autouse=True)deflogin_github(playwright:Playwright):chrome=playwright.chromium.launch(he......
  • 【日记】在街上跳舞被同事看见了(470 字)
    正文昨晚跳舞,照例在街上表演,被单位里的保洁阿姨撞见了……我以为这就完了,结果她还拍了视频发给做饭阿姨。晚上吃饭无意间聊起才知道有这回事。我竟一时间不知该哭还是该笑…….今天非常非常闲。虽然不是没工作,只是我懒得去做,堆在那里罢了。下午还差点跟主管吵起来(语气......
  • SRC实战:分享一个不到500块的高危,史低价(在steam都可以上头条了)。
    最近工作忙得飞起,挖洞的时间变少了,一般有什么活动之类的才会花个半天时间去尝试一下。今天这个漏洞有点意思,不到500块的高危,但是我个人认为是严重低估了的,但没办法,白帽子在审核面前没人权啊。以下主要是过程与思路分析,没截图,因为几百块的高危不配有图。0x00入口这次的......
  • 外网爆火!真正内行人必看的大模型神书!
    ......
  • 基于C++类与权限初识:银行系统
    功能:银行的账户是一个模板,是一个类,有存款人信息和账户额度,而具体的存款人视为一个对象,一个对象不能私自修改账户额度,需要通过一个操作流程,比如去ATM或者柜台进行操作才能修改到账户额度,所以,存款人信息和账户额度设计成私有权限,通过公有的操作流程,也就是公有函数去操作私有......