phonegap教程使用jspdf库在应用中生成pdf文件(pdf生成方法)

前端技术 2023/09/06 Android

首先在命令行创建一个PhoneGap工程

download the JSPDF project code, 将目标码拷贝到PhoneGap工程目录下。我放在 www/js下。然后,在main HTML文件中引入该文件。

复制代码 代码如下:

<script type=\"text/javascript\" src=\"js/jspdf.source.js\"></script>

我用的是\'dist\'目录下未经压缩/最小化的源文件。

接下来我们开始生成PDF文件。下面的代码片段利用PhoneGap的文件处理 API PhoneGap\'s File API.  来生成一个简单的PDF文件并保存至设备的本地。这个应该算是*AFTER* the deviceready事件。
其中console.log只是为了调试使用:

复制代码 代码如下:

//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 PhoneGap!\');

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 );
});

本文地址:https://www.stayed.cn/item/16704

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。