用于静音打印的 Chrome 扩展程序? [英] Chrome extensions for silent print?

查看:13
本文介绍了用于静音打印的 Chrome 扩展程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个打印 PDF 文件的无声打印 Web 应用程序.关键是将 JavaScript 添加到 PDF 文件中,以静默打印自己.

I have made a silent print web application that prints a PDF file. The key was to add JavaScript to the PDF file that silently print itself.

为此,我使用 chrome 中的 acrobat 阅读器打开 PDF,这允许我执行脚本(具有适当的权限).

To do this I open the PDF with acrobat reader in chrome, that allow me to execute the script (with the proper permissions).

但正如宣布的那样,由于 npapi 问题,此解决方案在 chrome 45 之后将不起作用.

But as it was announced this solution won't work after chrome 45 because the npapi issue.

我想一个可能的解决方案是使用最近发布的 chrome 扩展的 printProvider.

I guess a possible solution could be to use the recently release printProvider of chrome extensions.

尽管如此,我无法想象如何触发任何 printProvider 事件.所以问题是:可以考虑使用 chrome 扩展来制作无声打印 Web 应用程序,以及如何为 HTML 页面的嵌入式 PDF 触发和处理打印作业.

Nevertheless I can't imagine how to fire any of the printProvider events. So the question is: Is ok to think in chrome extensions to make a silent print web application, and how can I fire and handle a print job for an embedded PDF of a HTML Page.

推荐答案

最后我找到了一个可以接受的解决方案,因为我在那里找不到它,但阅读了许多有相同问题的帖子,我将离开我的解决方法在这里.

Finally I reached an acceptable solution for this problem, as I couldn't find it out there, but read to many post with the same issue I will leave my solution here.

因此,首先您需要将打印机添加到 Google Cloud Print,然后您需要将项目添加到 Google 开发者控制台

So first you need to add your printer to the Google Cloud Print and then you will need to add a proyect to the Google Developers Console

然后添加此脚本,并在需要打印某些内容时执行 print() 函数.此方法将打印内容中指示的文档

Then add this script and any time you need to print something execute the print() function. This method will print the document indicated in the content

该应用程序将请求您的许可来管理您的打印机.

The application will ask for your permission once to manage your printers.

function auth() {
  gapi.auth.authorize({
    'client_id': 'YOUR_GOOGLE_API_CLIENT_ID',
    'scope': 'https://www.googleapis.com/auth/cloudprint',
    'immediate': true
  });

}

function print() {
  var xhr = new XMLHttpRequest();
  var q = new FormData()
  q.append('xsrf', gapi.auth.getToken().access_token);
  q.append('printerid', 'YOUR_GOOGLE_CLOUD_PRINTER_ID');
  q.append('jobid', '');
  q.append('title', 'silentPrintTest');
  q.append('contentType', 'url');
  q.append('content',"http://www.pdf995.com/samples/pdf.pdf");
  q.append('ticket', '{ "version": "1.0", "print": {}}');


  xhr.open('POST', 'https://www.google.com/cloudprint/submit');
  xhr.setRequestHeader('Authorization', 'Bearer ' + gapi.auth.getToken().access_token);
  xhr.onload = function () {
    try {
      var r = JSON.parse(xhr.responseText);
      console.log(r.message)
    } catch (e) {
      console.log(xhr.responseText)
    }
  }

  xhr.send(q)

}

window.addEventListener('load', auth);

<script src="https://apis.google.com/js/client.js"></script>

无论如何,这个脚本会抛出一个Access-Control-Allow-Origin"错误,即使这出现在文档中......我无法让它工作:(

Anyway this script throw a 'Access-Control-Allow-Origin' error, even though this appears in the documentation... I couldn't make it work :(

Google API 支持使用跨源资源共享 (CORS) 的请求和响应.您无需加载完整的 JavaScript 客户端库即可使用 CORS.但是,如果您希望您的应用程序访问用户的个人信息,它仍然必须使用 Google 的 OAuth 2.0 机制.为了实现这一点,Google 提供了独立的身份验证客户端——JavaScript 客户端的一个子集.

Google APIs support requests and responses using Cross-origin Resource Sharing (CORS). You do not need to load the complete JavaScript client library to use CORS. If you want your application to access a user's personal information, however, it must still work with Google's OAuth 2.0 mechanism. To make this possible, Google provides the standalone auth client — a subset of the JavaScript client.

所以要抛出这个我不得不安装这个 chrome 扩展 CORS.我相信有人会改进这个脚本以避免这个 chrome 扩展.

So to go throw this I had to install this chrome extension CORS. I'm sure that some one will improve this script to avoid this chrome extension.

这篇关于用于静音打印的 Chrome 扩展程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆