【问题标题】:How to create and save pdf file using CORDOVA on mobile device如何在移动设备上使用 CORDOVA 创建和保存 pdf 文件
【发布时间】:2016-11-02 02:52:41
【问题描述】:

我正在尝试在移动设备上创建和保存 PDF 文件。我正在使用带有 jsPdf 的“离子”框架。 当我在浏览器上运行脚本时,它会创建 PDF 文件并下载到本地机器上。但我不知道如何将该 PDF 文档保存在移动本地目录中。我尝试了以下代码。

var pdfOutput = pdf.output("blob");

ocument.addEventListener('deviceready', function () {
                 alert('device is ready, lets do some file reading pdf');
                 $cordovaFile.createFile(cordova.file.externalRootDirectory, pdfOutput, true)
                 .then(function (success) {
                     alert('successfully created pdf file');
                  }, function (error){
                 alert('file write error' + error); 
              }); });

有人可以帮我吗?如果有人告诉我创建应用程序文件夹并将所有 pdf 文件存储在该文件夹中的过程,那将是非常棒的。

【问题讨论】:

标签: cordova pdf ionic-framework hybrid-mobile-app jspdf


【解决方案1】:

您好,您可以使用 JsPDF,它很容易与 cordova 集成,网址是 https://parall.ax/products/jspdf 第 1 步下载 JSPDF 项目代码,并将 jspdf 库从“dist”目录复制到您的 cordova 项目中,您将在其中保留所有 js 库 t.我把它放到 www/jslib 中。然后,确保在您的主 HTML 文件中包含该库。

示例代码

  //FIRST GENERATE THE PDF DOCUMENT
  console.log("generating pdf...");
  var doc = new jsPDF();

  doc.text(20, 20, 'HELLO!');

  doc.setFont("courier");
  doc.setFontType("normal");
  doc.text(20, 30, 'This is a PDF document generated using JSPDF.');
  doc.text(20, 50, 'YES, Inside of cordova!');

  var pdfOutput = doc.output();
  console.log( pdfOutput );

  //NEXT SAVE IT TO THE DEVICE'S LOCAL FILE SYSTEM
  console.log("file system...");
  window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {

     console.log(fileSystem.name);
     console.log(fileSystem.root.name);
     console.log(fileSystem.root.fullPath);

     fileSystem.root.getFile("test.pdf", {create: true}, function(entry) {
        var fileEntry = entry;
        console.log(entry);

  entry.createWriter(function(writer) {
     writer.onwrite = function(evt) {
     console.log("write success");
  };

        console.log("writing to file");
           writer.write( pdfOutput );
        }, function(error) {
           console.log(error);
        });

     }, function(error){
        console.log(error);
     });
  },
  function(event){
   console.log( evt.target.error.code );
  });

【讨论】:

  • 感谢您的代码,我试过了,没有出现任何错误,您能告诉我在哪里可以找到我的 android 设备上的 test.pdf 文件吗?
  • @user3136884 你找到文件了吗?
  • 我有同样的问题,答案只是创建和保存
【解决方案2】:

安装cordova plugin add cordova-pdf-generator

使用以下代码。

 $scope.Function_name = function() {

        var opts = {
            type: "share", //Open a context menu and ask the user what to do next (print, mail, etc..).
            fileName: 'v8-tutorial.pdf' //it will use this filename as a place-holder
        }

        pdf.fromData('<html><h1>Hello World</h1></html>', opts)
            .then((status) => console.log('success->', status))
            .catch((error) => console.log(error));

        // End here

    }

【讨论】:

    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    相关资源
    最近更新 更多