Skip to content

Instantly share code, notes, and snippets.

@misha130
Last active June 22, 2017 18:06
Show Gist options
  • Save misha130/1313bc496f3fb9d677ff82b52e8d4d73 to your computer and use it in GitHub Desktop.
Save misha130/1313bc496f3fb9d677ff82b52e8d4d73 to your computer and use it in GitHub Desktop.
PDF Handler class
import { Injectable } from '@angular/core';
import pdfMake from 'pdfmake/build/pdfmake';
import './CustomPDFFont';
@Injectable()
export
class PDFHandler {
constructor() {
pdfMake.fonts = {
alef: {
normal: 'Alef-Bold.ttf', bold: 'Alef-Bold.ttf', italics: 'Alef-Bold.ttf"', bolditalics: 'Alef-Bold.ttf',
}
};
}
public getPDFTable(columns: any[], data: any[][], name: string, tableOptions?: any): void {
data.unshift(columns);
data = this.turnHebrewText(data);
let contentDefinition = {
content: [
{ text: 'Clients List' },
{
table: {
widths: columns.map(() => '*'),
body: data
}
}
],
defaultStyle: {
font: 'alef'
}
};
pdfMake.createPdf(contentDefinition).download(name);
}
private turnHebrewText(data: string[][] | any[][]): any[][] {
let returnData = [];
for (let row of data) {
let rowData = [];
// reverse the order if need be
// row.reverse();
for (let cell of row) {
if (typeof cell === 'string') {
if (cell.charCodeAt(0) > 0x590 && cell.charCodeAt(0) < 0x5FF) {
cell = cell.split(' ').reverse().join(' ');
}
}
rowData.push(cell);
}
returnData.push(rowData);
}
return returnData;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment