phonegap教程使用jspdf庫在應(yīng)用中生成pdf文件(pdf生成方法)
首先在命令行創(chuàng)建一個(gè)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, 將目標(biāo)碼拷貝到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. 來生成一個(gè)簡單的PDF文件并保存至設(shè)備的本地。這個(gè)應(yīng)該算是*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)建過程其實(shí)很簡單。只要使用doc.output()獲取到已創(chuàng)建文件的字符串標(biāo)識就能做相應(yīng)的操作。不論是保存到本地,發(fā)送到服務(wù)器甚至是直接發(fā)送到本地設(shè)備上的PDF閱讀器中。
- Android使用phonegap從相冊里面獲取照片(代碼分享)
- APP添加CNZZ統(tǒng)計(jì)插件教程 Android版添加phonegap
- Phonegap使用拍照功能時(shí)的內(nèi)存問題
- android判斷phonegap是否聯(lián)網(wǎng)且加載super.loadUrl網(wǎng)址
- 深入理解移動(dòng)前端開發(fā)之viewport
- 自適應(yīng)布局meta標(biāo)簽中viewport、content、width、initial-scale、minimum-scale、maximum-scale總結(jié)
- 通過viewport實(shí)現(xiàn)jsp頁面支持手機(jī)縮放
- 關(guān)于viewport,Ext.panel和Ext.form.panel的關(guān)系
- ExtJs 學(xué)習(xí)筆記 Ext.Panle Ext.TabPanel Ext.Viewport
- 解決PhoneGap不支持viewport的幾種方法
相關(guān)文章
Android scrollTo和scrollBy方法使用解析
在一個(gè)View中,系統(tǒng)提供了scrollTo、scrollBy兩種方式來改變一個(gè)View的位置,下面通過本文給大家介紹Android scrollTo和scrollBy方法使用解析,需要的朋友參考下吧2018-01-01
Android實(shí)現(xiàn)RecyclerView嵌套流式布局的詳細(xì)過程
最近在做需求的時(shí)候,碰到有各種篩選項(xiàng)的界面,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)RecyclerView嵌套流式布局的詳細(xì)過程,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
android自定義view之實(shí)現(xiàn)日歷界面實(shí)例
本篇文章主要介紹了android自定義view之實(shí)現(xiàn)日歷界面實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
Android ImageView隨手勢變化動(dòng)態(tài)縮放圖片
這篇文章主要為大家詳細(xì)介紹了Android ImageView隨手勢變化動(dòng)態(tài)縮放圖片的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05
Android手動(dòng)檢查并申請權(quán)限方法
今天小編就為大家分享一篇Android手動(dòng)檢查并申請權(quán)限方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Android TextView設(shè)置背景色與邊框的方法詳解
本篇文章是對Android中TextView設(shè)置背景色與邊框的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

