Trying To Save A Pdf File With Intel Xdk
I´m trying to recreate this example. But When I click the button I get the error: 'Uncaught ReferenceError: LocalFileSystem is not defined'. Here's my code:
Solution 1:
Here's a modification of your code that I tested locally using Chrome 53 and Windows 7.
The main changes are:
- Before saving to the device, you need to call
requestQuota()
to get space on the local device - see the documentation and this question for an example; - Convert the string returned by
pdf.output()
to aBlob
for theFileWriter
; - Remove the unnecessary reference to
LocalFileSystem
.
The location of the written file depends on your operating system and browser; if you're using Chrome, you can find more information about where the file was written here.
On Windows 7, I found the file here: C:\Users\USERNAME\AppData\Local\Google\Chrome\User Data\Default\File System\018\p\00\00000016
I was able to open it in Adobe Reader by appending .pdf
to the filename.
<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"><title>jsPDF</title><scriptsrc="jquery.min.js"></script><scriptsrc="jspdf.js"></script><scriptsrc="FileSaver.js"></script><scriptsrc="html2canvas.js"></script><script>window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
functionsaveFile(){
var pdf = newjsPDF('p', 'pt', 'a4');
var source = $('#spendingTable')[0];
varPDFFILE ='';
var filename = prompt("Enter a file name:");
pdf.specialElementHandlers = {
'#bypassme': function (element, renderer) {
returntrue;
}
};
pdf.fromHTML(source, 15, 15, {'width': 170}, function (dispose) {
PDFFILE = newBlob([pdf.output()], {type: "application/pdf"});
});
//NEXT SAVE IT TO THE DEVICEvar requestedBytes = 1024*1024*10; // 10MB
navigator.webkitPersistentStorage.requestQuota (
requestedBytes, function (grantedBytes) {
window.requestFileSystem(PERSISTENT, 1024*1024, function (fileSystem) {
fileSystem.root.getFile(filename +".pdf", {create: true}, function (entry) {
var fileEntry = entry;
entry.createWriter(function(writer) {
writer.onwrite = function(evt) {
alert("Saved to root folder!");
};
writer.write(PDFFILE);
},
function (error) {
alert(error);
});
},
function (error) {
alert(error);
});
},
function (event) {
alert(event.target.error.code);
});
}
);
}
</script></head><body><h1>JSPDF EXAMPLE</h1><hr><divcontenteditable="true"id="spendingTable"style="border: 1px black solid">This text is saved into a PDF.</div><br><inputtype="button"value="Save"onclick="saveFile();"></body>
Post a Comment for "Trying To Save A Pdf File With Intel Xdk"