Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
911 views
in Technique[技术] by (71.8m points)

javascript - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...