Chrome extensions for silent print?

29,013

Solution 1

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.

Solution 2

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

Solution 3

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

Share:
29,013
Diego
Author by

Diego

Updated on April 07, 2020

Comments

  • Diego
    Diego over 4 years

    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.

    • Kevin Brown
      Kevin Brown almost 9 years
      My comment would be ... if you think that it is OK to automatically print a PDF that I open without my permission ... you have to be kidding. I am surprised you are saying that it worked. There is no way (hopefully) in the future that someone can create a document that will automatically on opening will spew pages from my printer.
    • Diego
      Diego almost 9 years
      As I said, as long as the user give the proper permission for silent print in his pc, then is not that crazy. Check this link on how to give the proper permission adobe.com/devnet-docs/acrobatetk/tools/PrefRef/Windows/…
  • tresf
    tresf over 7 years
    If one's willing to run a desktop application or a remote server there are some more alternatives.
  • hakan
    hakan over 7 years
    what if there is no internet access?
  • Diego
    Diego over 7 years
    @piedpiper I'm afraid that this solution only works with internet access. This solution takes advantage of the google cloud print server implementation of a print queue that receive your job and then send it to your google chrome to finally send it to the printer. The original solution that I mentioned, that worked before the npapi issue, does not use internet access and still works with ie. But this can only print pdf files (because is the embeded acrobat reader who sends to the printer) and it assumes that you can add javascript to the pdf file to print it self.
  • Pablo Cesar Cordova Morales
    Pablo Cesar Cordova Morales over 6 years
    Hi @Diego, I'm trying to do the same, I read about Google Cloud Print and is my last option, did you find another option? or how to use printProvider? Thanks.
  • Diego
    Diego over 6 years
    @PabloCesarCordovaMorales Google cloud print is working nice for me and I using it in a bunch of solutions at the time. Take care of the CORS extension anyway because of the risk of it use. My advise would be that you make your own one for that particular case.
  • Pablo Cesar Cordova Morales
    Pablo Cesar Cordova Morales over 6 years
    @Diego I'm trying to use GCP but I don't understand the flow to use it. Despite I'm reading the official documentation. I added my printer manually to GCP and now I have a lot of data (proxy, printer id, client id, etc..), but for what I need to create a project that you said? or Do you have a tutorial to learn more about GCP in javascript(side client)?Thanks.
  • Diego
    Diego over 6 years
    @PabloCesarCordovaMorales I do not have any tutorial. The reason why you need to create a project in the console is because Google ask for it in order that you can use their service and of course to get YOUR_GOOGLE_API_CLIENT_ID. There you will have to register your site because Google will only accept print jobs sent from that site. Otherwise any site will be able to print in your printer... I think it's mostly for security reasons... But anyway it shouldn't cause you any trouble registering it, it's a free service by the way
  • Josh
    Josh about 4 years
    Heads up: Google cloud print is scheduled to be discontinued at the end of 2020: support.google.com/chrome/a/answer/9633006