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

相关推荐
幽络源小助理2 小时前
SpringBoot+小程序高校素拓分管理系统源码 – 幽络源免费分享
spring boot·后端·小程序
Mr -老鬼2 小时前
移动端跨平台适配技术框架:从发展到展望
android·ios·小程序·uni-app
内存不泄露3 小时前
棋牌预约小程序系统论文
小程序
说私域21 小时前
短视频私域流量池的变现路径创新:基于AI智能名片链动2+1模式S2B2C商城小程序的实践研究
大数据·人工智能·小程序
毕设源码-邱学长1 天前
【开题答辩全过程】以 基于微信小程序的松辽律所咨询系统的设计与实现为例,包含答辩的问题和答案
微信小程序·小程序
+VX:Fegn08951 天前
计算机毕业设计|基于springboot + vue物流配送中心信息化管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·小程序·课程设计
说私域1 天前
B站内容生态下的私域流量运营创新:基于AI智能名片链动2+1模式与S2B2C商城小程序的融合实践
人工智能·小程序·流量运营
计算机毕设指导61 天前
基于微信小程序的钓鱼论坛系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
qq_12498707531 天前
基于微信小程序的宠物交易平台的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·微信小程序·小程序·毕业设计·计算机毕业设计
计算机毕设指导61 天前
基于微信小程序的精致护肤购物系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·intellij-idea