我已经浏览了很多博客,我已成功在phonegap中创建pdf文件,但没有数据。
我的代码 javascrpit:
document.addEventListener("deviceready", createPDF, false);
function createPDF(){
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!');
//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 erp.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 );
});
}
和config.xml:
<plugin name="LocalFileSystem" value="org.apache.cordova.file"/>
并且在身体底部只添加了一个javascrpit
<script src="js/html_to_pdf/jspdf.source.js"></script>
如果您尝试此操作,那么控制台上将没有错误,并且将在手机内存上创建新文件,但是作为空文件,您将无法打开它,因为对话框将出现“文档大小为0”。< / p>
任何人都可以对此有任何参考或解决方案。 等待有价值的回复。
先谢谢。
答案 0 :(得分:0)
以下是我尝试使用异步文件API的内容。虽然我无法设置文件编码,但它生成了一些pdf
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
// onRequestFileSystemReady
function(fileSystem)
{
fileSystem.root.getFile("my.pdf",
{create : true},
// onGetFileReady
function(entry)
{
// entry is a "file entry"
entry.createWriter(
// onWriterReady
function(writer)
{
writer.onwrite = function(evt) {
console.log("write success");
};
console.log("writing to file");
writer.onerror= onWriteError;
var blobBuilderError= false;
window.BlobBuilder = window.BlobBuilder ||
window.WebKitBlobBuilder ||
window.MozBlobBuilder ||
window.MSBlobBuilder;
if ( typeof window.BlobBuilder === "undefined")
{
console.error("BlobBuilder, not defined");
blobBuilderError= true;
}
if(blobBuilderError !== true)
{
// it works with android not with ios
var bb = new BlobBuilder(),
blob;
bb.append(PdfOutput);
blob = bb.getBlob("text/plain");
writer.write(blob);
}
else
{
writer.write(PdfOutput);
}
},
onWriterError
);
},
onGetFileError
);
},
onRequestFileSystemError
);
onWriteError,onWriterError,onGetFileError,onRequestFileSystemError是错误处理程序正在执行的操作