Diego
Diego

Reputation: 231

Chrome extensions for silent print?

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.

Upvotes: 8

Views: 31844

Answers (4)

tresf
tresf

Reputation: 7922

This used to be possible using browser plugins (e.g. Java + NPAPI, ActiveX) but has been blacklisted by most browsers for several years.

If interested in modern solutions that use similar techniques, the architecture usually requires the following:

  1. WebSocket, HTTP or Custom URI connection back to localhost
  2. API that talks through web transport (JavaScript or custom URI scheme) to an app running locally.

A detail of projects (several of them are open source) that leverage these technologies are available here: 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.

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.

Upvotes: 0

Jun Hsieh
Jun Hsieh

Reputation: 1614

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:

https://stackoverflow.com/a/37601807/409319

Upvotes: 2

Diego
Diego

Reputation: 231

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.

Upvotes: 7

Brian
Brian

Reputation: 1553

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.

Take a look at these SO posts:

Silent printing (direct) using KIOSK mode in Google Chrome

Running Chrome with extension in kiosk mode

Upvotes: 1

Related Questions