Javascript 用于静音打印的 Chrome 扩展程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31888183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chrome extensions for silent print?
提问by Diego
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.
我制作了一个打印 PDF 文件的无声打印 Web 应用程序。关键是将 JavaScript 添加到 PDF 文件中,以静默打印自己。
To do this I open the PDF with acrobat reader in chrome, that allow me to execute the script (with the proper permissions).
为此,我使用 chrome 中的 acrobat 阅读器打开 PDF,这允许我执行脚本(具有适当的权限)。
But as it was announced this solution won't work after chrome 45 because the npapi issue.
但是正如宣布的那样,由于 npapi 问题,此解决方案在 chrome 45 之后将不起作用。
I guess a possible solution could be to use the recently release printProvider of chrome extensions.
我想一个可能的解决方案可能是使用最近发布的 chrome 扩展的 printProvider。
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.
尽管如此,我无法想象如何触发任何 printProvider 事件。所以问题是:可以考虑使用 chrome 扩展来制作无声打印 Web 应用程序,以及如何为 HTML 页面的嵌入式 PDF 触发和处理打印作业。
采纳答案by Diego
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
然后添加此脚本,并在需要打印某些内容时执行 print() 函数。此方法将打印内容中指示的文档
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 :(
无论如何,这个脚本会抛出一个“Access-Control-Allow-Origin”错误,即使这出现在文档中......我无法让它工作:(
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.
Google API 支持使用跨源资源共享 (CORS) 的请求和响应。您无需加载完整的 JavaScript 客户端库即可使用 CORS。但是,如果您希望您的应用程序访问用户的个人信息,它仍然必须使用 Google 的 OAuth 2.0 机制。为了实现这一点,Google 提供了独立的身份验证客户端——JavaScript 客户端的一个子集。
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 扩展CORS。我相信有人会改进这个脚本以避免这个 chrome 扩展。
回答by Jun Xie
You can register an Application to a URI Scheme to trigger the local application to print silently. The setting is pretty easy and straightforward. It's a seamless experience. I have posted the solution here with full example:
您可以将应用程序注册到 URI 方案以触发本地应用程序静默打印。设置非常简单明了。这是一种无缝体验。我在这里发布了完整示例的解决方案:
回答by Brian
After the removal of npapi, I don't believe this is possible solely programmatically. The only current way I know to get chrome to print silently is using chrome kiosk mode, which is a flag (mode) you have to set when starting chrome.
删除 npapi 后,我认为这不可能仅以编程方式进行。我所知道的让 chrome 静默打印的唯一当前方法是使用 chrome kiosk 模式,这是启动 chrome 时必须设置的标志(模式)。
Take a look at these SO posts:
看看这些SO帖子:
Silent printing (direct) using KIOSK mode in Google Chrome
在 Google Chrome 中使用 KIOSK 模式进行静默打印(直接)
回答by tresf
This used to be possible using browser plugins (e.g. Java + NPAPI, ActiveX) but has been blacklisted by most browsers for several years.
这曾经可以使用浏览器插件(例如 Java + NPAPI、ActiveX)来实现,但几年来已被大多数浏览器列入黑名单。
If interested in modern solutions that use similar techniques, the architecture usually requires the following:
如果对使用类似技术的现代解决方案感兴趣,则架构通常需要以下内容:
- WebSocket, HTTP or Custom URI connection back to localhost
- API that talks through web transport (JavaScript or custom URI scheme) to an app running locally.
- WebSocket、HTTP 或自定义 URI 连接回本地主机
- 通过 Web 传输(JavaScript 或自定义 URI 方案)与本地运行的应用程序对话的 API。
A detail of projects (several of them are open source) that leverage these technologies are available here: https://stackoverflow.com/a/28783269/3196753
此处提供了利用这些技术的项目(其中几个是开源的)的详细信息:https: //stackoverflow.com/a/28783269/3196753
Since the source code of these projects can vary (hundreds of lines to tens-of-thousands of lines), a code snippet would be too large unless a inquiring about a specific project's API.
由于这些项目的源代码可能会有所不同(数百行到数万行),除非查询特定项目的 API,否则代码片段会太大。
Side note: Some technologies offer dedicated cloud resources, which add convenience at the expense of potential latency and privacy. At the time of writing this, the most popular "free" cloud solution -- Google Cloud Print -- is slated to be retired in December 2020.
旁注:一些技术提供专用的云资源,以牺牲潜在的延迟和隐私为代价增加了便利。在撰写本文时,最受欢迎的“免费”云解决方案——谷歌云打印——计划于 2020 年 12 月退役。