自已实现一个远程打印方案 解决小程序或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访问。

相关推荐
恩创软件开发8 小时前
自助KTV系统管理员续单功能
经验分享·小程序
AI源码2 天前
在线考试小程序
小程序·notepad++
2501_916008892 天前
iPhone 设备信息查看,用 KeyMob 看型号、序列号、电池健康和 UDID
android·ios·小程序·https·uni-app·iphone·webview
云-缩影3 天前
做微信小程序,自建服务器还是用云托管:技术选型对比
服务器·微信小程序·小程序
超级新零售4 天前
服装订货会数字化:商品版本、订单状态与后续配发数据如何衔接?
大数据·小程序·企业微信
xiezhr4 天前
我用workBuddy开发了自己的第一个小程序并成功上线了
ai·小程序·ai agent·workbuddy
sinat_267611915 天前
用 AI 开发微信小程序(八):一个极简收纳工具的开发复盘
微信小程序·小程序
李纲明5 天前
微信小程序真机预览白屏或 request 失败:先核 request 合法域名与 TLS,再查体验版与本地缓存
缓存·微信小程序·小程序·小程序开发·devdiy·真机预览·合法域名
klfdgl5 天前
线上商城小程序投入测算,有成熟开发经验服务商的长期收益对比
小程序
一木 之林6 天前
Dify学习笔记 03 · RAG检索增强生成:三代范式、评估与落地(第40-45、55-56集)
笔记·小程序·交互·dify