cordova-pdf-generator
Advanced tools
Comparing version 2.0.3 to 2.0.4
{ | ||
"name": "cordova-pdf-generator", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "is a HTML to PDF (offline) Generator.", | ||
@@ -5,0 +5,0 @@ "cordova": { |
@@ -296,2 +296,98 @@ # cordova-pdf-generator | ||
##### Saving a pdf file directly into the file system | ||
If you'd like to directly save the pdf file into the Downloads directory of the device, or any other, without asking the user for what to do with the file, you will need to use type as `base64` and then use such information to save the pdf into a file. For this you will need the plugin `cordova-plugin-file`. | ||
Here is an example | ||
```js | ||
var fileName = "myPdfFile.pdf"; | ||
var options = { | ||
documentSize: 'A4', | ||
type: 'base64' | ||
}; | ||
var pdfhtml = '<html><body style="font-size:120%">This is the pdf content</body></html>'; | ||
pdf.fromData(pdfhtml , options) | ||
.then(function(base64){ | ||
// To define the type of the Blob | ||
var contentType = "application/pdf"; | ||
// if cordova.file is not available use instead : | ||
// var folderpath = "file:///storage/emulated/0/Download/"; | ||
var folderpath = cordova.file.externalRootDirectory + "Download/"; //you can select other folders | ||
savebase64AsPDF(folderpath, fileName, base64, contentType); | ||
}) | ||
.catch((err)=>console.err(err)); | ||
``` | ||
You will also need these two functions. Due to javascript functions hoisting you can declare them afterwards as here: | ||
```js | ||
/** | ||
* Convert a base64 string in a Blob according to the data and contentType. | ||
* | ||
* @param b64Data {String} Pure base64 string without contentType | ||
* @param contentType {String} the content type of the file i.e (application/pdf - text/plain) | ||
* @param sliceSize {Int} SliceSize to process the byteCharacters | ||
* @see http://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript | ||
* @return Blob | ||
*/ | ||
function b64toBlob(b64Data, contentType, sliceSize) { | ||
contentType = contentType || ''; | ||
sliceSize = sliceSize || 512; | ||
var byteCharacters = atob(b64Data); | ||
var byteArrays = []; | ||
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) { | ||
var slice = byteCharacters.slice(offset, offset + sliceSize); | ||
var byteNumbers = new Array(slice.length); | ||
for (var i = 0; i < slice.length; i++) { | ||
byteNumbers[i] = slice.charCodeAt(i); | ||
} | ||
var byteArray = new Uint8Array(byteNumbers); | ||
byteArrays.push(byteArray); | ||
} | ||
var blob = new Blob(byteArrays, {type: contentType}); | ||
return blob; | ||
} | ||
/** | ||
* Create a PDF file according to its database64 content only. | ||
* | ||
* @param folderpath {String} The folder where the file will be created | ||
* @param filename {String} The name of the file that will be created | ||
* @param content {Base64 String} Important : The content can't contain the following string (data:application/pdf;base64). Only the base64 string is expected. | ||
*/ | ||
function savebase64AsPDF(folderpath,filename,content,contentType){ | ||
// Convert the base64 string in a Blob | ||
var DataBlob = b64toBlob(content,contentType); | ||
console.log("Starting to write the file :3"); | ||
window.resolveLocalFileSystemURL(folderpath, function(dir) { | ||
console.log("Access to the directory granted succesfully"); | ||
dir.getFile(filename, {create:true}, function(file) { | ||
console.log("File created succesfully."); | ||
file.createWriter(function(fileWriter) { | ||
console.log("Writing content to file"); | ||
fileWriter.write(DataBlob); | ||
}, function(){ | ||
alert('Unable to save file in path '+ folderpath); | ||
}); | ||
}); | ||
}); | ||
} | ||
``` | ||
#### Deprecated | ||
@@ -298,0 +394,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
770262
467