在Cordova中打印pdf文件 [英] Printing pdf file in cordova

查看:51
本文介绍了在Cordova中打印pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序移动cordova中,我想开始打印从远程URL下载的pdf文件.
我发现某些插件,例如 phonegap-print-plugin

In my app mobile cordova, I would like to start printing a pdf file downloaded from a remote url.
I fouded some plugin such phonegap-print-plugin and cordova-plugin-printer, but they only allow printing of text content or html documents.
There are tools that can satisfy my request?

推荐答案

如果要使用Ionic 2/3的cordova-plugin-print-pdf,则需要从Typescript世界访问Javascript.在新的Ionic中,有许多插件.提示是,您从Npm加载了包装文件.我没有找到任何包装,这是代码段.

If you want use cordova-plugin-print-pdf from Ionic 2/3, you need access from Typescript world to Javascript. In new Ionic are many plugins. The clue is, you load from Npm the wrapper file. I don't found no wrapper, here the snippet.

仅实现了打印功能.

您需要在计划使用的模块中的app.module.ts und中添加此模块.同样,您可以从新的Ionic中使用所有旧"插件.可能需要在发生故障时进行调试,可以从Chrome中的Developer-Console进行远程调试.

You need add this modul in app.module.ts und in the module, where you plan the use. In same way you can use from new Ionic all "old" plugins. Possible you need to debug while failures, you can remote-debug from Developer-Console in Chrome.

/**
 * @constructor
 */
var PrintPDF = function () {
  this.METHOD = 'printDocument';
  this.IS_AVAILABLE_METHOD = 'isPrintingAvailable';
  this.DISMISS_METHOD = 'dismissPrintDialog';
  this.CLASS = 'PrintPDF';
};

PrintPDF.prototype.print = function(options) {

  options = options || {};

  var data = options.data; // print data, base64 string (required)

  var type = options.type || 'Data'; // type of document

  var title = options.title || 'Print Document'; // title of document

  var dialogX = options.dialogX || -1; // if a dialog coord is not set, default to -1.
  // the iOS method will fall back to center on the
  var dialogY = options.dialogY || -1; // screen if it gets a dialog coord less than 0.

  // make sure callbacks are functions or reset to null
  var successCallback = (options.success && typeof(options.success) === 'function') ? options.success : this.defaultCallback;

  var errorCallback = (options.error && typeof(options.error) === 'function') ? options.error : this.defaultCallback;

  // make sure data is set
  if (!data) {
    if (errorCallback) {
      errorCallback({
        success: false,
        error: "Parameter 'data' is required."
      });
    }
    return false;
  }

  var args = [data];

  if (device.platform === "iOS") {
    args.push(type);
    args.push(dialogX);
    args.push(dialogY);

  } else {
    args.push(type);
    args.push(title);

  }

  // make the call
  cordova.exec(successCallback, errorCallback, this.CLASS, this.METHOD, args);

};

PrintPDF.prototype.isPrintingAvailable = function (callback) {

  // make sure callbacks are functions or reset to null
  var successCallback = (callback && typeof(callback) === 'function') ? callback : this.defaultCallback;

  cordova.exec(successCallback, null, this.CLASS, this.IS_AVAILABLE_METHOD, []);

};

PrintPDF.prototype.dismiss = function () {

  // Dismiss is only an iOS method because the dialog exists in the
  // same context as the cordova activity. In Android, when the
  // print activity starts, the cordova activity is paused.

  if (device.platform === "iOS") {

    cordova.exec(null, null, this.CLASS, this.DISMISS_METHOD, []);

  }

};

PrintPDF.prototype.defaultCallback = null;

// Plug in to Cordova
cordova.addConstructor(function () {
  if (!window.Cordova) {
    window.Cordova = cordova;
  };

  if (!window.plugins) window.plugins = {};
  window.plugins.PrintPDF = new PrintPDF();
});
/**
 * @constructor
 */
var PrintPDF = function () {
  this.METHOD = 'printDocument';
  this.IS_AVAILABLE_METHOD = 'isPrintingAvailable';
  this.DISMISS_METHOD = 'dismissPrintDialog';
  this.CLASS = 'PrintPDF';
};

PrintPDF.prototype.print = function(options) {

  options = options || {};

  var data = options.data; // print data, base64 string (required)

  var type = options.type || 'Data'; // type of document

  var title = options.title || 'Print Document'; // title of document

  var dialogX = options.dialogX || -1; // if a dialog coord is not set, default to -1.
  // the iOS method will fall back to center on the
  var dialogY = options.dialogY || -1; // screen if it gets a dialog coord less than 0.

  // make sure callbacks are functions or reset to null
  var successCallback = (options.success && typeof(options.success) === 'function') ? options.success : this.defaultCallback;

  var errorCallback = (options.error && typeof(options.error) === 'function') ? options.error : this.defaultCallback;

  // make sure data is set
  if (!data) {
    if (errorCallback) {
      errorCallback({
        success: false,
        error: "Parameter 'data' is required."
      });
    }
    return false;
  }

  var args = [data];

  if (device.platform === "iOS") {
    args.push(type);
    args.push(dialogX);
    args.push(dialogY);

  } else {
    args.push(type);
    args.push(title);

  }

  // make the call
  cordova.exec(successCallback, errorCallback, this.CLASS, this.METHOD, args);

};

PrintPDF.prototype.isPrintingAvailable = function (callback) {

  // make sure callbacks are functions or reset to null
  var successCallback = (callback && typeof(callback) === 'function') ? callback : this.defaultCallback;

  cordova.exec(successCallback, null, this.CLASS, this.IS_AVAILABLE_METHOD, []);

};

PrintPDF.prototype.dismiss = function () {

  // Dismiss is only an iOS method because the dialog exists in the
  // same context as the cordova activity. In Android, when the
  // print activity starts, the cordova activity is paused.

  if (device.platform === "iOS") {

    cordova.exec(null, null, this.CLASS, this.DISMISS_METHOD, []);

  }

};

PrintPDF.prototype.defaultCallback = null;

// Plug in to Cordova
cordova.addConstructor(function () {
  if (!window.Cordova) {
    window.Cordova = cordova;
  };

  if (!window.plugins) window.plugins = {};
  window.plugins.PrintPDF = new PrintPDF();
});

当您使用Remote-Debug在Android上调试Javascript时,这里是cordova.js中有用的断点:

When you debug the Javascript on your Android with Remote-Debug here are usefull breakpoints in cordova.js:

功能构建(模块)
要检查模块-所有模块,而不仅仅是插件-检查已加载的模块.如果模块不存在,则不会出现错误消息,但是会保留以前的空对象.返回module.exports包含插件的方法

function build(module)
To check the modules -all modules, not just plugins- check which are loaded. If a module does not exist, there is no error message, but the previously empty object remains. Return module.exports includes the methods of the plugin

onScriptLoadingComplete(moduleList,finishPluginLoading)在模块列表"中,包括所有插件.这些将分配给符号列表".符号列表"由类型(Clobber,Merge,Default,Run),PluginName和Clobber组成.Clobber是功能所针对的"pluginRef",通常是窗口,cordova或导航器

onScriptLoadingComplete(moduleList, finishPluginLoading) In the "modullist" include all plugins. These will assigned to the "symbollist". The "symbollist" consists of type (Clobber, Merge, Default, Run), PluginName and Clobber. Clobber is the "pluginRef" to which the functions are addressed, usually window, cordova or navigator

exports.injectScript 传递模块代码的URL.带有此URL的标签将添加到HTML元素"document.head".

exports.injectScript The URL of the code of the modules is passed. the tag with this URL is added to the HTML element "document.head".

CodeExample: https://github.com/alpinea310/ch.schb.rav.mobil

CodeExample:https://github.com/alpinea310/ch.schb.rav.mobil

这篇关于在Cordova中打印pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆