自已实现一个远程打印方案 解决小程序或APP在外面控制本地电脑打印实现

常规通过小程序或APP在外出时控制本地电脑实现打印功能,可以结合远程桌面技术、云打印服务或开发定制化的远程打印解决方案。

但这里我们采用自已的实现方案来解决

服务器端实现

搭建一个后端socket服务,监听来自手机的打印请求。监听到打印任务后向本地客户端打印机发送打印命令。

手机端小程序APP

开发小程序或APP,通过API与本地电脑的后端服务通信。

用户选择文件后,将文件上传或发送打印指令到后端服务。

本地客户端打印执行

本地客户端进行socket连接,注册服务。后端服务接收到打印请求后进行打印。本地客户端我们采用的是electron来连接服务器端socket。本地客户端就可以静默打印。客户端核心方案的打印代码是采用了electron BrowserWindow静默打印实现。

复制代码
win = new BrowserWindow({
        show: false,
        webPreferences: {
          nodeIntegration: false,
          contextIsolation: true,
          preload: join(__dirname, '../preload/print.js')
        }
      })

      if (options.printType === 'pdf') {
        // PDF打印 - 直接加载URL
        if (!options.url) {
          throw new Error('URL is required for PDF printing')
        }
        await win.loadURL(options.url)
      } else {
        // HTML内容打印 - 加载模板并注入内容
        if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
          await win.loadURL(`${process.env['ELECTRON_RENDERER_URL']}/print.html`)
        } else {
          await win.loadFile(join(__dirname, '../renderer/print.html'))
        }

        // 检查并注入HTML内容
        if (!options.content && !options.url) {
          throw new Error('Either content or url is required for HTML printing')
        }

        let htmlContent = options.content
        if (!htmlContent && options.url) {
          // 如果提供了url但没有content,尝试从 url 解析内容
          htmlContent = decodeURIComponent(
            options.url.replace('data:text/html;charset=utf-8,', '')
          )
        }

        await win.webContents.executeJavaScript(`
          document.getElementById('printContent').innerHTML = ${JSON.stringify(htmlContent)}
        `)
      }

      // 等待内容加载完成
      await new Promise((resolve) => setTimeout(resolve, 1000))
      await win.webContents.executeJavaScript('document.readyState === "complete"')

      // 执行打印
      const result = await new Promise((resolve, reject) => {
        win.webContents.print(
          {
            silent: true,
            printBackground: options.printBackground || true,
            deviceName: options.printer,
            pageSize: options.pageSize === 'custom'
              ? {
                width: options.customWidth * 1000,
                height: options.customHeight * 1000
              }
              : options.pageSize,
            landscape: options.landscape,
            copies: options.copies || 1,
            margins: {
              top: options.margins?.top || 0,
              bottom: options.margins?.bottom || 0,
              left: options.margins?.left || 0,
              right: options.margins?.right || 0
            }
          },
          (success, failureError) => {
            if (success) {
              resolve({ success: true })
            } else {
              reject(failureError || new Error('Print failed'))
            }
          }
        )
      })

      win.close()

优点

完全自定义,满足特定需求。

数据传输更安全,隐私可控。

缺点

开发成本较高,需要一定的技术能力。

需要确保本地电脑和手机在同一网络下,或通过公网IP访问。

相关推荐
bug总结2 小时前
uniapp 微信小程序分包规范
微信小程序·小程序·uni-app
Quor2 小时前
小程序工作室:AI 生成即运行的可视化工作台
人工智能·小程序
盟道科技7 小时前
小程序电商订单超时自动取消的三种实现方案:定时扫描、Redis 过期监听、延迟消息对比与生产落地
数据库·redis·小程序
lhldsg8 小时前
智慧场馆解决方案小程序系统:从架构设计到落地实践
java·小程序·需求分析
凉红茶9 小时前
用Cursor开发微信小程序的第一天
微信小程序·小程序·ai编程
CRMEB系统商城10 小时前
汽车养护连锁的数字化底座CRMEB 多门店系统实战方案
java·开发语言·小程序·开源·汽车
小马过河R10 小时前
微信小程序自定义登录态维护:从入门到生产级落地
后端·微信小程序·小程序·架构·登录态
火眼金睛炼单词10 小时前
零基础高效记忆单词:小程序学习法的完整实操指南
学习·小程序
vx+_bysj686911 小时前
springboot 旅行小程序97353
spring boot·后端·小程序·旅行小程序
andrsted16 小时前
用练题簿小程序 - 让错题不再白错
学习·微信小程序·小程序·学习方法