无声打印的Chrome扩展程序? [英] Chrome extensions for silent print?

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

问题描述

我制作了一个可打印PDF文件的无声打印网络应用程序。关键是将JavaScript添加到默认打印自己的PDF文件中。



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



但是正如它宣布的,这个解决方案在chrome 45之后将不起作用,因为npapi问题。



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



然而,我无法想象如何触发任何printProvider事件。
所以问题是:在Chrome扩展中可以考虑创建一个无声打印Web应用程序,以及如何触发和处理HTML页面的嵌入式PDF的打印作业。

$ b $最后我对这个问题达成了一个可以接受的解决方案,因为我找不到这个问题,但是读了很多文章,提到了同样的问题,我将离开我的解决方案在这里。



因此,首先您需要将打印机添加到Google Cloud Print,然后您需要将一个项目添加到 Google Developers Console 然后添加此脚本并随时添加打印执行print()函数的东西。此方法将打印内容中显示的文档



应用程序会要求您允许一次管理您的打印机。



 

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

一个'Access-Control-Allow-Origin'错误,尽管这出现在文档中...我无法使它工作:($ / b
$ b


Google API支持使用跨源资源共享(CORS)的请求和响应,您无需加载完整的JavaScript客户端库以使用CORS。但是,如果您希望应用程序访问用户的个人信息,则必须仍然使用Google的OAuth 2.0机制,为此,Google提供了独立的auth客户端 - JavaScript客户端的一个子集。


所以去扔这个我不得不安装此Chrome扩展程序 CORS 。我相信有人会改进这个脚本来避免这种扩展。


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.

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

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

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

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.

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

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>

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

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.

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天全站免登陆