Skip to content

Instantly share code, notes, and snippets.

@kumarandena
Created May 3, 2019 06:21
Show Gist options
  • Save kumarandena/558c8fd405c0f2758c467a7eb63de551 to your computer and use it in GitHub Desktop.
Save kumarandena/558c8fd405c0f2758c467a7eb63de551 to your computer and use it in GitHub Desktop.
jsPDF Javascript library integration with NativeScript Angular
Library Github Repo - https://github.com/MrRio/jsPDF
Integration:
1. Let’s create Nativescript angular project
npm install jspdf –save
npm install @types/jspdf –save
npm install base-64 --save
2. Verify whether it is installed or not
Package.json
"dependencies": {
"@types/jspdf": "^1.2.2",
"base-64": "^0.1.0",
"jspdf": "1.3.5"
}
3. Start using the jsPDF in component
//You can create a fork of jsPDF to remove the following global variables for NativeScript
global['window'] = {
'document': {
'createElementNS': () => { return {} }
}
};
global['document'] = {
'createElement': (str) => { return {} }
};
global['navigator'] = {};
//Fix the variables using imports
var base64 = require('base-64');
import * as jsPDF from 'jspdf';
global['btoa'] = (str) => {
return base64.encode(str);
};
global['atob'] = (bytes) => {
return base64.decode(bytes);
};
global['utf8'] = {};
import { File, Folder, path } from "file-system";
declare var android: any;
//to get android downloads path
public basePath = android.os.Environment.getExternalStoragePublicDirectory(
android.os.Environment.DIRECTORY_DOWNLOADS).toString();
//generate the dataurl of the resulted pdf
generatePdf() {
var doc = new jsPDF('p', 'pt', 'a4');
doc.setFontSize(26);
doc.text(40, 40, "First PDF with NativeScript Angular using jsPDF!");
let imgData = base64string;
doc.addImage(imgData, 'JPEG', 0, 0, 100, 100, "newCertificate", 'FAST');
var base64 = doc.output('datauristring');
if (base64) {
let sliced = base64.toString().slice(28);
this.savePdf(sliced);
}
}
//save the base64 as pdf file using file system
savePdf(encodedData) {
let folder = Folder.fromPath(path.join(this.basePath, "jsPDF Files"));
let tofile: File = folder.getFile('nscertificate.pdf');
if (tofile) {
let data = android.util.Base64.decode(encodedData, android.util.Base64.DEFAULT);
tofile.writeSync(data, err =>
{
console.log("err :", err);
});
}
}
4. Steps to add Image:
1. Add atob global variable
2. Add btoa global variable
3. Add utf8 global variable
4. Supply only the base64 encoded JPEG images
(https://www.base64-image.de/)
(https://parall.ax/products/jspdf)
(https://stackoverflow.com/questions/19072241/jspdf-not-working-with-images)
@madmas
Copy link

madmas commented Aug 24, 2022

Hi @kumarandena ,

unfortunately this does not work anymore, I get this exception:

System.err: An uncaught Exception occurred on "main" thread.
  System.err: Unable to create application com.tns.NativeScriptApplication: com.tns.NativeScriptException: Error calling module function
  System.err: TypeError: Cannot read property 'bind' of undefined
  System.err: File: (file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:27:0)
  System.err:
  System.err: StackTrace:
  System.err: (file: src/webpack:/nativescript-pdf-sample-angular/node_modules/jspdf/dist/jspdf.es.js:465:27)
  System.err: 	at ./node_modules/jspdf/dist/jspdf.es.js(file: src/webpack:/nativescript-pdf-sample-angular/node_modules/jspdf/dist/jspdf.es.js:464:0)
  System.err: 	at __webpack_require__(file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:24:0)
  System.err: 	at fn(file: src/webpack:/nativescript-pdf-sample-angular/webpack/runtime/hot module replacement:62:0)
  System.err: 	at ./src/app/item/item-detail.component.ts(file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:235:80)
  System.err: 	at __webpack_require__(file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:24:0)
  System.err: 	at fn(file: src/webpack:/nativescript-pdf-sample-angular/webpack/runtime/hot module replacement:62:0)
  System.err: 	at ./src/app/app-routing.module.ts(file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:155:85)
  System.err: 	at __webpack_require__(file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:24:0)
  System.err: 	at fn(file: src/webpack:/nativescript-pdf-sample-angular/webpack/runtime/hot module replacement:62:0)
  System.err: 	at ./src/app/app.module.ts(file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:207:77)
  System.err: 	at __webpack_require__(file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:24:0)
  System.err: 	at fn(file: src/webpack:/nativescript-pdf-sample-angular/webpack/runtime/hot module replacement:62:0)
  System.err: 	at ./src/main.ts(file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:14:73)
  System.err: 	at __webpack_require__(file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:24:0)
  System.err: 	at __webpack_exec__(file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:428:39)
  System.err: 	at (file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:429:189)
  System.err: 	at __webpack_require__.X(file: src/webpack:/nativescript-pdf-sample-angular/webpack/runtime/startup entrypoint:6:0)
  System.err: 	at (file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:429:47)
  System.err: 	at (file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:434:3)
  System.err: 	at require(:1:266)
  System.err:
  System.err:
  System.err: TypeError: Cannot read property 'bind' of undefined
  System.err:
  System.err: StackTrace:
  System.err: java.lang.RuntimeException: Unable to create application com.tns.NativeScriptApplication: com.tns.NativeScriptException: Error calling module function
  System.err: TypeError: Cannot read property 'bind' of undefined
  System.err: File: (file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:27:0)
  System.err:
  System.err: StackTrace:
  System.err: (file: src/webpack:/nativescript-pdf-sample-angular/node_modules/jspdf/dist/jspdf.es.js:465:27)
  System.err: 	at ./node_modules/jspdf/dist/jspdf.es.js(file: src/webpack:/nativescript-pdf-sample-angular/node_modules/jspdf/dist/jspdf.es.js:464:0)
  System.err: 	at __webpack_require__(file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:24:0)
  System.err: 	at fn(file: src/webpack:/nativescript-pdf-sample-angular/webpack/runtime/hot module replacement:62:0)
  System.err: 	at ./src/app/item/item-detail.component.ts(file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:235:80)
  System.err: 	at __webpack_require__(file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:24:0)
  System.err: 	at fn(file: src/webpack:/nativescript-pdf-sample-angular/webpack/runtime/hot module replacement:62:0)
  System.err: 	at ./src/app/app-routing.module.ts(file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:155:85)
  System.err: 	at __webpack_require__(file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:24:0)
  System.err: 	at fn(file: src/webpack:/nativescript-pdf-sample-angular/webpack/runtime/hot module replacement:62:0)
  System.err: 	at ./src/app/app.module.ts(file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:207:77)
  System.err: 	at __webpack_require__(file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:24:0)
  System.err: 	at fn(file: src/webpack:/nativescript-pdf-sample-angular/webpack/runtime/hot module replacement:62:0)
  System.err: 	at ./src/main.ts(file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:14:73)
  System.err: 	at __webpack_require__(file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:24:0)
  System.err: 	at __webpack_exec__(file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:428:39)
  System.err: 	at (file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:429:189)
  System.err: 	at __webpack_require__.X(file: src/webpack:/nativescript-pdf-sample-angular/webpack/runtime/startup entrypoint:6:0)
  System.err: 	at (file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:429:47)
  System.err: 	at (file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:434:3)
  System.err: 	at require(:1:266)
  System.err:
  System.err:
  System.err: TypeError: Cannot read property 'bind' of undefined
  System.err: 	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6596)
  System.err: 	at android.app.ActivityThread.access$1500(ActivityThread.java:230)
  System.err: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1911)
  System.err: 	at android.os.Handler.dispatchMessage(Handler.java:107)
  System.err: 	at android.os.Looper.loop(Looper.java:224)
  System.err: 	at android.app.ActivityThread.main(ActivityThread.java:7551)
  System.err: 	at java.lang.reflect.Method.invoke(Native Method)
  System.err: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
  System.err: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)
  System.err: Caused by: com.tns.NativeScriptException: Error calling module function
  System.err: TypeError: Cannot read property 'bind' of undefined
  System.err: File: (file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:27:0)
  System.err:
  System.err: StackTrace:
  System.err: (file: src/webpack:/nativescript-pdf-sample-angular/node_modules/jspdf/dist/jspdf.es.js:465:27)
  System.err: 	at ./node_modules/jspdf/dist/jspdf.es.js(file: src/webpack:/nativescript-pdf-sample-angular/node_modules/jspdf/dist/jspdf.es.js:464:0)
  System.err: 	at __webpack_require__(file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:24:0)
  System.err: 	at fn(file: src/webpack:/nativescript-pdf-sample-angular/webpack/runtime/hot module replacement:62:0)
  System.err: 	at ./src/app/item/item-detail.component.ts(file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:235:80)
  System.err: 	at __webpack_require__(file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:24:0)
  System.err: 	at fn(file: src/webpack:/nativescript-pdf-sample-angular/webpack/runtime/hot module replacement:62:0)
  System.err: 	at ./src/app/app-routing.module.ts(file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:155:85)
  System.err: 	at __webpack_require__(file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:24:0)
  System.err: 	at fn(file: src/webpack:/nativescript-pdf-sample-angular/webpack/runtime/hot module replacement:62:0)
  System.err: 	at ./src/app/app.module.ts(file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:207:77)
  System.err: 	at __webpack_require__(file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:24:0)
  System.err: 	at fn(file: src/webpack:/nativescript-pdf-sample-angular/webpack/runtime/hot module replacement:62:0)
  System.err: 	at ./src/main.ts(file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:14:73)
  System.err: 	at __webpack_require__(file: src/webpack:/nativescript-pdf-sample-angular/webpack/bootstrap:24:0)
  System.err: 	at __webpack_exec__(file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:428:39)
  System.err: 	at (file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:429:189)
  System.err: 	at __webpack_require__.X(file: src/webpack:/nativescript-pdf-sample-angular/webpack/runtime/startup entrypoint:6:0)
  System.err: 	at (file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:429:47)
  System.err: 	at (file:///data/data/org.nativescript.pdfsample/files/app/bundle.js:434:3)
  System.err: 	at require(:1:266)
  System.err:
  System.err:
  System.err: TypeError: Cannot read property 'bind' of undefined
  System.err: 	at com.tns.Runtime.runModule(Native Method)
  System.err: 	at com.tns.Runtime.runModule(Runtime.java:689)
  System.err: 	at com.tns.Runtime.run(Runtime.java:681)
  System.err: 	at com.tns.NativeScriptApplication.onCreate(NativeScriptApplication.java:21)
  System.err: 	at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1189)
  System.err: 	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6591)
  System.err: 	... 8 more

at the given code, jsPDF tries to bin atob function of the global object. Do you have an idea how to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment