diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /mobile/android/chrome/content/PrintHelper.js | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'mobile/android/chrome/content/PrintHelper.js')
-rw-r--r-- | mobile/android/chrome/content/PrintHelper.js | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/mobile/android/chrome/content/PrintHelper.js b/mobile/android/chrome/content/PrintHelper.js new file mode 100644 index 000000000..9b071ee92 --- /dev/null +++ b/mobile/android/chrome/content/PrintHelper.js @@ -0,0 +1,73 @@ +// -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ +"use strict"; + +XPCOMUtils.defineLazyModuleGetter(this, "Snackbars", "resource://gre/modules/Snackbars.jsm"); + +var PrintHelper = { + init: function() { + Services.obs.addObserver(this, "Print:PDF", false); + }, + + observe: function (aSubject, aTopic, aData) { + let browser = BrowserApp.selectedBrowser; + + switch (aTopic) { + case "Print:PDF": + Messaging.handleRequest(aTopic, aData, (data) => { + return this.generatePDF(browser); + }); + break; + } + }, + + generatePDF: function(aBrowser) { + // Create the final destination file location + let fileName = ContentAreaUtils.getDefaultFileName(aBrowser.contentTitle, aBrowser.currentURI, null, null); + fileName = fileName.trim() + ".pdf"; + + let file = Services.dirsvc.get("TmpD", Ci.nsIFile); + file.append(fileName); + file.createUnique(file.NORMAL_FILE_TYPE, parseInt("666", 8)); + + let printSettings = Cc["@mozilla.org/gfx/printsettings-service;1"].getService(Ci.nsIPrintSettingsService).newPrintSettings; + printSettings.printSilent = true; + printSettings.showPrintProgress = false; + printSettings.printBGImages = false; + printSettings.printBGColors = false; + printSettings.printToFile = true; + printSettings.toFileName = file.path; + printSettings.printFrameType = Ci.nsIPrintSettings.kFramesAsIs; + printSettings.outputFormat = Ci.nsIPrintSettings.kOutputFormatPDF; + + let webBrowserPrint = aBrowser.contentWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebBrowserPrint); + + return new Promise((resolve, reject) => { + webBrowserPrint.print(printSettings, { + onStateChange: function(webProgress, request, stateFlags, status) { + // We get two STATE_START calls, one for STATE_IS_DOCUMENT and one for STATE_IS_NETWORK + if (stateFlags & Ci.nsIWebProgressListener.STATE_START && stateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK) { + // Let the user know something is happening. Generating the PDF can take some time. + Snackbars.show(Strings.browser.GetStringFromName("alertPrintjobToast"), Snackbars.LENGTH_LONG); + } + + // We get two STATE_STOP calls, one for STATE_IS_DOCUMENT and one for STATE_IS_NETWORK + if (stateFlags & Ci.nsIWebProgressListener.STATE_STOP && stateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK) { + if (Components.isSuccessCode(status)) { + // Send the details to Java + resolve({ file: file.path, title: fileName }); + } else { + reject(); + } + } + }, + onProgressChange: function () {}, + onLocationChange: function () {}, + onStatusChange: function () {}, + onSecurityChange: function () {}, + }); + }); + } +}; |