【问题标题】:Accessing printers installed and printing raw data to label printer using Google Native client使用 Google Native 客户端访问已安装的打印机并将原始数据打印到标签打印机
【发布时间】:2016-03-15 15:06:37
【问题描述】:

我们的 Web 应用程序有一个流程,在点击页面上的打印按钮时,来自服务器的原始数据会在用户选择的打印机(Zebra 标签打印机)上打印在客户端机器上。浏览器和服务器之间的交互顺序如下:
1)用户点击页面上的打印
2)服务器发回一个小程序
3)此小程序通过url连接到服务器以检索打印数据
4)小程序然后查找安装在客户端机器上的打印机列表并显示打印机选择对话框。所有打印机查找都是使用 javax.print
完成的 5)一旦用户选择了标签打印机,小程序就会创建一个打印作业并将其发送到选定的打印机。打印标签

现在,由于谷歌已经从 chrome 中删除了 NPAPI,我们正在研究我们必须有哪些替代方案才能在 chrome 上获得相同的功能,特别是在 windows 上。最初通过 Google 本地客户端文档,我们认为我们可以使用 PNACL 实现上述用例。以下代码来自 MSDN,它是使用 winspool.h 将原始数据打印到默认打印机的例程

BOOL RawDataToPrinter(LPBYTE lpData, DWORD dwCount) {

   HANDLE       hPrinter;
   DOC_INFO_1       DocInfo;
   DWORD      dwJob;
   DWORD      dwBytesWritten;

               TCHAR result[ MAX_PATH ] = {'\0'};
                            DWORD length = MAX_PATH;
                            GetDefaultPrinter( result, &length );


    // Need a handle to the printer.
    if( ! OpenPrinter(result, &hPrinter, NULL ) )
      return FALSE;

    // Fill in the structure with info about this "document."
    DocInfo.pDocName = L"Demo Page";
    DocInfo.pOutputFile = NULL;
    DocInfo.pDatatype = L"RAW";
    // Inform the spooler the document is beginning.
    if( (dwJob = StartDocPrinter( hPrinter, 1, (BYTE *)&DocInfo )) == 0 )
    {
      ClosePrinter( hPrinter );
      return FALSE;
    }
    // Start a page.
    if( ! StartPagePrinter( hPrinter ) )
    {
      EndDocPrinter( hPrinter );
      ClosePrinter( hPrinter );
      return FALSE;
    }
    // Send the data to the printer.
    if( !WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten ) )
    {
      EndPagePrinter( hPrinter );
      EndDocPrinter( hPrinter );
      ClosePrinter( hPrinter );
      return FALSE;
    }
    // End the page.
    if( ! EndPagePrinter( hPrinter ) )
    {
      EndDocPrinter( hPrinter );
      ClosePrinter( hPrinter );
      return FALSE;
    }
    // Inform the spooler that the document is ending.
    if( ! EndDocPrinter( hPrinter ) )
    {
      ClosePrinter( hPrinter );
      return FALSE;
    }
    // Tidy up the printer handle.
    ClosePrinter( hPrinter );
    // Check to see if correct number of bytes were written.
    if( dwBytesWritten != dwCount )
      return FALSE;
    return TRUE;
  }

最初我们认为我们可以从一个辣椒插件中调用上述例程。页面上的 javascript 将检索标签数据并将其传递给本机客户端实例,这将调用 RawDataToPrinter(..) 例程。
但是,根据我们从论坛中了解到的情况,本机客户端将无法访问/查找打印机并将原始数据假脱机到选定的打印机。它不允许您调用它们提供的 api 端口之外的任何内容 这种理解正确吗?如果是,那么是否有任何替代方法可以在 chrome 上实现上述用例?如果否且上述情况可行,是否有任何可用端口允许访问安装在客户机器上的打印机并允许我们将数据打印到选定的打印机?

【问题讨论】:

  • 你是如何解决这个问题的?我有类似的要求。谢谢!
  • 我们使用 chrome 扩展运行时 api 解决了这个问题。 developer.chrome.com/apps/nativeMessaging
  • 我们使用 chrome 扩展运行时 api 解决了这个问题。 developer.chrome.com/apps/nativeMessaging。我们开发了一个 chrome 扩展,它将充当我们的应用程序网页和本机应用程序(使用 VC++ 开发)之间的桥梁。该网页将连接到 chrome 扩展,将打印作业信息作为 json 发送到扩展。该扩展反过来将连接到本机 exe 应用程序并传递它从网页接收到的 json 数据。本机 exe 应用程序将负责从标准 I/O 读取 json 数据,然后查找打印机
  • 谢谢!我们将使用 JNLP 做类似的事情。

标签: google-chrome printing google-nativeclient ppapi


【解决方案1】:

简短的回答是否定的,这在 Native Client 中是不可能的,因为它的安全沙箱旨在防止网站使用系统 API。您可以在客户端上安装本机 Windows 应用程序,并使用 Native Messaging 和 Javascript 与它进行通信,从您的网页传递您想要的任何数据。

【讨论】:

  • 感谢您的回答,德里克。但是,当我们要求用户安装越来越多的软件以实现应用程序功能时,我觉得这会破坏 Web 应用程序的目的。我们会尝试原生消息传递。另一种选择是为 chrome 提供 IE-tab 扩展。再次感谢
猜你喜欢
  • 2020-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多