Javascript JPG到PDF转换Cordova插件

Javascript JPG到PDF转换Cordova插件,javascript,cordova,ionic-framework,cordova-plugins,jspdf,Javascript,Cordova,Ionic Framework,Cordova Plugins,Jspdf,我使用的是ionic框架,我想从galley中选取一个图像文件,并将其转换为PDF格式,然后将其保存在手机上的文件系统中。 是否有javascript或cordova插件可用于执行此任务 我发现它可以用来将图像转换为pdf格式,但它真的可以在手机上使用吗?我希望我没有那么晚 除此之外,还需要以下软件包: cordova插件文件 cordova-plugin-file-opener2 并安装相应的离子包: bower install ngCordova 请注意,关于如何在文件系统中保存pdf

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


我发现它可以用来将图像转换为pdf格式,但它真的可以在手机上使用吗?

我希望我没有那么晚

除此之外,还需要以下软件包:

  • cordova插件文件
  • cordova-plugin-file-opener2
并安装相应的离子包:

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

iOS:您可以修改存储目录,使其也可以在iOS版本上工作

Index.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>
控制器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');
    });
  };

});

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

通过使用jsPDF,我认为它应该可以在移动环境下工作。@ArpitVasani我可以将PDF保存在中吗filesystem@Anil8753我建议你看看这个链接-。你所需要做的就是将你的JPG文件转换成base64图像URI,并输入插件以获取PDF格式。希望有帮助。@Anil8753是的,你可以。一旦jsPDF准备好PDF,然后使用将其保存到设备中。
// to not forget to include ngCordova in order to use $cordovaFile in your controller
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services','ngCordova'])
...
...
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');
    });
  };

});