Skip to content

Instantly share code, notes, and snippets.

@kumarandena
Created May 3, 2019 06:21
Show Gist options
  • Save kumarandena/b98ad19a631fd3ca82316f8e44bb8583 to your computer and use it in GitHub Desktop.
Save kumarandena/b98ad19a631fd3ca82316f8e44bb8583 to your computer and use it in GitHub Desktop.
PdfMake Javascript library integration with NativeScript Angular
PdfMake Javascript library integration with NativeScript Angular
Library Github Repo - https://github.com/bpampuch/pdfmake
Integration:
1. Let’s create Nativescript angular project
Install PdfMake Library
npm i pdfmake
2. Verify whether it is installed or not
Package.json
"dependencies": {
"pdfmake": "^0.1.41"
}
3. Start using the pdfmake in component
import * as pdfMake from 'pdfmake/build/pdfmake.js';
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();
//you can use any font family and must supply Italic, Medium, MediumItalic, and Regular styles as param
public vfs = {
"Roboto-Italic.ttf": base64string,
"Roboto-Medium.ttf": base64string,
"Roboto-MediumItalic.ttf": base64string,
"Roboto-Regular.ttf": base64string
}
//generate the dataurl of the resulted pdf
generatePdf() {
var docDefinition = { content: 'This is an sample PDF printed with NativeScript Angular app using pdfMake'};
pdfMake.createPdf(docDefinition, ' ', ' ', this.vfs)
.getDataUrl((dataUrl) => {
let sliced = dataUrl.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, "PdfMake 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);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment