javascript - JPG转PDF Cordova插件

标签 javascript cordova ionic-framework cordova-plugins jspdf

我正在使用 ionic 框架,我想从厨房中挑选一个图像文件并将其转换为 PDF 并将其保存在手机上的文件系统中。 是否有任何 javascript 或 cordova 插件可用于执行此任务。

我找到了 jsPDF可用于将图像转换为 pdf,但它真的适用于移动设备吗?

最佳答案

我希望我没有那么晚!

除了jSPDF ,需要以下包:

  • cordova 插件文件
  • cordova-plugin-file-opener2

并安装相应的ionic-package:

bower install ngCordova

请注意,关于如何在您的文件系统中保存 pdf 文件,有几种可能的解决方案。我必须将 pdf 文件移动到某个目录才能打开它。尽管如此,这个解决方案适用于我迄今为止测试过的所有安卓手机,你可以创建你喜欢的所有 pdf。

iOS:您可以修改存储目录,使其也适用于 iOS 版本。

索引.html:

<head>
<meta charset="utf-8">
... include jsPDF
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.debug.js"></script>

....
some html
....

... include ngCordova
<script src="lib/ngCordova/dist/ng-cordova.js"></script>

.... the core-library
<script src="cordova.js"></script>
....
</head>

View.html:

<ion-view view-title="Dashboard">
  <ion-content class="padding">
    <p on-tap="savePDF()">Create PDF</p>
    <p on-tap="openPDF()">Open PDF</p>
  </ion-content>
</ion-view>

应用程序 JS:

// to not forget to include ngCordova in order to use $cordovaFile in your controller
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services','ngCordova'])
...
...

Controller -JS:

angular.module('starter.controllers', []).controller('DashCtrl', function($scope,$cordovaFile) {

  $scope.pdfFilename = "test.pdf";

  $scope.makePDF = function(){
    var imgData = "your base64 image-data";

    var doc = new jsPDF();

    doc.setFontSize(40);
    doc.text(35, 25, "Octonyan loves jsPDF");
    doc.addImage(imgData, 'JPEG', 15, 40, 180, 180);
    return doc.output('blob');
  };

  $scope.getDirectoryInfo = function(){
    return {
      storage: cordova.file.externalRootDirectory, // for android, you may have to use another directory
      pdfDir: cordova.file.externalRootDirectory+"pdfs/"
    }
  };

  $scope.savePDF = function(){
    var dirs = $scope.getDirectoryInfo();
    var storageDir = dirs.storage;

    // create directory on the first place if it has not been created so far
    $cordovaFile.createDir(storageDir, "pdfs", false);

    var pdfBlob = $scope.makePDF();
    // create empty pdf-file(in the root-dir)
    $cordovaFile.createFile(storageDir, $scope.pdfFilename, true).then(function(success) {
      // write pdf-file(in the root dir)
      $cordovaFile.writeExistingFile(storageDir, $scope.pdfFilename, pdfBlob, true).then(function (success) {
        var fullTmpPath = storageDir + $scope.pdfFilename;
        window.resolveLocalFileSystemURL(fullTmpPath, function (fileEntry) {
          // get destination-directory to put your pdf into
          window.resolveLocalFileSystemURL(dirs.pdfDir, function (dirEntry) {
            // move written pdf to the destination-directory
            fileEntry.moveTo(dirEntry, $scope.pdfFilename, function (newFileEntry) {
              alert($scope.pdfFilename+' is finish! Now you can open it');
            });
          });
        });
      }, function (error) {
        console.log('there was some error while writting file: ',error);
      });
    });

  };

  $scope.openPDF =function(){
    var dirs = $scope.getDirectoryInfo();
    cordova.plugins.fileOpener2.open(dirs.pdfDir+$scope.pdfFilename,'application/pdf',function(){
      console.log('success');
    });
  };

});

尽管您的问题已在一段时间前提出,但希望这对您有所帮助。

关于javascript - JPG转PDF Cordova插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37338796/

相关文章:

ios - Visual Studio Cordova - 无法部署应用程序

ios - 隐藏 Ionic(AngularJS) 后退按钮不出现

android - 提高我的 ionic 应用程序的执行速度

javascript - 网络 Storm 中的"unresolved variable js"

javascript - 如何从左下角删除 MapBox Logo ?

javascript - 从 require 外部调用在 dojo 中 require 内部编写的函数

javascript - 将两个整数之间的所有数字相加

cordova - 安装后台地理定位插件后,Ionic 3 应用程序停止运行

angularjs - 如何将 ngCordova s​​qlite 服务和 Cordova-SQLitePlugin 与 Ionic Framework 一起使用?

javascript - --prod 在构建 Ionic 3 应用程序时不工作