在cordova中打印pdf文件

时间:2015-01-13 15:53:35

标签: cordova

在我的app mobile cordova中,我想开始打印从远程网址下载的pdf文件 我查了一些插件phonegap-print-plugincordova-plugin-printer,但它们只允许打印文本内容或html文档。
有些工具可以满足我的要求吗?

2 个答案:

答案 0 :(得分:1)

我写了一个cordova插件,它将从base64编码的字符串中打印pdf,而不必先在另一个应用程序中打开它。

appendChild

答案 1 :(得分:1)

如果你想使用Ionic 2/3的cordova-plugin-print-pdf,你需要从Typescript世界访问Javascript。在新的Ionic中有很多插件。线索是,您从Npm加载包装文件。我没有找到包装器,这里是片段。

它只是实现了打印功能。

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

/**
 * @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:

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

onScriptLoadingComplete(moduleList,finishPluginLoading) 在“模块主义者”中包括所有插件。这些将分配给“符号列表”。 “symbollist”由类型(Clobber,Merge,Default,Run),PluginName和Clobber组成。 Clobber是函数所针对的“pluginRef”,通常是window,cordova或navigator

<强> exports.injectScript 传递模块代码的URL。带有此URL的标记将添加到HTML元素“document.head”。

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