欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2014年01月11日 09:08:13   作者:  
在PhoneGap應用中生成pdf文件,實現(xiàn)起來很簡單,使用JSPDF這個標準的JavaScript類庫來實現(xiàn)這個功能

首先在命令行創(chuàng)建一個PhoneGap工程

復制代碼 代碼如下:

phonegap create . "jspdf.sample" "JSPDF App"
phonegap local plugin add org.apache.cordova.file
phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-console.git

然后,下載JSPDF代碼 download the JSPDF project code, 將目標碼拷貝到PhoneGap工程目錄下。我放在 www/js下。然后,在main HTML文件中引入該文件。

復制代碼 代碼如下:

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

我用的是'dist'目錄下未經(jīng)壓縮/最小化的源文件。

接下來我們開始生成PDF文件。下面的代碼片段利用PhoneGap的文件處理 API PhoneGap's File API.  來生成一個簡單的PDF文件并保存至設備的本地。這個應該算是*AFTER* the deviceready事件。
其中console.log只是為了調(diào)試使用:

復制代碼 代碼如下:

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

PDF創(chuàng)建過程其實很簡單。只要使用doc.output()獲取到已創(chuàng)建文件的字符串標識就能做相應的操作。不論是保存到本地,發(fā)送到服務器甚至是直接發(fā)送到本地設備上的PDF閱讀器中。

相關文章

最新評論