Reputation: 53
Hi I try to save a PDF Datastring in Phonegap with the cordova-plugin-file
Sample:
%PDF-1.3
%ºß¬à
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/MediaBox [0 0 595.28 841.89]
/Contents 4 0 R
>>
endobj
4 0 obj
<</Length 8757>>
to a PDF-File with this function:
savePDF("Storecheck"+surveyID+".pdf", pdfOutput)
function savePDF(fileName, fileData) {
console.log(fileData);
//function writeToFile(fileName, data) {
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(directoryEntry) {
directoryEntry.getFile(fileName, {
create: true
}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
// for real-world usage, you might consider passing a success callback
console.log('Speichern von "' + fileName + '"" abgeschlossen.');
};
fileWriter.onerror = function(e) {
// you could hook this up with our global error handler, or pass in an error callback
console.log('speichern fehlgeschlagen ' + e.toString());
};
var blob = new Blob([fileData], {
type: 'application/pdf'
});
fileWriter.write(blob);
}, );
}, );
}, );
}
I get no error messages but the file does not appear. Also, I would like to save the PDF in "documents" and not in ".file.dataDirectory"
Can someone tell me what I am doing wrong?
Upvotes: 1
Views: 1731
Reputation: 53
Oh yaay I solved it after three days
actually, I only had to change one line of code
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
LocalFileSystem.PERSISTENT only works on Android WITH ROOT
LocalFileSystem.PERSISTENT stands for the system URL in Android wich what you can see in the documentation: File System Layouts
I changed it to cordova.file.externalDataDirectory
On Phongap Desktop the output NULL you have tu build the app in order to work
so i came out with this:
savePDF("Storecheck.pdf", pdfOutput)
function savePDF(fileName, fileData) {
window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (dir) {
dir.getFile(fileName, {create: true, exclusive: false}, function (fileEntry) {
fileEntry.createWriter(function (writer) {
writer.onwrite = function (evt) {
console.log("write success");
alert(fileName+" wurde gespeichert unter " + cordova.file.externalDataDirectory );
};
console.log("writing to file");
writer.write(fileData);
})
}, function () {
console.log("ERROR SAVEFILE");
alert ("heute gibts keine PDF")
});
});
}
Upvotes: 2
Reputation: 53
Acording to the cordova-plugin-file documentaion I changed the funtion. https://jsfiddle.net/frief/7umnwj0r/1/
I have also added
<preference name="AndroidPersistentFileLocation" value="Internal" />
<preference name="AndroidPersistentFileLocation" value="Compatibility" />
<preference name="AndroidExtraFilesystems" value="sdcard,cache" />
<?xml version='1.0' encoding='utf-8'?>
<widget id="de.check" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
<name> check</name>
<description>
check
</description>
<author email="[email protected]" href="http://ally.de">
Development
</author>
<preference name="AndroidPersistentFileLocation" value="Internal" />
<preference name="AndroidPersistentFileLocation" value="Compatibility" />
<preference name="AndroidExtraFilesystems" value="sdcard,cache" />
<preference name="iosPersistentFileLocation" value="Library" />
<preference name="iosPersistentFileLocation" value="Compatibility" />
<!-- Don't block any requests -->
<access origin="*" />
<content src="index.html" />
<plugin name="cordova-plugin-battery-status" spec="~1.2.2" />
<plugin name="cordova-plugin-camera" spec="~2.3.1" />
<plugin name="cordova-plugin-file" spec="~4.3.1" />
<plugin name="cordova-plugin-file-transfer" spec="~1.6.1" />
<plugin name="cordova-plugin-geolocation" spec="~2.4.1" />
<plugin name="cordova-plugin-vibration" spec="~2.1.3" />
<plugin name="cordova-plugin-statusbar" spec="~2.2.1" />
<plugin name="cordova-plugin-whitelist" source="npm"/>
<engine name="android" spec="^6.4.0" />
</widget>
Now I have the problem that I always get the error that my file could not be created. I think it has something to do with the access rights, but I do not know exactly.
Upvotes: 0