cordova-pdf-generator
Advanced tools
Comparing version 1.5.6 to 1.9.0
{ | ||
"name": "cordova-pdf-generator", | ||
"version": "1.5.6", | ||
"version": "1.9.0", | ||
"description": "is a HTML to PDF (offline) Generator.", | ||
@@ -5,0 +5,0 @@ "cordova": { |
155
README.md
@@ -53,4 +53,110 @@ # cordova-pdf-generator | ||
## API | ||
Before using the plugin just make sure that the device is ready by listening to the onDeviceReady event: | ||
``` | ||
document.addEventListener('deviceready', function(){ | ||
// start using cordova plugin here. | ||
}) | ||
``` | ||
### Description | ||
The plugin expose a global variable named **pdf**, this variable expose the following functions. | ||
###### pdf.usingURL( url, options ) | ||
Creates a PDF using a URL, it download the document into an in memory Webkit object, and renders it into a PDF. | ||
- **url** : Takes the URL with the HTML document you want to transform to PDF, once the document finish loading is render by webkit and transformed into a PDF file. | ||
Example: | ||
``` | ||
let options = { | ||
documentSize: "A4", | ||
type: "base64" | ||
} | ||
pdf.usingURL( "http://www.google.es", options ) | ||
.then(()=>'ok') | ||
.catch((err)=>console.err(err)) | ||
``` | ||
###### pdf.usingData( url, options ) | ||
Creates a PDF using string with the HTML representation, it download the document into an in memory Webkit object, and renders it into a PDF. | ||
- **data** : Takes a string representing the HTML document, it load this in Webkit and creates a PDF. | ||
Example: | ||
``` | ||
let options = { | ||
documentSize: "A4", | ||
type: "base64" | ||
} | ||
pdf.usingData( "<html> <h1> Hello World </h1> </html>", options ) | ||
.then((base64)=>'ok') // it will | ||
.catch((err)=>console.err(err)) | ||
``` | ||
#### Options | ||
##### documentSize | ||
- Its take ```A4, A3, A2``` this specify the format of the paper, just available in iOS, in Android this option is ignored. | ||
##### type | ||
- ```base64``` it will return a Base64 representation of the PDF file. ```{ type: 'base64' } ``` | ||
``` | ||
let options = { | ||
documentSize: "A4", | ||
type: "base64" | ||
} | ||
pdf.usingData( "<html> <h1> Hello World </h1> </html>", options ) | ||
.then((base64)=> console.log(base64) ) // returns base64:JVBERi0xLjQKJdPr6eEKMSAwIG9iago8PC9DcmVh... | ||
.catch((err)=>console.err(err)) | ||
``` | ||
- ```share``` It will delegate the file to the OS printing infraestructure, this basically will allow the user to handle the file himself using the mobile OS features available. | ||
``` | ||
let options = { | ||
documentSize: "A4", | ||
type: "share" | ||
} | ||
pdf.usingData( "<html> <h1> Hello World </h1> </html>", options ) | ||
.then((stats)=> console.log('status', stats) ) // ok..., ok if it was able to handle the file to the OS. | ||
.catch((err)=>console.err(err)) | ||
``` | ||
##### filename | ||
- You can specify the name of the PDF file. | ||
**Legacy Examples** | ||
Here are examples to use the legacy method pdf API below 1.9. | ||
This generates a pdf from a URL, it convert HTML to PDF and returns the file representation in base64. | ||
@@ -107,3 +213,3 @@ | ||
//Example: file:///android_asset/index.html | ||
//Example: file:///android_asset/index.html | ||
@@ -114,5 +220,5 @@ function printInternalFile(param) { | ||
if(cordova.platformId === 'ios') { | ||
// To use window.resolveLocalFileSystemURL, we need this plugin https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/ | ||
// You can add this by doing cordova plugin add cordova-plugin-file or | ||
// To use window.resolveLocalFileSystemURL, we need this plugin https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/ | ||
// You can add this by doing cordova plugin add cordova-plugin-file or | ||
// cordova plugin add https://github.com/apache/cordova-plugin-file | ||
@@ -123,8 +229,9 @@ window.resolveLocalFileSystemURL(cordova.file.applicationDirectory, | ||
pdf.htmlToPDF({ | ||
url: file, | ||
documentSize: "A4", | ||
pdf.fromURL(file, { | ||
documentsize: "a4", | ||
landscape: "portrait", | ||
type: "share" | ||
}, this.success, this.failure); | ||
}) | ||
.then((stats)=> this.preparetogobackground ) | ||
.catch((err)=> this.showerror) | ||
}, | ||
@@ -135,14 +242,14 @@ (err) => | ||
}else { | ||
pdf.htmlToPDF({ | ||
url: param, | ||
documentSize: "A4", | ||
landscape: "portrait", | ||
type: "share" | ||
}, this.success, this.failure); | ||
} | ||
pdf.fromURL(param, { | ||
documentsize: "a4", | ||
landscape: "portrait", | ||
type: "share" | ||
}) | ||
.then((stats)=> this.preparetogobackground ) | ||
.catch((err)=> this.showerror) | ||
} | ||
} | ||
``` | ||
[Gist](https://github.com/brentnycum/BNHtwmlPdfKi://gist.github.com/cesarvr/6dc7156963dbfa7606b54c85fae84dba§) | ||
[Gist](https://gist.github.com/cesarvr/6dc7156963dbfa7606b54c85fae84dba) | ||
@@ -172,11 +279,5 @@ | ||
//generate the pdf. | ||
cordova.plugins.pdf.htmlToPDF({ | ||
data: "<html> <h1> Hello World </h1> </html>", | ||
//url: "www.cloud.org/template.html" | ||
}, | ||
(sucess) => console.log('sucess: ', sucess), | ||
(error) => console.log('error:', error)); | ||
}); | ||
cordova.plugins.pdf.usingData( "<html> <h1> Hello World </h1> </html>", options ) | ||
.then(()=>'ok') | ||
.catch((err)=>console.err(err)) | ||
} | ||
@@ -217,3 +318,3 @@ | ||
- landscape: parameter is ignored but required. | ||
- type: | ||
- type: | ||
- *base64* give you the pdf in Base64 format. | ||
@@ -220,0 +321,0 @@ - *share* opens Android native PDF viewer. |
/*global cordova, module*/ | ||
function opts(options){ | ||
options.documentSize = options.documentSize || "A4"; | ||
options.landscape = options.landscape || "portrait"; | ||
options.type = options.type || "base64"; | ||
options.fileName = options.fileName || "default.pdf"; | ||
return options; | ||
} | ||
function validate(param, message){ | ||
if(param === '' || | ||
param === undefined || | ||
param === null | ||
) | ||
throw message | ||
} | ||
module.exports = { | ||
htmlToPDF: function (options, successCallback, errorCallback) { | ||
if(!options.url && !options.data) throw "No URL or HTML Data found."; | ||
var url = options.url; | ||
@@ -13,6 +33,45 @@ var data = options.data; | ||
var type = options.type || "base64"; | ||
var fileName = options.fileName || "default"; | ||
var fileName = options.fileName || "default.pdf"; | ||
cordova.exec(successCallback, errorCallback, "PDFService", "htmlToPDF", [ url, data, docSize, landscape, type, fileName ]); | ||
}, | ||
fromURL: function(url, options){ | ||
return new Promise(function(resolve, reject){ | ||
debugger | ||
validate(url, "URL is required") | ||
options = opts(options) | ||
cordova.exec(resolve, | ||
reject, | ||
"PDFService", | ||
"htmlToPDF", [ url, | ||
null, | ||
options.documentSize, | ||
options.landscape, | ||
options.type, | ||
options.fileName ]); | ||
}) | ||
}, | ||
fromData: function(data, options){ | ||
return new Promise(function(resolve, reject){ | ||
debugger | ||
validate(data, "String with HTML format is required") | ||
options = opts(options) | ||
cordova.exec(resolve, | ||
reject, | ||
"PDFService", | ||
"htmlToPDF", [ null, | ||
data, | ||
options.documentSize, | ||
options.landscape, | ||
options.type, | ||
options.fileName ]); | ||
}) | ||
} | ||
}; |
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
72245
17
59
338