Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  *
  3.  * Phonegap print plugin for Android
  4.  * patadejaguar@gmail.com 2012
  5.  *
  6.  */
  7.  
  8. var LocalPrint = function() {};
  9.            
  10. LocalPrint.prototype.print = function(content, success, fail) {
  11.     return cordova.exec( function(args) {
  12.         success(args);
  13.     }, function(args) {
  14.         fail(args);
  15.     }, 'LocalPrint', '', [content]);
  16. };
  17.  
  18. if(!window.plugins) {
  19.     window.plugins = {};
  20. }
  21. if (!window.print) {
  22. }
  23. function printPage(src){
  24.         var data    = ( typeof src == "undefined" ) ? document.body.innerHTML : document.getElementById(src).innerHTML;
  25.         window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
  26.                                     function(fileSystem){
  27.                                         var d   = new Date();
  28.                                         var fil   = "tmp-" +  ( d.getTime() ) + ".html";
  29.                                         fileSystem.root.getFile( fil, { create: true }, function (fileEntry) {
  30.                                                                                         var mFullPath   = fileEntry.fullPath;
  31.                                                                                         fileEntry.createWriter(
  32.                                                                                                 function (file){
  33.                                                                                                 var txt     = "<!DOCTYPE html><html><head><meta charset=\"utf-8\" /></head><body>" + data + "</body></html>";
  34.                                                                                                 file.write(txt);
  35.                                                                                                     file.onwriteend = function(evt) {
  36.                                                                                                         //console.log("write success");
  37.                                                                                                         var p       = new LocalPrint();
  38.                                                                                                         //console.log(mFullPath);
  39.                                                                                                         p.print({fileURI : mFullPath, mimeType: "text/html"}, function(){}, function(){ alert("Android Print: Error!");});
  40.                                                                                                     };
  41.                                                                                                 }
  42.                                                                                                 ,function(){} ); }, function(){} ); }                                        
  43.                                         , function(){} );
  44. }